Repository navigation
Improve speed of Json escaping - #1363
Open
tkaitchuck wants to merge 2 commits into
Open
tkaitchuck wants to merge 2 commits into
tkaitchuck wants to merge 2 commits into
Conversation
Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
This was referenced Oct 29, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Faster json escaping
When writing strings into json, it preforms a byte by byte scan to determine if characters need to be escaped.
This accelerates that process by using a bit twiddling implementation of the scan.
The code is written to operate on u32s (scanning 4 bytes at a time) but does 4 iterations at a time.
This is done deliberately to induce the compiler to optimize the loop into SIMD statements, without depending on any arch specific, unsafe, or non-portable code.
At -O2/-O3 this correctly vectorizes into SIMD instructions on i686, x64, and ARM64.
Even on platforms without SIMD instructions it will still operate 4 bytes at a time instead of 1, so there will be a minor perf gain.
Benchmark results
(canada is almost all numbers)
Instruction counts for different inputs:
No input is slower.
Tests & correctness
I have added a test to cover all branches to validate it. (This is somewhat abbreviated on miri for time)
Every byte that gets escaped is still decided by the original loop's ESCAPE lookup. The existing unsafe UTF-8 splits still happen only at ASCII bytes the table chose. So skip_to_escape can break output in only one way: by returning an index past a byte that needs escaping. Returning one too early (a false flag, or a position below the real one) costs speed, not correctness.
This was manually tested with two fuzzers compare output with serde_json's original per-byte algorithm
Explanation of algorithm
The core of the algorithm builds on the idea that it is possible to efficiently check if each byte in an integer is zero. This is accomplished though a standard trick. We can leverage this to compare bytes against known fixed characters (by xoring) and those less than a given value. By:
wordthat is less thann. (When subtracting no bit below it; a borrow out of that byte may flag bytes after it.)nwraps around to 0x80 or more, setting its high bit and borrowing from the next byte, exactly when it is less thann.n, so it borrows nothing from the next byte. Its high bit may still be set after the subtraction, but& !wordclears it, since the byte's own high bit is set. Such a byte is never flagged, even after a borrow.