Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:57:44+00:00 2026-05-23T03:57:44+00:00

NOTE: If you experience this issue as well, please upvote it on Apache JIRA:

  • 0

NOTE: If you experience this issue as well, please upvote it on Apache JIRA:

https://issues.apache.org/jira/browse/XALANJ-2540

I have come to an astonishing conclusion that this:

Element e = (Element) document.getElementsByTagName("SomeElementName").item(0);
String result = ((Element) e).getTextContent();

Seems to be an incredible 100x faster than this:

// Accounts for 30%, can be cached
XPathFactory factory = XPathFactory.newInstance();

// Negligible
XPath xpath = factory.newXPath();

// Negligible
XPathExpression expression = xpath.compile("//SomeElementName");

// Accounts for 70%
String result = (String) expression.evaluate(document, XPathConstants.STRING);

I’m using the JVM’s default implementation of JAXP:

org.apache.xpath.jaxp.XPathFactoryImpl
org.apache.xpath.jaxp.XPathImpl

I’m really confused, because it’s easy to see how JAXP could optimise the above XPath query to actually execute a simple getElementsByTagName() instead. But it doesn’t seem to do that. This problem is limited to around 5-6 frequently used XPath calls, that are abstracted and hidden by an API. Those queries involve simple paths (e.g. /a/b/c, no variables, conditions) against an always available DOM Document only. So, if an optimisation can be done, it will be quite easy to achieve.

My question: Is XPath’s slowness an accepted fact, or am I overlooking something? Is there a better (faster) implementation? Or should I just avoid XPath altogether, for simple queries?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:57:45+00:00Added an answer on May 23, 2026 at 3:57 am

    I have debugged and profiled my test-case and Xalan/JAXP in general. I managed to identify the big major problem in

    org.apache.xml.dtm.ObjectFactory.lookUpFactoryClassName()
    

    It can be seen that every one of the 10k test XPath evaluations led to the classloader trying to lookup the DTMManager instance in some sort of default configuration. This configuration is not loaded into memory but accessed every time. Furthermore, this access seems to be protected by a lock on the ObjectFactory.class itself. When the access fails (by default), then the configuration is loaded from the xalan.jar file’s

    META-INF/service/org.apache.xml.dtm.DTMManager
    

    configuration file. Every time!:

    JProfiler profiling results

    Fortunately, this behaviour can be overridden by specifying a JVM parameter like this:

    -Dorg.apache.xml.dtm.DTMManager=
      org.apache.xml.dtm.ref.DTMManagerDefault
    

    or

    -Dcom.sun.org.apache.xml.internal.dtm.DTMManager=
      com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault
    

    The above works, as this will allow to bypass the expensive work in lookUpFactoryClassName() if the factory class name is the default anyway:

    // Code from com.sun.org.apache.xml.internal.dtm.ObjectFactory
    static String lookUpFactoryClassName(String factoryId,
                                         String propertiesFilename,
                                         String fallbackClassName) {
      SecuritySupport ss = SecuritySupport.getInstance();
    
      try {
        String systemProp = ss.getSystemProperty(factoryId);
        if (systemProp != null) { 
    
          // Return early from the method
          return systemProp;
        }
      } catch (SecurityException se) {
      }
    
      // [...] "Heavy" operations later
    

    So here’s a performance improvement overview for 10k consecutive XPath evaluations of //SomeNodeName against a 90k XML file (measured with System.nanoTime():

    measured library        : Xalan 2.7.0 | Xalan 2.7.1 | Saxon-HE 9.3 | jaxen 1.1.3
    --------------------------------------------------------------------------------
    without optimisation    :     10400ms |      4717ms |              |     25500ms
    reusing XPathFactory    :      5995ms |      2829ms |              |
    reusing XPath           :      5900ms |      2890ms |              |
    reusing XPathExpression :      5800ms |      2915ms |      16000ms |     25000ms
    adding the JVM param    :      1163ms |       761ms |        n/a   |
    

    note that the benchmark was a very primitive one. it may well be that your own benchmark will show that saxon outperforms xalan

    I have filed this as a bug to the Xalan guys at Apache:

    https://issues.apache.org/jira/browse/XALANJ-2540

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note This is not a REBOL-specific question. You can answer it in any language.
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
NOTE: I am not set on using VI, it is just the first thing
Note that I am not asking which to choose (MVC or MVP), but rather
Note The question below was asked in 2008 about some code from 2003. As
NOTE : I mention the next couple of paragraphs as background. If you just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.