#603 DOMSender.walkNode bottleneck

v8.8
closed
nobody
5
2012-10-08
2006-10-05
John Baker
No

Hello,

For years, I've wondered why the NodeList.getLength()
method is so slow. While profiling some code to
transform a DOM, I came across this bottleneck in
DOMSender:

 for (int i=0; nit.getLength(); i++) {

The larger the NodeList (nit), the longer getLength()
will take to execute. Hence, I rewrote a few lines:

private void walkNode (Node node) throws

XPathException {
if (node.hasChildNodes()) {
NodeList nit = node.getChildNodes();
int len = nit.getLength();
Node child;
NamedNodeMap atts;
Element element;
for (int i=0; i<len; i++) {

I pulled out a few variable declarations too. With this
minor change, the method now runs over three times
faster than it did. Consider my test results below -
these two log lines are before and after the DOM has
been transformed into a String:

Without patch:

Oct 5, 2006 3:13:19 PM
com.eaf.xml.gateway.queries.processors.SQLResultsProcessor
execute
FINE: Building complete, result count=15412
Oct 5, 2006 3:14:26 PM
com.eaf.webapp.servlet.xmlgateway.XMLGateway handleRequest

With patch:

Oct 5, 2006 3:09:27 PM
com.eaf.xml.gateway.queries.processors.SQLResultsProcessor
execute
FINE: Building complete, result count=15412
Oct 5, 2006 3:09:53 PM
com.eaf.webapp.servlet.xmlgateway.XMLGateway handleRequest

And impressive improvement, I'm sure you'll all agree?

Can we get this into the codebase as soon as possible?
Also, I'd recommend a review of all code to see where
else getLength is being used in such a fashion.

Back to the profiler...

Thanks!

John

http://www.javasystemsolutions.com

Discussion

  • John Baker

    John Baker - 2006-10-05

    Logged In: YES
    user_id=1613906

    What I forgot to add is, the figure of 15412 is the number
    of nodes I've placed under one parent.

    John

     
  • Michael Kay

    Michael Kay - 2006-10-06

    Logged In: YES
    user_id=251681

    Thanks for the measurements, I will make this change. There
    are a few other places where NodeList.getLength() is called
    in a loop. It's certainly a bit of a surprise that this
    method is slow, but the DOM is full of surprises.

    Michael Kay

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks