3

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.

user432024
  • 4,392
  • 8
  • 49
  • 85
  • of course you should reuse the autoPilot, creating autoPilot in the loop is going to be slow, it has little to do with parsing, you can attach different VTDNav objects to the same AutoPilot object, so what is teh problem? – vtd-xml-author May 26 '13 at 03:17
  • But can I create the Autopilots as global variables and have multiple threads give them different VTDNav? Sorry for double post. I had internet issue and wasn't sure my other question got posted. – user432024 May 26 '13 at 16:32
  • I'm looking at the code for VTD could we not create an Expr factory. Which creates Expr objects and we could createthose globallyand pass the Exp global object to local AutoPilots? – user432024 May 26 '13 at 17:10
  • Think of AutoPilot as a wraper around a compiled XPath, they are inseparable... – vtd-xml-author May 26 '13 at 20:33

1 Answers1

1

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.

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • I'm using a http servlet. That servlet can process a couple hundred requests a second and even more. So 1 I don't have an exact thread count. The servlet container manages that. The servlet parses the same document but of course each request has it's own values. I guess I have no choice but to manage a pool of Autopilots. I was just hoping I wouldn't have to :) – user432024 May 27 '13 at 13:41
  • 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. – vtd-xml-author May 27 '13 at 20:06
  • This should probably be the answer for servlet type environments. If you make it the answer then I can mark it as answered :) – user432024 May 28 '13 at 05:56