Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JAXB go faster

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?

like image 388
More Than Five Avatar asked Dec 24 '11 18:12

More Than Five


1 Answers

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:

  • http://woodstox.codehaus.org/Performance
like image 115
bdoughan Avatar answered Sep 22 '22 00:09

bdoughan