Fast filter support in SAX2
Tyler Baker
tyler at infinet.com
Mon Mar 29 17:28:51 BST 1999
David Megginson wrote:
> Bill la Forge writes:
>
> > It would be great if filters had the same advantages as parsers in
> > being able to simply test for equality (x==y) rather than having to
> > do a string comparison (x.equals(y)) when checking for a specific
> > element or attribute name.
>
> Yes, but as someone (James Clark?) pointed out during the last round,
> with most serious applications you're going to end up doing hash
> lookups anyway, so the == doesn't buy you much.
That depends on your implementation of a hash table. Also as of JDK 1.1.6 the equals method
for strings first tests for identity of the two string objects and then tests to see if the
length is the same and then tests for matching of each character in each string. When dealing
with names in XML they are uniformly nothing more than symbols so in application code being
able to do something like this:
if (x == "foo")
is generally much faster than:
if (x.equals("foo"))
as you do not incur the overhead of calling one dynamic method. Really it depends on your
code. In an XML related technology I worked on I had lots of if-else statements that did
exactly this. The parser I used presented the strings to the application as interned strings
and did significantly improve performance from using the equals method approach.
Another thing that I used for speeding up my applications is to have a special hash table for
interned strings. Basically all that this table did was use System.identityHashcode() instead
of String.hashcode() to get a hash for the string. In effect you use the Object.hashCode()
implementation.
It also depends a lot on your VM. Some VM's are good enough with dynamic method invocation
that the difference between testing for string identity and string equality is neglibible.
The so-called Hotspot VM may even inline String.equals() into your code.
I suggest using the identity approach if possible as it is easier to read and maintain IMHO
and in the general case you may get significant speedups if your application does many string
comparisons. If you need a faster hash table for strings build one yourself.
Tyler
xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)
More information about the Xml-dev
mailing list