Commit 0d5d951
committed
gh-150878: Speed up json.dumps(ensure_ascii=False) for long strings
escape_size() sizes the ensure_ascii=False encoder output one character at a
time; a character needs escaping only when c == '"' || c == '\\' || c < 0x20,
and non-ASCII is kept verbatim. For the one-byte representation, detect the
no-escape case eight bytes at a time and return the verbatim size directly; a
length guard keeps short strings on the original per-character loop. Strings
with characters above U+00FF keep the current path.
Output is byte-identical, verified against test_json and a 199-case dumps
differential in both ensure_ascii modes. dumps of long 1-byte strings runs up
to 5.8x faster (4.2x for Latin-1 text); short keys and non-Latin-1 strings are
unaffected.1 parent 7a468a1 commit 0d5d951
2 files changed
Lines changed: 34 additions & 0 deletions
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
284 | 314 | | |
285 | 315 | | |
286 | 316 | | |
| |||
0 commit comments