Skip to content

[core] Performance: Lots of time spent doing XML initialization #6155

Open

Description

@AB-xdev

Is your feature request related to a problem? Please describe.

While profiling PMD I noticed that a lot of time is spent in this stack:

com.intellij.util.lang.JarLoader.getResource(String)
com.intellij.util.lang.ClassPath.findResource(String)
com.intellij.ide.plugins.cl.PluginClassLoader.doFindResource()
com.intellij.ide.plugins.cl.PluginClassLoader.findResource(String)
java.lang.ClassLoader.getResource(String)
java.lang.Class.getResource(String)
org.xmlresolver.XMLResolverConfiguration.loadConfiguration(List, List)
org.xmlresolver.XMLResolverConfiguration.<init>(List, List)
net.sf.saxon.lib.CatalogResourceResolver.<init>()
net.sf.saxon.Configuration.init()
net.sf.saxon.Configuration.<init>()
java.lang.invoke.Invokers$Holder.invokeExact_MT(Object, Object)
jdk.internal.reflect.DirectConstructorHandleAccessor.invokeImpl(Object[])
jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(Object[])
java.lang.reflect.Constructor.newInstanceWithCaller(Object[], boolean, Class)
java.lang.reflect.ReflectAccess.newInstance(Constructor, Object[], Class)
jdk.internal.reflect.ReflectionFactory.newInstance(Constructor, Object[], Class)
java.lang.Class.newInstance()
net.sf.saxon.Configuration.newConfiguration()
net.sourceforge.pmd.lang.rule.xpath.internal.SaxonXPathRuleQuery.initialize()
net.sourceforge.pmd.lang.rule.xpath.internal.SaxonXPathRuleQuery.<init>(String, XPathVersion, Map, XPathHandler, DeprecatedAttrLogger)
net.sourceforge.pmd.lang.rule.xpath.XPathRule.initialize(LanguageProcessor)
net.sourceforge.pmd.lang.rule.RuleReference.initialize(LanguageProcessor)
net.sourceforge.pmd.lang.rule.internal.RuleSets.initializeRules(LanguageProcessorRegistry, PmdReporter)
net.sourceforge.pmd.lang.impl.BatchLanguageProcessor.launchAnalysis(LanguageProcessor$AnalysisTask)
net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(List, List)
net.sourceforge.pmd.PmdAnalysis.performAnalysisImpl(List)
net.sourceforge.pmd.PmdAnalysis.performAnalysisAndCollectReport()

It looks like net.sf.saxon.Configuration.newConfiguration() is extremely slow as it always completely initializes everything for every XPath based rule that is in a RuleSet.

It looks like most performance is lost by constantly loading a xmlresolver.properties file in the upstream xmlresolver library:
https://github.com/xmlresolver/xmlresolver/blob/a45539745a596b17db4ab227ed6e6c4b53e6022a/src/main/java/org/xmlresolver/XMLResolverConfiguration.java#L373

Describe the solution you'd like
Performance should be improved here.
The XML should only be loaded once, maybe this needs to be fixed upstream or it's just a usage problem here.

Describe alternatives you've considered
N/A

Additional context
This is executed during IDE code highlighting where fast feedback is critical!

Activity

  1. added
    an:enhancement
    on Oct 21, 2025
  2. AB-xdev commented on Oct 21, 2025

    @AB-xdev
    Author

    It might also be an idea to maybe skip

    public void initialize(LanguageProcessor languageProcessor) {
    String xpath = getXPathExpression();
    XPathVersion version = XPathVersion.DEFAULT;
    try {
    xpathRuleQuery = new SaxonXPathRuleQuery(xpath,
    version,
    getPropertiesByPropertyDescriptor(),
    languageProcessor.services().getXPathHandler(),
    attrLogger);
    } catch (PmdXPathException e) {
    throw addExceptionContext(e);
    }
    }

    when xpathRuleQuery is already present and the underlying XPathHandler is identical to the previously used one.

  3. jsotuyod commented on Oct 21, 2025

    @jsotuyod
    Member

    I'd not go so far as to not-reinitialize the SaxonXPathRuleQuery, as that is very implementation dependent (ie: it would assume that XPathHandler implementations are stateless / immutable, which is true right now, but may change over time).

    However, I think that adopting a single, statically shared Configuration instance would probably be feasible… the Saxon doc doesn't explicitly call it out, but it seems this is only a settings holder + factory, which would make it thread-safe. It also mentions that node identity is tied to the configuration, but that should not be an issue as far as I can tell… something we could play with for sure.

    Having said that, it would be great if we could put numbers to "Lot's of time spent doing XML initialization".

    If you can share benchmarking data / microbenchmark results it would be great. Otherwise, it's really hard to asses the actual value of investing into this line of work…

  4. AB-xdev commented on Oct 22, 2025

    @AB-xdev
    Author

    If you can share benchmarking data / microbenchmark results it would be great.

    According to the IntelliJ profiler (CPU time only): performAnalysisAndCollectReport took 76ms of which 38ms (50%) were spent with XML initialization. The rest was cacheFile validation and other overhead.

    As mentioned above our use case is on-the-fly analysis during code highlighting so every wasted ms counts ;)

  5. AB-xdev commented on Oct 22, 2025

    @AB-xdev
    Author

    I currently fixed this in the following way (at least no problems so far... however I only implemented it a few hours ago):

    1. SaxonXPathRuleQuery#initialize: Globally caches the Configuration and namespacesToDeclare when the same XPathHandler is used
    2. RuleSets
      • initializeRules checks if an XPathRule was already initialized and skips the initialization when the current xPathHandler is identical to the one used previously
      • The deep copy constructor no longer deeply copies Rules - so that their initialization state is not lost

    I will try to post the code somewhere once this was finally implemented and tested by us internally.

  6. changed the title [-]Performance: Lot's of time spent doing XML initialization[/-] [+][core] Performance: Lots of time spent doing XML initialization[/+] on May 16, 2026
  7. zbynek commented on May 16, 2026

    @zbynek
    Contributor

    @AB-xdev

    I will try to post the code somewhere once this was finally implemented and tested by us internally.

    Did you manage to implement and test your solution? A pull request would be helpful

  8. adangel commented on May 19, 2026

    @adangel
    Member

    @zbynek
    See the classes here: https://github.com/xdev-software/intellij-plugin-pmd/tree/develop/src/main/java/net/sourceforge/pmd

    I have it on my personal TODO list to check these out and if it makes sense to integrate it in pmd-core...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions