Skip to content

Commit c83a0e0

Browse files
KRRT7nedbat
andauthored
perf: skip AST parsing without soft keywords (#2240)
* perf: skip AST parsing without soft keywords * tighten the check for match/case With "and", still checks 59% of files. With "or" only checks 50% of files. * changelog --------- Co-authored-by: Ned Batchelder <[email protected]>
1 parent a57f981 commit c83a0e0

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ Unreleased
3232
- A performance improvement in the low-level line number bookkeeping when
3333
combining data files, thanks to `Kevin Turcios <pull 2239_>`_.
3434

35+
- Performance improvement in HTML reporting by reducing the number of times
36+
files have to be parsed, thanks to `Kevin Turcios <pull 2240_>`_.
37+
3538
.. _issue 2200: https://github.com/coveragepy/coveragepy/issues/2200
3639
.. _pull 2234: https://github.com/coveragepy/coveragepy/pull/2234
3740
.. _pull 2239: https://github.com/coveragepy/coveragepy/pull/2239
41+
.. _pull 2240: https://github.com/coveragepy/coveragepy/pull/2240
3842

3943

4044
.. start-releases

‎coverage/phystokens.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
9292

9393
def find_soft_key_lines(source: str) -> set[TLineNo]:
9494
"""Helper for finding lines with soft keywords, like match/case lines."""
95+
# Do a quick check first, to eliminate files with no possibility of soft keywords.
96+
# match/case is only a soft keyword if both words are in the source.
97+
if "match" not in source or "case" not in source:
98+
if sys.version_info < (3, 12) or "type" not in source:
99+
if sys.version_info < (3, 15) or "lazy" not in source:
100+
return set()
101+
95102
soft_key_lines: set[TLineNo] = set()
96103

97104
for node in ast.walk(ast.parse(source)):

0 commit comments

Comments
 (0)