[XOM-interest] Attributes performance patch

Wolfgang Hoschek whoschek at lbl.gov
Sun Nov 21 20:06:39 EST 2004


When bnux deserializing I noticed Attributes.checkPrefixConflict and 
Attributes.get(String) as prominent hotspots.
This is with a WURFL document such as this one here: 
http://wurfl.sourceforge.net/wurfl.zip

The patch below eliminates some unnecessary iteration and uses indexes 
instead of iterators. Makes the hotspot completely go away. Otherwise, 
behaviour is unchanged.


     void checkPrefixConflict(Attribute attribute) {

         String prefix = attribute.getNamespacePrefix();
         String namespaceURI = attribute.getNamespaceURI();



         // Look for conflicts
//        Iterator iterator = attributes.iterator();
//        while (iterator.hasNext()) {
//            Attribute a = (Attribute) iterator.next();
         for (int i=attributes.size(); --i >= 0; ) {
         	   Attribute a = (Attribute) attributes.get(i);
             if (a.getNamespacePrefix().equals(prefix)) {
             	 if (a.getNamespaceURI().equals(namespaceURI)) return; // 
no need to look further
                // there can't be any more attrs with the same prefix 
but different URI.
                // the original code would continue to iterate over the 
remaining attributes!
               throw new NamespaceConflictException(
                         "Prefix of " + attribute.getQualifiedName()
                         + " conflicts with " + a.getQualifiedName());
             }
         }
     }


     Attribute get(String localName, String namespaceURI) {

//        Iterator iterator = attributes.iterator();
//        while (iterator.hasNext()) {
//        Attribute a = (Attribute) iterator.next();
         for (int i=attributes.size(); --i >= 0; ) {
      	   Attribute a = (Attribute) attributes.get(i);
             if (a.getLocalName().equals(localName)
              && a.getNamespaceURI().equals(namespaceURI)) {
                 return a;
             }
         }

         return null;

     }



More information about the XOM-interest mailing list