string serialization escaping optimisations#1273
Merged
dtolnay merged 2 commits intoserde-rs:masterfrom Jul 18, 2025
Merged
Conversation
…ndtrip EscapeChar to the u8 without actually branching
b59f06b to
de70b7d
Compare
dtolnay
reviewed
Jul 18, 2025
Member
dtolnay
left a comment
There was a problem hiding this comment.
Thanks!
Would there be any benefit to coordinating the discriminants of CharEscape with the values that are being converted to/from it in this code, even if as cannot be used?
#[repr(u8)]
pub enum CharEscape {
Quote = b'"',
ReverseSolidus = b'\\',
Solidus = b'/',
Backspace = b'b',
FormFeed = b'f',
LineFeed = b'n',
CarriageReturn = b'r',
Tab = b't',
AsciiControl(u8) = b'u',
}
Contributor
Author
I had that in a draft of this PR, but I couldn't get any benefit from it unfortunately. Maybe it still makes sense from a semantic point of view |
dtolnay
approved these changes
Jul 18, 2025
takumi-earth
pushed a commit
to earthlings-dev/json
that referenced
this pull request
Jan 27, 2026
…scaping string serialization escaping optimisations
1 task
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.
While serializing strings, we enter the hot loop checking 1 byte at a time. By massaging the code and using a little bit of unsafe, we can optimise this hot loop a bit.
This benchmark shows about a 2-5% speedup when working with string heavy documents
Benchmark was run with target x86_64-unknown-linux-gnu on an Intel Xeon Platinum 8375C CPU @ 2.90GHz, as well as on an Apple M4 Max.