Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Element.getValue() optimization

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Wolfgang Hoschek <whoschek AT lbl.gov>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] Element.getValue() optimization
  • Date: Sun, 22 May 2005 22:45:35 -0700

Here is a small optimization to Element.getValue(), with a fast path for cases where an element has one and only one Text child:

public final String getValue() {

// non-recursive algorithm avoids stack size limitations
if (this.getChildCount() == 0) return "";
Node current = this.getChild(0);
if (this.getChildCount() == 1 && current.isText()) return current.getValue(); // WH fast path
StringBuffer result = new StringBuffer(); // WH
int index = 0;
...
}

I've seen an app where this resulted in a speedup factor 50 (!) with the stringified Text version (not just because StringBuffer copying is avoided, but mainly because using the returned String in a HashMap reduces equality comparisons to == and String.hashCode() to O(1) because the hashcode is internally cached by String).

Wolfgang.




Archive powered by MHonArc 2.6.24.

Top of Page