Repository navigation
Improve performance of String#dump using lookup table - #17287
Open
FletcherDares wants to merge 1 commit into
Open
FletcherDares wants to merge 1 commit into
FletcherDares wants to merge 1 commit into
Conversation
FletcherDares
force-pushed
the
fletcher/string-dump-ascii-fastpath
branch
4 times, most recently
from
June 16, 2026 18:21
aa8d435 to
921100f
Compare
FletcherDares
force-pushed
the
fletcher/string-dump-ascii-fastpath
branch
from
June 17, 2026 15:57
c05cf14 to
32c383b
Compare
FletcherDares
marked this pull request as ready for review
June 17, 2026 16:05
Contributor
|
I tried running the benchmark and got similar results. The tradeoffs might not be as big as you state, as the The tradeoff is real for interleaved printable and unsafe bytes though. |
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.
Background
String#dumpcurrently walks the input twice: once to compute the exact result length, and once to write the escaped output.For printable ASCII bytes, both passes still advance one byte at a time through the full switch/branch structure:
That is cheap for small strings, but it leaves a lot of overhead on common long strings where most bytes are safe ASCII and do not need escaping.
Proposal:
Reuse the existing
String#inspectsafe-ASCII lookup table introduced in #17126 forString#dump, and make it a shared string escaping table:The existing handling for quotes, backslashes, control bytes, UTF-8 non-ASCII bytes, invalid bytes, and non-ASCII-compatible encodings stays on the current paths.
Justification:
The common case for
String#dumpis printable ASCII text with only occasional bytes that need escaping. Before this change, a long run like:still paid per-byte dispatch in both the length calculation and the copy pass. After this change, those runs are skipped/copied in bulk, while unsafe bytes still fall back to the existing logic.
Tradeoffs:
This adds a lookup-table scan after the first printable byte in the generic printable branch. Inputs with long safe ASCII runs benefit substantially.
Inputs with few or no safe ASCII runs can be slower because they pay the added table check without getting much bulk-copy benefit.
Results: