[jdom-interest] Presences of Namespace dramatically slows SAXBuilder

Joseph Bowbeer jozart at csi.com
Tue Apr 3 16:11:13 PDT 2001

Here's a micro-optimization that might make a difference:

I notice that SAXBuilder.startElement always calls transferNamespaces.  I'm
not sure how much work transferNamespaces typically has to do, but it can be
written much more efficiently if we added a protected "bulk" namespace
method to Element.

Here's the old version:

    private void transferNamespaces(Element element) {
        Iterator i = declaredNamespaces.iterator();
        while (i.hasNext()) {
            Namespace ns = (Namespace)i.next();

Here's a new version that uses addAll and clear, which saves creating an
Iterator and doing individual adds and removes:

    private void transferNamespaces(Element element) {
        if (!declaredNamespaces.isEmpty()) {
            availableNamespaces.addAll(0, declaredNamespaces);

The code above requires that we add a new protected method to Element:

    protected void addNamespaceDeclarations(Collection namespaces) {
        if (additionalNamespaces == null) {
            additionalNamespaces = new LinkedList();

Misc. Notes:

1. Replace LinkedList with ArrayList?  LinkedList is rarely more efficient
than ArrayList, especially when bulk-mode operations can be used.

2. Is "!isEmpty()" more readable than "size() > 0"?

3. Element.getAdditionalNamespaces is a little strange.  If there are no
additional namespaces, it returns Collections.EMPTY_LIST, which is static
and immutable.  However, if there are additional namespaces, it returns the
actual list, which is mutable.

Joe Bowbeer

----- original message -----
From: Jason Hunter jhunter at acm.org
Date: Tue, 03 Apr 2001 12:52:08 -0700

On the SAX side: Priming alone didn't bring the SAX build in line, so I
did a little OptimizeIt testing and found that one line was taking the
vast bulk of time:


I changed it to:

  List addl = element.getAdditionalNamespaces();
  if (addl.size() > 0) {

And now what did take 13219ms now takes 580ms (on Xerces).  Quite a
change, and right in line with the no-NS build that takes 470ms.  I'll
be checking in the enhancement after this.

More information about the jdom-interest mailing list