Skip to content

Re-use SAXParserFactory in AbstractUnmarshallerImpl for better performance #284

@winne42

Description

@winne42

The current design of AbstractUnmarshallerImpl has a serious negative performance impact when unmarshalling many XML files, which could easily be avoided.

Most JAXB documentation found anywhere including the JAXB Users Guide recommend creating a single JAXBContext and then new Unmarshallers for every unmarshalling tasks (the Users Guide recommends using object pooling for best performance, but many JAXB users will never have read that remark; also, this approach is more complex).

My issue with using JAXB in this "standard way" is that AbstractUnmarshallerImpl.getXMLReader() creates a new SAXParserFactory for every new Unmarshaller:

  • This is potentially quite an expensive operation, because the JAXP Lookup Mechanism uses the notoriously slow ServiceLoader mechanism. (this can be avoided by using system environment variables or jaxp.properties, but I highly doubt that this is a well-known fact)
  • It also seems unintuitive that a new factory with the same parameters is built over-and-over-again (e.g., we don't build a new car factory for every car we build, do we?)

My suggestion would be to make the well-documented default use-case of unmarshalling faster by creating a singleton SAXParserFactory in AbstractUnmarshallerImpl and re-using it in getXMLReader().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions