Search code examples
javaperformancejaxbstaxwoodstox

JaxB performance enhancement


The project i'm adopting is using JaxB API to unmarshal XML to Java objects. We're passing a ByteArrayInputStream to Unmarshaller like:

ByteArrayInputStream bais = new BytearrayInputStream(byte[]...)
unmarshaller.unmarshal(bais)

Now i would like to find ways to optimise the speed of this process given that the byte array is really small (Default JaxB takes about 1-5ms) but there's a tons of them. I've tried to pass to the Unmarshaller different input like StAX, StAX-Woodstox parsers and StreamSource object for comparison.

unmarshall(..XMLInputFactory -> XMLStreamReader(bais)..)
unmarshall(..XMLInputFactory2 ->XMLStreamReader(bais)..)
unmarshall(..StreamSource(bais)..)

Out of roughly 5000 tries, StAX-Woodstox parser comes out to have been performing better than the others.

Here are 2 questions that I want to ask:

  • Given these pieces of information, could you think of a better way to optimise this in terms of speed ?
  • What could be the reason why passing StAX-Woodstox parser has given a better speed ?

Solution

  • It is quite likely that in other cases, unnecessary initialization is done (like constructing factories; or using very slow service introspection). I don't think there are more efficient alternatives than constructing stream reader yourself, although Woodstox at least allows you to pass byte[] directly without ByteArrayInputStream. That won't make huge difference most likely, but there's no benefit from wrapping.

    Aalto XML is likely even faster: https://github.com/FasterXML/aalto-xml -- for what that's worth.