Skip to content

Make Source index-based instead of materializing a char list#489

Merged
frostming merged 1 commit into
python-poetry:masterfrom
AstekGroup:perf/index-source
Jun 8, 2026
Merged

Make Source index-based instead of materializing a char list#489
frostming merged 1 commit into
python-poetry:masterfrom
AstekGroup:perf/index-source

Conversation

@tfoutrein
Copy link
Copy Markdown
Contributor

What

Source.__init__ built iter([(i, TOMLChar(c)) for i, c in enumerate(self)]), allocating one tuple and one TOMLChar for every character of the whole input before parsing even starts.

This tracks an integer index over the underlying string instead: inc() bumps the index and reads self[idx], and _State save/restore snapshots the index rather than copy()-ing an iterator. Construction becomes O(1) and per-character work is deferred to the read.

Benchmarks

Parsing speedup across document sizes/shapes (median, interleaved A/B vs master):

document speedup
large flat (~90 KB) 1.13×
poetry.lock-like (~64 KB) 1.11×
array-heavy 1.09×
nested / string-heavy 1.07×
typical mixed (~4 KB) 1.08×

No regression on any shape (tiny inputs unchanged).

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 master on a varied corpus.

`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.
@frostming frostming merged commit 231370c into python-poetry:master Jun 8, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants