Using Java 2.11 version. I'm building a xml binding component based on the CD Catalog data binding example. I have a fairly complex but rather small doc 2000 bytes about. And AutoPilot.bind() seems quite slow...
int count = 10000;
long start = System.nanoTime();
for(int i = 0; i <= count; i++)
{
//Init VTDGen, VTGNav here...
AutoPilot ap0 = new Autopilot();
AutoPilot ap1 = new Autopilot();
AutoPilot ap2 = new Autopilot();
AutoPilot ap3 = new Autopilot();
AutoPilot ap4 = new Autopilot();
// Set the XPAth for auto pilots here...
// Read bytes and parse...
// Bind autopilot to NAV.
MyObj mine = new MyObj();
// Do data binding here to My Object here...
}
long end = System.nanoTime();
long avgTime = (end - start) / count;
Hardware = 3GH 8 Core intel
Avg parse time is about 80000 nanoseconds.
Now if you Move the creation of Autopilot out of the loop, the average parse time is 10000 nano seconds.
Of course here this works because we are parsing the same document over and over. Now picture a server scenario like a servlet where each request parses what ever the received XML document is. It would be nice if we could reuse the auto pilot instead of creating a new one.
I remember reading somewhere to create a pool of autopilots but that's no fun especially where you have over a dozen autopilots.
I think what you may want to do is to create, per thread, some autoPilot objects containing XPath expressions. Suppose you have to create ten threads all processing XPath /a/b/c, then you just instantiate 10 threads, each compiling the same expression, thus avoiding the sharing issue.
Also notice that don't compile XPath expression in any kind of XPath evaluation while loop because it will slow you down. Take it out of the loop whenever possible.
Managing a pool of thread might make sense, because you can reuse AutoPilot objects, and avoid repetitive compile of XPath expressions. Having AutoPilot threadsafe may not be a good idea because it will cause your thread to stall, waiting the release of lock by another thread.