Repository navigation
[core] Incremental analysis - #125
Conversation
There was a problem hiding this comment.
Report listeners are synchronized, even 'though the cache is thread-safe... I've not profiled it, but it may be a cause of contingency. Any better way around this?
Also, do listeners really need to be synchronized? Can't we just make them thread-safe?
There was a problem hiding this comment.
The listeners might be provided by external libraries/programs, when they call PMD - so they could potentially be not-thread-safe...
I think, we had this problem, once we switched on multi-threading by default....
The underlying problem is of course the API the PMD provides for integration: How to execute PMD and how to gather the results (violations including processing errors and suppressions). If we find a better way than these listeners, we can mark this as deprecated. Maven-pmd-plugin uses a custom renderer by now to collect the results and doesn't use the listener anymore....
There was a problem hiding this comment.
I don't like this at all, but can't think of a better way other than making it nullable and checking against null all the time.... ideas?
There was a problem hiding this comment.
This checksum reminded me of hashCode - especially in RuleSets.
What about a "ChecksumAware" interface (name not good)?
Is it correct, that only RuleSet instances, that would be created programmatically, could end up without a checksum? For any other case, we would load the ruleset from file and use the file checksum, right?
So, maybe it's enough to mark this constructor as deprecated and leave it and remove it in a later version....
There was a problem hiding this comment.
Sounds good. The name reminds me a bit of Spring Framework's Aware interface and subinterfaces, it probably works. I'll refactor it as soon as I get the chance. Yes, it's implemented as a hashCode on RuleSets. As a checksum I just want something that can be generated fast, and will change if anything changes.
Right now this second constructor is used only in unit tests, the designer, the benchmarker and XPathCLI. None of these should use the analysis cache, except maybe XPathCLI?
In future versions we may have Rule be ChecksumAware and have RuleSet compute their checksums off the Rules, just like RuleSets does from RuleSet maybe?
There was a problem hiding this comment.
I tend more towards just defining a RuleSet must always be ChecksumAware and we do not allow a way around it. This means, we do not need to mark RuleSets explicitly as ChecksumAware.
Maybe we can make the constructor of RuleSet private and allow to create rulesets only through RuleSetFactory, which
- either loads a ruleset from an XML file - then we have the checksum
- or creates a ruleset with a single xpath rule - we could create the checksum based on the xpath expression. That would be the XPath CLI use case.
RuleSets can stay the way it is - always calculating the checksum based on the checksums of the contained RuleSet instances.
The XPathCLI is btw. a very old client - it's purpose seems to be to execute a XPath expression against a source file and display the matches. This is exactly what the Designer is able to do within its GUI. It is not even exposed in the start script, so we might ditch this XPathCLI altogether.
There was a problem hiding this comment.
Should we split this into the AnalysisCache and a separate persistence strategy, using composition rather than inheritance?
There was a problem hiding this comment.
I would say, it's not needed yet - or do you have another persistence strategy in mind already?
There was a problem hiding this comment.
Nothing in particular, that' why I did it this way. But still wanted someone else's opinion.
- Only invalidated by PMD version change. - Hooked to CLI and ANT. - A NOOP and a File based implementations available. - Unit tested where possible. - Still several TODOs to handle.
- Not completely convinced by this implementation, but is simple enough
and it's working.
- Split the generic cache implementation from the file-backing persistence
strategy. This could probably work better as a composition rather than
inheritance, but we will take it one step at a time.
- Took the chance to improve error messages produces by FileAnalysisCache
- Make sure the `ruleSet` collection has a consistent access order.
- A simple constructor will work just fine.
- Plus, this makes it even more obvious that no exceptions / null values
are to be expected when loading a cache from file, even when it's invalid
or badly configured.
- indentation fixes, missing licenses, some javadoc, complete some TODOs
4b635a7 to
0d684d2
Compare
|
PR looks good! Thanks! Once the feature is ready for use, we should update the command line / ant documentation: https://github.com/pmd/pmd/blob/master/src/site/markdown/usage/ant-task.md and https://github.com/pmd/pmd/blob/master/src/site/markdown/usage/running.md.vm For the cache file, I have one question: How do we handle compatibility? E.g. we change the file format, etc... Ok, it starts with the PMD version string. Is this enough? |
|
@adangel I think so, yes. I don't see how a same version of PMD would produce two incompatible versions of the cache, and I'd rather invalidate by version, since new versions may fix false positives / false negatives meaning we want to re-analyze in spite of what the cache may say. Of course this will have to be reviewed if we change the release process and stop releasing all jars at the same time with the same version numbers, but until then... |
|
Btw. It seems we have a benchmark measuring problem when running multithreaded:
The "Total PMD" time is too small, leading to a negative "Non-measured total"... The Total PMD time should be the total time, PMD was running... |
|
I believe the issue raises because we are measuring different things. Total PMD is wall clock time, from start to finish. Measured total however is total CPU time. Each thread runs for x time on a given task, and that time is stored and added for "Measured Total". Since threads run in parallel, we are adding up more "processing seconds" than actual seconds of execution. |
- This follows discussion on pmd#125 (comment) - We add a marker interface for thread-safe listeners, and implement it were appropriate. - Breaking change: `getSynchronizedListeners` and `addSynchronizedListeners` now handle `List<ThreadSafeReportListener>`. - We may mark the non-thread-safe methods and interfaces as deprecated and force everyone to handle synchronization on their own for future releases.
This turned out to be much simpler to implement than expected, however, I'd love to have some in-depth peer review. There are actually a couple points on which I'm not completely sure and would love some feedback.
The results are really promising (always analyzing the complete PMD source code):
Mono thread - java-braces - noop cache
-d ./ -b -f text -r no-cache.report -R java-braces -threads 0Mono thread - java-braces - empty file cache
-d ./ -b -f text -r empty-cache.report -R java-braces -threads 0 -cache pmd.cache~0.65 overhead for writing the file cache.
Mono thread - java-braces - primed file cache
-d ./ -b -f text -r primed-cache.report -R java-braces -threads 0 -cache pmd.cacheHuge gains since we skipped analyzing several files altogether.
Of course, all reports are exactly the same:
I repeated the experiment with a somewhat more complex ruleset: http://static.monits.com/pmd.xml. More rules raises the chances of having violations, and therefore, the misses on the cache (wihch right now only saves files with no violations).
Mono thread - http://static.monits.com/pmd.xml - no cache
Mono thread - http://static.monits.com/pmd.xml - outdated file cache
We are reusing the cache from the previous run, but the ruleset changed, invalidating it. So we have the extra cost of loading, and no benefits. This is the worst case scenario.
~2.8 overhead for reading & writing the file cache.
Mono thread - http://static.monits.com/pmd.xml - primed file cache
Once again, significant gains, even with a much lower hit ratio.
Multi thread (4 threads) - http://static.monits.com/pmd.xml - no cache
Multi thread (4 threads) - http://static.monits.com/pmd.xml - empty file cache
~3.1 secs overhead
Multi thread (4 threads) - http://static.monits.com/pmd.xml - primed file cache
Once again, significant gains, even with a much lower hit ratio.
I still need to run this through a profiler. The overhead, specially for reading, seems to be a little excesive, 'though I see nothing obvious in the code (I'm using a
BufferedInputStream), and it could also be a meassuring error (the problem with single run measurements when difference is smaller than the noise).