[jdom-interest] Optimizing tree construction

phil at triloggroup.com phil at triloggroup.com
Thu Mar 28 12:42:00 PST 2002


I added some methods to my own JDOM build, in order to optimize the tree construction when I don't need validation. In
general, this happens when you read a document from a database or when you programmatically create a JDOM. For example,
in our product we construct a JDOM from an HTML file, and then generate Java code that will rebuild the tree on demand,
without parsing anything. In that case, I'm sure that the JDOM is valid and, as it is executed on a server, I want that
process to be as fast as possible.

The best way I found is to make this available as "protected" members. So, it is available only via inheritance, for
well known subclasses of the base one.  This ensure that a user, who do not know the actual class, cannot call these
methods.
Then, in my application I created a set of "Fast" classes, that leverage these new methods and provide "Fast"
constructors (without checking the names validity).

When building from a modified SAXParser, and because of the parser overhead, the gain is about 15-20%, which is not
negligible at least. When building the tree from a java program, using new() and fastAdd() statements, the gain can be
up to 50%!

Brad, Jason, Alex.... what do you think about that? Can it be part of the standard JDOM?

Phil.

Here is the code I added:

=> Element class:
    protected void fastAddAttribute(Attribute o) {
        attributes.fastAddAttribute(o);
    }
    protected void fastAddContent(Element o) {
        content.fastAddContent(o);
    }
    protected void fastAddContent(Comment o) {
        content.fastAddContent(o);
    }
    protected void fastAddContent(ProcessingInstruction o) {
        content.fastAddContent(o);
    }
    protected void fastAddContent(CDATA o) {
        content.fastAddContent(o);
    }
    protected void fastAddContent(Text o) {
        content.fastAddContent(o);
    }
    protected void fastAddContent(EntityRef o) {
        content.fastAddContent(o);
    }

=> ContentList class:
    // PHIL
    protected final void fastAddContent(Element o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }
    protected final void fastAddContent(Comment o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }
    protected final void fastAddContent(ProcessingInstruction o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }
    protected final void fastAddContent(CDATA o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }
    protected final void fastAddContent(Text o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }
    protected final void fastAddContent(EntityRef o) {
        if( list==null ) {
            list = new ArrayList(INITIAL_ARRAY_SIZE);
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }


=>AttributeList class:
    final void fastAddAttribute(Attribute o) {
        if( list==null ) {
            list = new ArrayList();
        }
        o.parent = parent;
        list.add(o);
        modCount++;
    }




More information about the jdom-interest mailing list