gh-150875: Speed up JSON string encoding for long ASCII strings - #150876
gh-150875: Speed up JSON string encoding for long ASCII strings#150876gaborbernat wants to merge 2 commits into
Conversation
ascii_escape_size() scans each string one character at a time to size the escaped output, and write_escaped_ascii() writes it verbatim when nothing needs escaping. For the one-byte representation, detect that 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 that need escaping and non-Latin-1 strings 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 ASCII strings runs up to 5.3x faster; short keys, escaped strings, and non-ASCII are unaffected.
Cover long runs that cross the scan windows and the short-string guard, with a character needing escaping at every offset in 1-byte and wider strings, plus the no-escape verbatim fast path and \uXXXX escaping of non-ASCII.
|
Added |
|
This PR is stale because it has been open for 90 days with no activity. |
|
@picnixz any appetite to get this merged or should we close it? Thanks! |
|
_Py_popcount32() has a 32-bit SWAR (SIMD Within A Register) implementation when built-in popcount() function is not available. It's just a few lines but it has long comments to explain it. I'm not sure about this change, it looks to me quite complicated and hard to maintain. I'm not sure that it's worth it. But I don't consider myself as a json module maintainer :-) |
json.dumpsescapes each string by first scanning it one character at a time to size the escaped output (ascii_escape_size), after whichwrite_escaped_asciicopies the string verbatim when nothing needs escaping. For a long string with no characters that need escaping, which is the common case for text values, log messages, and other long content, that per-character sizing scan is pure overhead before the verbatim copy.This detects the no-escape case on the one-byte (ASCII/Latin-1) representation eight bytes at a time, so it returns the verbatim size after about one eighth of the work. It is the encode-side counterpart to #150872; the two touch different code paths and are separate changes.
What we do now (scalar, one code point at a time)
S_CHARis printable ASCII except"and\, so a byte needs escaping whenc < 0x20 || c > 0x7e || c == '"' || c == '\\'. For a long escape-free string this reads and tests every byte just to learn that the output equals the input plus two quotes.What SWAR does (8 bytes at a time, in one register)
SWAR is "SIMD within a register": load 8 bytes into a single
uint64_tand test all 8 lanes at once with ordinary integer ops.haszero(v) = (v - 0x0101…) & ~v & 0x8080…lights the high bit of exactly the zero lanes, with no false positives or negatives. Broadcasting a byte (b * 0x0101…) and XOR-ing turns "equals b" into "is zero". The range checks< 0x20and> 0x7ereuse the same idea. When all 8 lanes are ordinary, the loop advances 8 bytes; at the first lane that needs escaping it breaks and the existing per-character loop computes the exact size and does the work. A length guard keeps short strings (the common dict key) on the original loop, where the fast path's setup would not pay off.These are the same
0x0101…/0x8080…masks thatObjects/unicodeobject.candObjects/stringlib/find_max_char.halready use for ASCII scanning.When and how this changes performance
json.dumps, current encoder versus this change:The gain scales with string length. The short-string guard keeps key-heavy documents unaffected; an earlier guardless version measured about 1.18x slower on a 2000-short-key document, which the guard removes.
Correctness
Output is byte-identical to the current encoder. Verified against the full
test_jsonsuite and a 199-case differential corpus that places each escape-relevant character (",\\, control chars,0x7f, and non-Latin-1 characters) at every offset across the eight-byte window, in bothensure_ascii=Trueandensure_ascii=Falsemodes. Every output matched.Benchmark
References for the bit tricks: Sean Anderson, Bit Twiddling Hacks (zero byte, byte equal to n, byte less than n); Henry S. Warren Jr., Hacker's Delight, 2nd ed., chapter 6.
It is not the SIMD parsing backend from #142915: it adds no intrinsics, no CPU detection, and no build configuration, and it does not depend on #125022.
Resolves #150875.