12

I have a 8 Meg file. Marshalling using JAXB takes 1082ms, using DOM takes 862ms, using SAX takes 438ms. This is using all defaults with JDK 1.6, no extra configuration such as using woodstox is used.

In an effort, to get better performance from JAXB, I try to make it use SAX parsing by doing...

FileReader fr = new FileReader("myfile.xml");
JAXBContext jc = JAXBContext.newInstance(MyObjectList.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();

XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLEventReader xmler = xmlif.createXMLEventReader(fr);

long beginTime = System.currentTimeMillis();
MyObjectList obj = (MyObjectList)unmarshaller.unmarshal(xmler);
long endTime = System.currentTimeMillis();

This makes it go even slower - 3207ms.

My questions are: 1. How do I make JAXB go faster? 2. How can I be 100% sure what underlying parsing mechanism it is using?

More Than Five
  • 9,959
  • 21
  • 77
  • 127

2 Answers2

16

1 - How do I make JAXB go faster?

You are on the right track with unmarshalling from a StAX input, but I would recommend a XMLStreamReader instead of a XMLEventReader.

XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLStreamReader xmler = xmlif.createXMLStreamReader(fr);

Since StAX is a standard you can switch in another implementation such as WoodStox as the underlying parser.

2 - How can I be 100% sure what underlying parsing mechanism it is using?

Just like you are doing. If you pass a JAXB implementation an instance of XMLStreamReader then you can be reasonably sure that it is being used. If on the other hand you unmarshalled from something like an InputStream then the JAXB implementation is free to use whatever parsing technique it wants to. If you go with Woodstox be sure to check out there performance page as well:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Thanks. Ah so I was using StAX instead of SAX. You reckon that's the fastest way to use JAXB? – More Than Five Dec 24 '11 at 20:25
  • 1
    No, StAX offers two types of readers `XMLStreamReader` and `XMLEventReader`. The `XMLStreamReader` option is always the faster one. – bdoughan Dec 24 '11 at 20:29
  • Yeah I get that but I was never using SAX in my original example. The way you suggest comes in at about 1800ms (without woodstox). Still much slower than using normal JAXB. Why is this? – More Than Five Dec 24 '11 at 20:31
  • I'm not sure where SAX came into the discussion :). There may be a perf issue the version of the StAX parser included in the JRE/JDK you are using. When you "normal JAXB" do you mean unmarshalling from a `FileReader`? What perf numbers do you get with Woodstox? With the SAX or DOM numbers are you processing any of the data or just doing a straight parse? – bdoughan Dec 24 '11 at 20:40
  • By normal JAXB I mean: unmarshaller.unmarshal(new File("servers.xml")); Times are better with JAXB. Down to 920. So that is the best result using JAXB / StreamReader. However it's still not as fast as DOM or SAX. I wonder why? Any ideas? Thanks a mill. – More Than Five Dec 24 '11 at 20:51
2

I haven't tried these, but EclipseLink provides a JAXB implementsion. http://www.eclipse.org/eclipselink/moxy.php Jibx is supposed to be fast, but I don't think its a JAXB implementation. Although it does the same thing. http://jibx.sourceforge.net/index.html

If EclipseLink is compliant, then you should be able to just drop it in and try it. Not sure about the effort to test Jibx.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • +1 for EclipseLink MOXy (I'm the tech lead). MOXy is compliant with the JAXB 2.1 & 2.2. For information on using it as your provider see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html. Be sure to check out our latest 2.3.2 release: http://www.eclipse.org/eclipselink/downloads/. JiBX is not a JAXB implementation. – bdoughan Dec 24 '11 at 19:16