Skip to content

Speed up parsing of comments by scanning the comment text in bulk - #723

Merged
frostming merged 1 commit into
python-poetry:masterfrom
g4lb:perf/bulk-scan-comments
Nov 27, 2026
Merged

frostming merged 1 commit into
python-poetry:masterfrom
g4lb:perf/bulk-scan-comments

Conversation

@g4lb

@g4lb g4lb commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

The parser read each comment one character at a time. This change scans the comment text with Source.advance_until(), as the string parser already does (#491, #518). The output and the errors do not change.

parse(), CPython 3.12 Before After Change
16 KB pyproject.toml with comments 6.87 ms 6.12 ms -10.9%
Same file, then edit and dumps() 7.56 ms 6.94 ms -8.2%
200 long comments 8.99 ms 3.87 ms -57.0%
6000 array values with comments 44.5 ms 41.1 ms -7.6%

CPython 3.9 and 3.14 give -11.9% and -8.7% on the pyproject.toml row. Lock files with no comments do not change. Short and empty comments change by at most +1.0%, which is in the noise. Import time did not change.

Checks: pytest tests passes on CPython 3.9, 3.12 and 3.14 and on PyPy 3.11. pre-commit run passes. Old and new code gave the same results on 1,015,600 inputs. Two new tests check the comment round trip and the error position.

Duplicate Check

  • I searched the open PRs and found none that already fixes this issue.
  • Related open PRs (if any, and why this PR is still needed): none change _parse_comment_trail().

Agent Drafting Metadata

  • Agent: Claude Code
  • Model: Claude Opus 5.5
  • Notes: Helped with the code and the benchmarks.

The comment body was read one character at a time, with a control-char
check and a Source.inc() call per character. Scan it with
Source.advance_until() up to the line break or the first control char
that is invalid in a comment, as the string-body fast paths already do.
The output and the errors do not change.
@human-attestation

human-attestation Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Human Attestation: @g4lb attested this PR at commit 5ea195c (2026-11-26T08:17:05.796Z).

I have read and understood all agent-written code in this PR. / 我已阅读并理解此 PR 中 Agent 编写的全部代码。

Later pushes to this PR stay attested. Attestation page

@frostming
frostming merged commit 3cdcbde into python-poetry:master Nov 27, 2026
26 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