Repository navigation
Speed up escaping strings and keys when writing TOML - #709
Merged
Merged
Conversation
|
🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge. Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements. |
frostming
approved these changes
Nov 10, 2026
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.
Summary
escape_string()walked the value one character at a time and sliceds[i:]at every position as soon as the value held anything to escape, so a value with a quote cost time quadratic in its length. It now builds one regex from the sequences actually present (longest first, so"""still matches whole) and replaces them in a singlere.sub. A value with nothing to escape returns early.StringType.escaped_sequencesandinvalid_sequencesnow return sets built once at import instead of rebuilding them on every call, andSingleKeychecks bare keys against a module frozenset instead of concatenating the allowed characters for every character of the key.Every string value and every new key goes through this path when a document is built from Python data, which is how Poetry writes
poetry.lockand how most tools calltomlkit.dumps(dict). Same-process A/B on CPython 3.14, 7 alternating rounds, median:dumps()of a 760 KBpoetry.lock(255 packages)dumps()of a 198 KBCargo.lockdumps()of a smallpyproject.tomltomlkit.string('a"b')dumps()of one 20 KB value with quotesOutput is unchanged. A differential fuzz (about 170k cases: random strings with quotes, backslashes, CR/LF, control characters,
"""and''', for all four string types, keys,Stringinputs, concatenation, pickling anddumps()) gave identical results. The function always returns a plainstr, also forstrsubclasses such asString, as before. The new test covers repeated quotes and every escape kind in one value.Duplicate Check
String.__add__aroundescape_string(), but only its call site, not the escaping itself.Agent Drafting Metadata