Scan character runs in bulk while parsing#490
Open
tfoutrein wants to merge 2 commits into
Open
Conversation
`Source.__init__` built `iter([(i, TOMLChar(c)) for i, c in enumerate(self)])`, allocating one tuple and one TOMLChar per character of the whole input up front. Track an integer index into the underlying string instead: `inc()` bumps the index and reads `self[idx]`, and state save/restore snapshots the index rather than copying an iterator. Construction is O(1) and per-character work is deferred to the read. No behaviour change (full suite incl. the toml-test conformance submodule passes); ~1.07-1.14x faster parsing across document sizes.
The parser advanced one character at a time through runs of whitespace, bare-key and number characters, paying a `Source.inc()` call (attribute lookups + a `TOMLChar` build + bounds check) for every character. Add `Source.advance_while(charset)` / `advance_until(stopset)`, which scan the underlying string in a single pass and update the index and current character only once, and use them for the leading-whitespace, bare-key and number/date runs. Same value contract as the `while ... and self.inc()` loops they replace. No behaviour change (full suite incl. the toml-test conformance submodule passes; round-trip output byte-identical on a varied corpus). ~1.05-1.32x faster parsing depending on shape (e.g. ~1.26x on a poetry.lock-like file).
This was referenced Jun 5, 2026
Contributor
|
LGTM, please resolve the conflicts. BTW: we are merging by "squash and merge" so the stacked PR is inconvenient. |
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.
What
The parser advances one character at a time through runs of whitespace, bare-key and number characters, paying a
Source.inc()call (attribute lookups + aTOMLCharbuild + bounds check) for every character of the run.This adds
Source.advance_while(charset)/advance_until(stopset), which scan the underlying string in a single pass and update the index + current character only once, and uses them for the leading-whitespace, bare-key and number/date runs. They keep the exact value contract of thewhile ... and self.inc()loops they replace (on returncurrentis the first character outside / inside the set, or EOF).Benchmarks
Parsing speedup across document sizes/shapes (median, interleaved A/B vs
master, includes #489):No regression on any shape.
Tests
Full suite passes (972 tests, incl. the toml-test conformance submodule). No public API or behaviour change — round-trip output is byte-identical to
masteron a varied corpus.