Skip to content

only walk statement nodes when scanning for defs and classes - #2216

Merged
nedbat merged 3 commits into
coveragepy:mainfrom
reaperhulk:ast-walk
Jul 6, 2026
Merged

nedbat merged 3 commits into
coveragepy:mainfrom
reaperhulk:ast-walk

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

_raw_parse and AstArcAnalyzer.analyze use ast.walk to find module, function, and class nodes, visiting every node in the tree. Those are all statements, which can never appear inside expression subtrees.

We can optimize by adding walk_statement_nodes which filters out statements.

This also replace analyze()'s per-node getattr dispatch with isinstance checks against the three handler types.

Some performance tests as run by claude (4 core x86_64 VM):

command 7.15.0 ast-walk delta
report 4.25s 3.83s -10.0%
html 12.88s 12.27s -4.7%
xml 4.77s 4.38s -8.2%
json 5.38s 5.10s -5.2%
lcov 7.85s 7.21s -8.2%

~0.4s saved (with some variance due to standard benchmarking challenges).

_raw_parse and AstArcAnalyzer.analyze used ast.walk to find module,
function, and class nodes, visiting every node in the tree. Those are
all statements, which can never appear inside expression subtrees.

We can optimize this with walk_statement_nodes, which is ast.walk
with the enqueue filtereds.

This also replace analyze()'s per-node getattr dispatch with
isinstance checks against the three handler types.
@nedbat
nedbat merged commit cb7c59a into coveragepy:main Jul 6, 2026
42 checks passed
@nedbat

nedbat commented Jul 12, 2026

Copy link
Copy Markdown
Member

This is now released as part of coverage 7.15.1.

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