I'm doing some research on the possibility of caching XPath objects in a
large, J2EE based application to reduce parse time of identical XPath
statements. I'm expecting the same set of XPath expressions to be executed
over and over in multiple threads.

I can't keep the XPath objects around forever but I would like to be able to
use the same XPath object multiple times if it hasn't been gc'd. My initial
thought is to require XPath object creation to go through a facade class
that would first check a WeakHashMap for an XPath object, and if it doesn't
exist, create the XPath object and add it to the Map for subsequent uses.

basically,

class XPathCache
{
    private static final Map cache = new WeakHashMap( extralarge );


    public static XPath getXPath( String expression )
    {
        XPath xp = (XPath)cache.get( expression );
        if( xp == null )
        {
           xp = new DOMXPath( expression );
           cache.put( expression, xp );
           // pretend the hash id for a String is
           // calculated based on its content and not
           // its address
        }
        return xp;
    }
}


This should reduce the number of XPath parses to 1 when the same expression
is used multiple times (by either the same piece of code, or even cooler,
different, unrelated pieces of code). The cost is the Hash lookup and
possible put each time a path runs. Still, I figure that two uses of the
same XPath is all that would be needed for this to be beneficial.

This raises some questions that I thought someone might already be
knowledgeable about. The XPath interface design looks completely solid for
concurrent execution on multiple threads. Has anyone had issues or successes
with this? e.g. If I create a single XPath object and kick off two exact
threads that call evaluate can I expect both to do their thing happily?

Anything that you guys can wax about on the topic of threads or caching w/
jaxen would be greatly appreciated. I should be getting into this with more
depth over the next week or so and plan on putting the API through a battery
of tests. And, of course, I'll submit anything contributable to the list.

Thanks,
- Ryan



-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing 
your web site with SSL, click here to get a FREE TRIAL of a Thawte 
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
Jaxen-interest mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jaxen-interest

Reply via email to