Repository navigation
Conversation
Don't create TokenEntry instances unless needed for reporting. Matching algorithm and internal state can use more compressed representations with fewer indirections.
We could do more for memory consumption though
Huge performance degradation
This reverts commit 22a013b97b75f6f5c7e58fc323bb58932df67e67.
|
Thanks for this improvement. Unfortunately I won't have time to review it until the release tomorrow - and it seems to be a substantial change. |
This is a good idea. TBH i wasn't able to collect heap size numbers in an automated way. If you have ideas about how to do that that would be great |
I was also looking into this the last few days/weeks. We could continue, what I did in main...adangel:pmd:core/cpd-memory-usage and manually measure the metrics (like peak heap usage). This can be extended to monitor also cpu usage (see https://github.com/kabutz/teardown-talk/blob/master/src/main/java/eu/javaspecialists/tjsn/talks/teardown/util/Benchmark.java for an example). You can even monitor allocations (https://github.com/kabutz/teardown-talk/blob/master/src/main/java/eu/javaspecialists/tjsn/talks/teardown/util/ByteWatcherSingleThread.java). This approach basically uses the MBeans to query the VM statistics. But... there is JFR (JDK Flight Recorder) and this should bring us those numbers for free (and much more). JFR should be available since Java 11. As long as we run PMD with Java 11+ it should work. I played around with this now and I think, we should add it to the regression tester:
# start end end timestamps
jfr print --json --events jdk.JVMInformation,jdk.Shutdown recording.jfr |jq '.recording.events[0].values.jvmStartTime, .recording.events[1].values.startTime'
"2025-11-29T17:43:38.513+01:00"
"2025-11-29T17:50:50.947936009+01:00"
# duration in seconds
jfr print --json --events jdk.JVMInformation,jdk.Shutdown recording.jfr |jq '
[ .recording.events[0].values.jvmStartTime, .recording.events[1].values.startTime
| match("([0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+).[0-9]+([+-][0-9]+:[0-9]+)")
| .captures[0].string + .captures[1].string
| strptime("%Y-%m-%dT%H:%M:%S%z") | mktime
] | .[1] - .[0]'
432
jfr print --json --events jdk.JVMInformation,jdk.Shutdown recording.jfr |jq '
[ .recording.events[0].values.jvmStartTime, .recording.events[1].values.startTime
| match("([0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+).[0-9]+([+-][0-9]+:[0-9]+)")
| .captures[0].string + .captures[1].string
| strptime("%Y-%m-%dT%H:%M:%S%z") | mktime
] | (.[1] - .[0]) / 60 * 10 | floor / 10 | tostring + " minutes"'
"7.2 minutes"
# max heap memory
jfr print --json --events jdk.GCHeapSummary recording.jfr | jq '[.recording.events[].values.heapUsed]|max'
976278600
jfr print --json --events jdk.GCHeapSummary recording.jfr | jq '[.recording.events[].values.heapUsed]|max / 1024 / 1024 * 10 | floor / 10 | tostring + "GB"'
"931GB"
# max cpu load
jfr print --json --events jdk.CPULoad recording.jfr | jq '[.recording.events[].values.jvmUser]|max'
0.5767327
jfr print --json --events jdk.CPULoad recording.jfr | jq '[.recording.events[].values.jvmUser]|max * 1000 | floor / 10 | tostring + "%"'
"57.6%"
# average cpu load
jfr print --json --events jdk.CPULoad recording.jfr | jq 'add(.recording.events[].values.jvmUser) / (.recording.events | length)'
0.25153296741258757
jfr print --json --events jdk.CPULoad recording.jfr | jq '(add(.recording.events[].values.jvmUser) / (.recording.events | length) * 1000) | floor / 10 | tostring + "%"'
"25.1%"
|
|
+1 on using JFR. Low overhead, reliable, applicable to all PMD modes (CPD, PMD, etc.) and no maintenance on our end. |
|
I'm moving this to 7.23.0. I'm still working on the pmd-regression-tester (see pmd/pmd-regression-tester#169 ), but that won't be finished for this Friday's release. |
|
Moving this to 7.24.0. Depends on pmd/pmd-regression-tester#171 and new release of regression tester and #6515 |
We can't use try-with-resources here, as we compile against Java 8. But we build with Java 21 (enforced) and PMD uses that version for the auxclasspath. See also pmd#6224
|
Good news - the OOM error for alsa-firmware is fixed. We have now 487 new duplications. But I'm puzzled with the duplications for the java projects, e.g. checkstyle: we have 175 duplications less and 332 duplications changed. Or in other words: Before this change, we detected 507 duplications in checkstyle, now we only have 332. Looking at the xml reports directly, I can see:
--- base-abstractcheck.xml 2026-05-07 08:32:34.114667477 +0200
+++ patch-abstractcheck.xml 2026-05-07 08:32:59.678176449 +0200
@@ -1,16 +1,16 @@
<duplication lines="20" tokens="96">
- <file begintoken="37874"
+ <file begintoken="271"
column="5"
endcolumn="59"
endline="231"
- endtoken="37969"
+ endtoken="366"
line="212"
path="/home/runner/work/pmd/target/repositories/checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractCheck.java"/>
- <file begintoken="38646"
+ <file begintoken="434"
column="5"
endcolumn="59"
endline="218"
- endtoken="38741"
+ endtoken="529"
line="200"
path="/home/runner/work/pmd/target/repositories/checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java"/>
<codefragment><![CDATA[ }
I started to look into the duplications of IndentationCheckTest.java. Some are the same. But one example of a change is: previously, we matched a bigger chunk (94 tokens) + smaller chunks (85 tokens and 61 tokens). Now, we seem to match only the smaller chunk (61 tokens)?
@oowekyala What's the best approach to move forward here? |
Describe the PR
This PR is work I undertook a while ago with the goal of reducing memory usage in CPD. The side effect is also improved runtime, in particular, lexing can be done in parallel now.
The main changes w.r.t. memory usage are
The way I rewrote the algorithm allows for compacting several near-matches into one larger match. For this to happen all matches must have a common prefix longer than the minimum tokens parameter. This might report something like this:
This means that all of the duplicates have at least 130 tokens in common, and at least two of them have even 147 tokens in common. It prevents us from reporting different duplicates which look alike but only differ by a couple of tokens at their end, which is noisy. We would need an update to renderers like CSV and XML to report this properly.
Numbers
On my machine, running CPD on the JDK sources (14k files, 2.2MLOC):
Those numbers are without discounting warmup (so a lot of time is jvm startup), ran on an OpenJDK 21.0.8 runtime. Below I report some numbers for the same workload but with warmup time excluded.
Scaling case study (click to reveal)
I benchmarked CPD more closely on those sources to see how it scales w.r.t threads and found the following. (The plots use warmup iterations so the numbers are better than above).The most obvious thing here is that adding threads improves performance until it starts degrading it. This is caused by contention on the shared map that records the integer ID of each token image.
I improved this by adding a read-only map that stores pre-allocated IDs for common tokens (keywords, punctuation, etc). The concurrent map is only queried if the image is not one of those known tokens. From what I saw, 2/3 of the tokens are punctuation or keywords, so this is very effective to reduce contention on the map. Here is a refreshed plot with this optimization.
Note that these plots are comparing a late version of this branch and the latest optimization I added, they're not a comparison to main. But, I thought the plots look cool, and CPD on main wouldn't fit on the plot!
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)