Skip to content

perf(python): avoid repeated scans of long comment runs - #2866

Closed
doomsday1906 wants to merge 3 commits into
colbymchenry:mainfrom
doomsday1906:upstream/python-comment-scans-W9cb1df
Closed

doomsday1906 wants to merge 3 commits into
colbymchenry:mainfrom
doomsday1906:upstream/python-comment-scans-W9cb1df

Conversation

@doomsday1906

@doomsday1906 doomsday1906 commented Oct 23, 2026 •

Copy link
Copy Markdown

Native extraction of a valid Python module with 70,000 leading comments exceeded 90 seconds. The WASM fallback also exceeded a 15-second child deadline on module, function and class shapes.

Two causes combine: indexed child/sibling lookups repeatedly rescan wide extra-node lists, and Python's external scanner repeatedly looks ahead through indented comments. Walk affected children forward, bound backward documentation scans before switching to one forward sibling collection, and locate preceding decorators directly. Preserve documentation order, decorator wrappers, annotation step-over, doc filters, value references and inheritance.

Python's existing v0.23.6 generated parser and headers are copied unchanged from the pinned dependency into the established vendored-grammar build route (MIT license included). Only five scanner lines change. The WASM grammar is now built from those same patched sources; docs/grammars/tree-sitter-python.md records the rebuild command. Generated parser tables account for nearly all of the large diff; no grammar regeneration or version change.

Validation on Linux/WSL at d8e99e9:

  • Native release build and 226 focused extraction/grammar/documentation checks passed.
  • Wide-comment checks run both native and forced-WASM extraction with bounded child processes, retaining all 70,000 documentation lines, decorator links, base classes, calls and value references.
  • An independent comparison of old and rebuilt WASM grammars produced identical parse trees on 216 indentation/tab/line-ending/comment-alignment cases.
  • Normal CLI forced-WASM journeys indexed six stress files in 6.7 seconds on this upstream branch and 3.1 seconds on the maintained fork. Python AST and direct database inspection agreed on all eight functions/classes/methods, all 70,000 module documentation lines and the exact five call/value-reference edges.

Windows x64 and macOS arm64 native builds and native/WASM regressions passed on the exact PR head (237 tests per platform, including the native scaffold). Cross-platform validation. This PR excludes the maintained-fork installer, runtime identity and workstation configuration.

@doomsday1906
doomsday1906 marked this pull request as ready for review October 23, 2026 21:17
@doomsday1906 doomsday1906 changed the title Avoid repeated scans of long Python comment runs perf(python): avoid repeated scans of long comment runs Oct 23, 2026
@colbymchenry

Copy link
Copy Markdown
Owner

Thanks for speeding up Python files with tens of thousands of comment lines, @doomsday1906! Getting there vendors about 131k lines of grammar, patches its scanner, rebuilds its wasm and changes extraction code every language shares, which is a lot to carry for a rare file shape. Closing this one.

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