← Back to Pull requests

gh-156241: Add a no-escape fast path to the _json str escapers #156242

Open
unknown opened this pull request with 412 lines diff across 5 files
View on GitHub
unknown unknown commented on Aug 22, 2026 19:59 UTC
Author

ascii_escape_unicode() and escape_unicode() now return the input bulk-copied with surrounding quotes when nothing needs escaping, via a shared quote_unescaped_unicode() helper, like the writer-based write_escaped_ascii()/write_escaped_unicode() already do.

| Benchmark | main | PR |
|---|---|---|
| dumps ascii 10 | 342 ns | 349 ns: ~same |
| dumps ascii 100 | 508 ns | 442 ns: 1.15x faster |
| dumps ascii 1 kB | 1.73 µs | 1.19 µs: 1.45x faster |
| dumps ascii 16 kB | 21.4 µs | 13.2 µs: 1.63x faster |
| dumps unicode 1 kB, ensure_ascii=False | 3.04 µs | 1.80 µs: 1.69x faster |
| dump list of 100 strings | 23.2 µs | 21.1 µs: 1.10x faster |
| dumps escapes 100 | 531 ns | 520 ns: ~same |
| dumps list of 3 short strings | 1.30 µs | 1.29 µs: ~same |

<details>
<summary>Benchmark script</summary>

"""Benchmark for the _json str-escaper no-escape fast path.

json.dumps(str) uses encode_basestring[_ascii] directly; json.dump() uses
it for every encoded string; dumps() of a container uses the writer-based
escapers (unchanged).
"""
import functools
import io
import json

import pyperf

CASES = {
    "ascii 10": "callsign-7",
    "ascii 100": "The quick brown fox jumps over the lazy dog. " * 2 + "0123456789",
    "ascii 1k": "abcdefghij" * 100,
    "ascii 16k": "abcdefghij" * 1600,
    "escapes 100": ('line\n"quoted"\ttab\\ ' * 5)[:100],
    "unicode 1k": "héllo wörld unicode … " * 47,
}


def dumps_obj(o, **kw):
    json.dumps(o, **kw)


if __name__ == "__main__":
    runner = pyperf.Runner()
    for name, s in CASES.items():
        runner.bench_func("dumps %s" % name, dumps_obj, s)
    runner.bench_func("dumps unicode 1k noascii",
                      functools.partial(dumps_obj, CASES["unicode 1k"],
                                        ensure_ascii=False))
    runner.bench_func("dumps list of 3 short strings", dumps_obj,
                      ["alpha-01", "beta-002", "gamma-03"])

    def dump_obj(o):
        json.dump(o, io.StringIO())
    runner.bench_func("dump list of 3 short strings", dump_obj,
                      ["alpha-01", "beta-002", "gamma-03"])
    runner.bench_func("dump list of 100 strings", dump_obj,
                      ["string number %04d of the benchmark payload" % i
                       for i in range(100)])

Run with python bench_escape.py --fast -o out.json per build; compare with pyperf compare_to base.json branch.json --table.

</details>

<!--
Thanks for your contribution!
Please read this comment in its entirety. It's quite important.

Pull Request title

It should be in the following format:

gh-NNNNNN: Summary of the changes made

Where: gh-NNNNNN refers to the GitHub issue number.

Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.

Backport Pull Request title

If this is a backport PR (PR made against branches other than main),
please ensure that the PR title is in the following format:

[X.Y] <title from the original PR> (GH-NNNNNN)

Where: [X.Y] is the branch name, for example: [3.13].

GH-NNNNNN refers to the PR number from main.

-->

<!-- gh-issue-number: gh-156241 -->

  • Issue: gh-156241

  • <!-- /gh-issue-number -->

    Diff: 412 lines (+309 / -103) across 5 files Reviewer: brittanyrey Review duration: 144118m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.52) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 412 lines diff across 5 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.360
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #156242"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 51.9% (MEDIUM) · Higher = larger blast radius
    51.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 28.7% · Safe to merge
    28.7%
    Repository