Skip to content

gh-152930: Make tomllib parse each table section in linear time#152931

Open
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-tomllib-quadratic-header
Open

gh-152930: Make tomllib parse each table section in linear time#152931
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-tomllib-quadratic-header

Conversation

@tonghuaroot

Copy link
Copy Markdown
Contributor

Resolve each table section's flags and nest node once when its [header]
line is parsed, and have key_value_rule walk only the key relative to that
anchor instead of re-walking header + key from the document root for every
key. A section with a depth D header and M keys goes from O(M * D) to
O(M + D).

Parse results are unchanged: the fix produces identical output against the
full toml-test 1.0.0 corpus and the existing test_tomllib suite, and
D=998, M=80000 (714 KB) drops from about 11.7 s to about 0.18 s.

key_value_rule re-walked the whole table header from the document root
for every key under it, so a deep header followed by many keys parsed in
time proportional to depth times keys. Resolve the section's flags and
nest node once per header and walk only the key relative to it, making
each section linear in depth plus keys.
@StanFromIreland

Copy link
Copy Markdown
Member

This should be first handled in https://github.com/hukkin/tomli.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants