Skip to content

cache file reporters and analyses for the whole reporting phase - #2215

Merged
nedbat merged 2 commits into
coveragepy:mainfrom
reaperhulk:more-caches
Jul 6, 2026
Merged

nedbat merged 2 commits into
coveragepy:mainfrom
reaperhulk:more-caches

Conversation

@reaperhulk

Copy link
Copy Markdown
Contributor

_analyze and _get_file_reporter used lru_cache(maxsize=1), which unfortunately doesn't work when reporting sweeps the file list, e.g. pytest-cov's total pass followed by its term report.

We now cache them in per-instance dicts instead, clearing whenever measured data changes (flush, load, combine, erase).

_analyze and _get_file_reporter used lru_cache(maxsize=1), which
unfortunately doesn't work when reporting sweeps the file list,
e.g. pytest-cov's total pass followed by its term report.

We now cache them in per-instance dicts instead, clearing whenever
measured data changes (flush, load, combine, erase).
@reaperhulk

reaperhulk commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor Author

For cryptography, running this on a 4 core Linux VM (5 averaged runs)

total pass term report total
7.15.0 3.74s 3.81s 7.55s
PR 3.78s 0.04s 3.81s

(The total pass is done first to do fail under checks, then it produces whatever potential output you might want, e.g. the terminal report, HTML, etc)

@nedbat
nedbat merged commit 9744860 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.

reaperhulk pushed a commit to reaperhulk/coveragepy that referenced this pull request Jul 13, 2026
Version 7.15.1 regressed memory during reporting: the whole-phase caches added in coveragepy#2215 retained every file's FileReporter and Analysis, and each cached PythonFileReporter pinned its parser's AST, so a 1,500-module branch corpus peaked at 555 MB vs 98 MB on 7.15.0.  PythonParser now releases its AST once _analyze_ast() has memoized everything derived from it, and the analysis and file reporter caches are bounded with oldest-first eviction, so modest projects keep the full repeated-sweep speedup while large projects get bounded memory; with both changes the corpus peaks at 127 MB.  The bound is configurable as the new [report] analysis_cache_size setting or the --analysis-cache-size flag on reporting commands, defaulting to 256 files.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0116eASQSH9ZQ9ZGwXtjobqH
reaperhulk pushed a commit to reaperhulk/coveragepy that referenced this pull request Jul 13, 2026
Version 7.15.1 regressed memory during reporting: the whole-phase caches added in coveragepy#2215 retained every file's FileReporter and Analysis, and each cached PythonFileReporter pinned its parser's AST, so a 1,500-module branch corpus peaked at 555 MB vs 98 MB on 7.15.0.  PythonParser now releases its AST once _analyze_ast() has memoized everything derived from it, and the analysis and file reporter caches are bounded with oldest-first eviction, so modest projects keep the full repeated-sweep speedup while large projects get bounded memory; with both changes the corpus peaks at 127 MB.  The bound is configurable as the new [report] analysis_cache_size setting or the --analysis-cache-size flag on reporting commands, defaulting to 256 files.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0116eASQSH9ZQ9ZGwXtjobqH
reaperhulk pushed a commit to reaperhulk/coveragepy that referenced this pull request Jul 13, 2026
Version 7.15.1 regressed memory during reporting: the whole-phase caches added in coveragepy#2215 retained every file's FileReporter and Analysis, and each cached PythonFileReporter pinned its parser's AST, so a 1,500-module branch corpus peaked at 555 MB vs 98 MB on 7.15.0.  PythonParser now releases its AST once _analyze_ast() has memoized everything derived from it, and the analysis and file reporter caches are bounded with oldest-first eviction, so modest projects keep the full repeated-sweep speedup while large projects get bounded memory; with both changes the corpus peaks at 127 MB.  The bound is configurable as the new [report] analysis_cache_size setting or the COVERAGE_ANALYSIS_CACHE_SIZE environment variable, defaulting to 256 files.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0116eASQSH9ZQ9ZGwXtjobqH
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