Investigate and reduce memory and CPU usage on large code bases
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- php
- Domain
- performance
Research direction
Start with phpbench.json and benchmarks/, then inspect collectFromPaths() and the Parser visitors named in the issue. Add a repeatable benchmark with run instructions, and measure memory and time on a pinned large code base before investigating the listed hypotheses. Done means the benchmark and a write-up are in the repo, with any worthwhile improvements supported by before and after measurements.
Written by the indexing model from the issue text.
Description
Background
#91 found that the batching and streaming report API never reduced memory, so it is being deprecated in #118.
Memory on large code bases is still a real concern, though.
This ticket investigates where the memory and time actually go, and improves them where the measurements justify it.
Current numbers
Measured with bin/phpcca analyse <path> --report-type=json in the project's Docker image (PHP 8.5), cache disabled:
| Code base | Files | Classes | Peak memory |
|---|---|---|---|
src/ |
150 | – | 18 MB |
vendor/ |
5,653 | 4,499 | 164 MB |
Around 5,000 files already exceeds PHP's common CI default of memory_limit=128M.
Hypotheses to check (measure first)
- Unused metrics are always calculated and kept.
Parseralways runs the Halstead and cyclomatic visitors.
EveryCognitiveMetricskeeps aHalsteadMetrics,CyclomaticMetricsandUnderstandabilityMetricsobject, plus the cyclomatic decision-point breakdown, even whenshowHalsteadComplexity,showCyclomaticComplexityandshowUnderstandabilityare off.
Skipping or dropping them when they are not shown, sorted on, or used in a report could save memory and CPU. - Per-method object size.
CognitiveMetricshas about 40 properties, including 12 nullableDeltaobjects that are only used with a baseline.
Check the real size per method and whether lazy or compact storage is worth it. - Visitor state across files.
Check that per-file visitor state (for example Halstead class and method metrics, and the annotation visitor's ignored items) is released after each file and does not grow with the project. - File list materialisation.
collectFromPaths()turns the whole file iterator into an array.
Probably small, but confirm. - Cache hits.
Compare memory and time for a cold run, a warm-cache run, and a run with the cache disabled.
Approach
- Add a repeatable benchmark:
phpbenchis already configured (phpbench.json,benchmarks/), or a small script that reportsmemory_get_peak_usage(true)and wall time.
Use a large, pinned code base such asvendor/or a fixed Symfony checkout. - Profile memory (for example with
memory_get_usage()checkpoints, or php-meminfo or Blackfire) to find the biggest contributors before changing code. - Implement only the changes that show a clear win, one per PR, with before and after numbers in the PR description.
- Avoid behaviour changes.
Output, reports, baselines and cache files must stay the same.
If a change needs a public API break, deprecate first and remove in 2.0, like #91 and #92.
Done when
- The benchmark is in the repo, with instructions to run it.
- This issue has a short write-up of where memory and time go.
- Every improvement that clearly pays off is merged with measured numbers, or this issue documents why none was worth it.
- Dominant language
- PHP
- Stars
- 55
- Forks
- 3
- Avg merge
- 25m
- Merged PRs (30d)
- 22
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Phauthentic/cognitive-code-analysis
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Phauthentic/cognitive-code-analysis#94 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Phauthentic/cognitive-code-analysis#93 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Phauthentic/cognitive-code-analysis#96 ·
Maintainers usually reply within 1 day
-
backlog enhancement help wanted
Difficulty 5/5 Over a week Newbie friendliness 25/100
Phauthentic/cognitive-code-analysis#30 · 1 comment ·
Maintainers usually reply within 1 day
-
backlog enhancement help wanted
Difficulty 3/5 1-2 days Newbie friendliness 42/100
Phauthentic/cognitive-code-analysis#27 ·
Maintainers usually reply within 1 day
All issues in Phauthentic/cognitive-code-analysis
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
My-Little-Forum/mylittleforum#936 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 84/100
mailcow/mailcow-dockerized#7652 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100