Juan Hernandez has uploaded a new change for review. Change subject: restapi: Faster JAXB element creation ......................................................................
restapi: Faster JAXB element creation Currently when we need to create a JAXB element we always iterate the object factory looking for the appropiate method. To improve the performance of this lookup this patch introduces an index that is created during startup so that the iteration is done only once. Change-Id: Ie6f2cc91701ff45a443d3b8b37e8225d7b26c4fa Signed-off-by: Juan Hernandez <[email protected]> --- M backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/xml/JAXBProvider.java 1 file changed, 19 insertions(+), 13 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/90/30090/1 diff --git a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/xml/JAXBProvider.java b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/xml/JAXBProvider.java index 376c47a..89c05f5 100644 --- a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/xml/JAXBProvider.java +++ b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/xml/JAXBProvider.java @@ -23,6 +23,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; @@ -63,7 +65,12 @@ /** * The factory used to create JAXB elements. */ - private ObjectFactory objectFactory; + private ObjectFactory objectFactory = new ObjectFactory(); + + /** + * A index used to speed up finding the factory method used to create JAXB elements. + */ + private Map<Class<?>, Method> factoryMethods = new HashMap<>(); /** * The factory used to create XML document readers. @@ -81,8 +88,15 @@ private ValidationEventHandler jaxbHandler = new JAXBValidationEventHandler(); public JAXBProvider() { - // Create the object factory: - objectFactory = new ObjectFactory(); + // In order to create the JAXB element that wraps the object we need to call the method of the object factory + // that uses the correct element name, and in order to avoid doing this with every request we populate this + // map in advance: + for (Method factoryMethod : ObjectFactory.class.getDeclaredMethods()) { + Class<?>[] parameterTypes = factoryMethod.getParameterTypes(); + if (parameterTypes.length == 1) { + factoryMethods.put(parameterTypes[0], factoryMethod); + } + } // Create a factory that will produce XML parsers that ignore entity references and DTDs: parserFactory = XMLInputFactory.newFactory(); @@ -182,16 +196,8 @@ public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { - // In order to create the JAXB element that wraps the object we need to iterate the object factory and find the - // method that creates it: - Method factoryMethod = null; - for (Method currentMethod : ObjectFactory.class.getDeclaredMethods()) { - Class<?>[] parameterTypes = currentMethod.getParameterTypes(); - if (parameterTypes.length == 1 && parameterTypes[0] == type) { - factoryMethod = currentMethod; - break; - } - } + // Find the factory method used to create the JAXB element with the right tag: + Method factoryMethod = factoryMethods.get(type); if (factoryMethod == null) { throw new IOException("Can't find factory method for type \"" + type.getName() + "\"."); } -- To view, visit http://gerrit.ovirt.org/30090 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie6f2cc91701ff45a443d3b8b37e8225d7b26c4fa Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
