[jdom-interest] Fast Factory

phil at triloggroup.com phil at triloggroup.com
Fri May 23 02:37:49 PDT 2003


I'm happy to see that old debate re-opened :-)
Just for your knowlegde, we already did such optimizations in our code and it is now in production for more than one
year.

We also went a step further, by enhancing ContentList & AttributeList with some fast access methods, to make faster the
add of new elements/attributes when building the tree. This bypasses all the tests done when adding a new child.

First, we added some method to Attribute/ContentList. These methods are package protected, so they cannot be accessed
from outside JDOM itself.

    // AttributeList
    final void fastAddAttribute(Attribute o) {
        o.parent = parent;
        ensureCapacity(size + 1);
        elementData[size++] = o;
        modCount++;
    }

    // ContentList
    protected final void fastAddContent(Element o) {
        o.parent = parent;
        ensureCapacity(size + 1);
        elementData[size++] = o;
        modCount++;
    }
    ....

Then, we added some protected methods to Element. These methods are protected, so they can be accessed by inherited
classes, but do not change the public API.
    protected static final void __fastAddAttribute(Element e, Attribute o) {
        e.attributes.fastAddAttribute(o);
    }
    protected static final void __fastAddContent(Element e, Element o) {
        e.content.fastAddContent(o);
    }
    ...

Finally, we created our own Factory that uses an inherited Element class and that uses both non verified constructors
and fast list access. Not that fast list methods can be added to JDOMFactory.

According to OptimizeIt, this boost our performance from more than 25%!

This should be used carefully, but it is very effective in some particular cases. For example, our Framework always
stores valid XML documents and there is no reason for doing such a validation when these documents are read.

Phil.




More information about the jdom-interest mailing list