Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(report): replace translate with str.replace in _json_for_script
- a multi-character str.translate table leaves the CPython ASCII fast
  path, so escaping the embedded JSON was ~10x slower than json.dumps
  on large reports (regression since 1.0.1)
- on a 5.2 MB payload: before ~210 ms, after ~22 ms, json.dumps ~14 ms
- str.replace on <, >, &, U+2028 and U+2029 keeps the fast path
- output is byte-identical, checked by a new test against the 1.0.1
  translate table
- add a CHANGELOG entry

Closes #457
  • Loading branch information
digalaviz committed Nov 8, 2026
commit d7f193075152a03ada893246ecd9c5d9627d9650
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Each release links to full notes on the
gallery step of `generate_report()` fail without writing a page
([#454](https://github.com/awslabs/stickler/issues/454))

- **The HTML report is fast to write again with `individual_results_jsonl_path`.**
1.0.1 escaped the report's embedded JSON with a `str.translate` table, which
is about ten times slower than `json.dumps` on large reports. The same escapes
are now applied with `str.replace`, and the page bytes are unchanged
([#457](https://github.com/awslabs/stickler/issues/457))

## [1.0.1] - 2026-11-02

### Changed
Expand Down
27 changes: 17 additions & 10 deletions src/stickler/reporting/html/html_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,27 @@
# the JSON identical to the page's JavaScript while no document value can end
# the block early (#431). `json.dumps` already escapes U+2028/U+2029 under its
# default `ensure_ascii=True`, so those two entries do not change today's
# output; they are kept so the table stays correct if that ever changes, since
# both are line terminators outside a JS string literal.
_SCRIPT_SAFE = str.maketrans({
"<": "\\u003c",
">": "\\u003e",
"&": "\\u0026",
"\u2028": "\\u2028",
"\u2029": "\\u2029",
})
# output; they are kept so the escapes stay correct if that ever changes, since
# both are line terminators outside a JS string literal. The escapes are
# applied with `str.replace`, not `str.translate`: `translate` with
# multi-character replacements leaves CPython's ASCII fast path at the first
# `<`, `>` or `&`, which made large reports about ten times slower to write
# (#457).
_SCRIPT_SAFE_ESCAPES = (
("<", "\\u003c"),
(">", "\\u003e"),
("&", "\\u0026"),
("\u2028", "\\u2028"),
("\u2029", "\\u2029"),
)


def _json_for_script(value: Any) -> str:
"""Serialize ``value`` as JSON that is safe to place inside ``<script>``."""
return json.dumps(value).translate(_SCRIPT_SAFE)
script_safe_json = json.dumps(value)
for unsafe_char, json_escape in _SCRIPT_SAFE_ESCAPES:
script_safe_json = script_safe_json.replace(unsafe_char, json_escape)
return script_safe_json


class EvaluationHTMLReporter:
Expand Down
31 changes: 31 additions & 0 deletions tests/reporting/html/test_script_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,34 @@ def test_json_for_script_round_trips_and_leaves_no_markup(value):
assert json.loads(out) == value
for ch in "<>&\u2028\u2029":
assert ch not in out


# The 1.0.1 escape, kept as the oracle: the faster implementation must emit the
# same bytes for every input, so the #431 guarantee does not depend on timing.
_TRANSLATE_REFERENCE = str.maketrans(
{
"<": "\\u003c",
">": "\\u003e",
"&": "\\u0026",
"\u2028": "\\u2028",
"\u2029": "\\u2029",
}
)


@pytest.mark.parametrize(
"value",
[
PAYLOAD,
"a & b <= c >= d",
"x\u2028y\u2029z",
"café 中文 😀 <b>",
"already escaped \\u003c and &amp;",
{"k</script>": ["<!--", "-->", 1.5, None, True]},
[{"reason": "score 0.91 >= threshold 0.8"}, {"reason": "0.2 < 0.5"}],
None,
"",
],
)
def test_json_for_script_is_byte_identical_to_the_translate_escape(value):
assert _json_for_script(value) == json.dumps(value).translate(_TRANSLATE_REFERENCE)
Loading