[hibernate-issues] [Hibernate-JIRA] Created: (HV-384) Performence problem when using programatic constrain declaration

Jan Cuzy (JIRA) noreply at atlassian.com
Mon Oct 11 12:51:57 EDT 2010


Performence problem when using programatic constrain declaration
----------------------------------------------------------------

                 Key: HV-384
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-384
             Project: Hibernate Validator
          Issue Type: Bug
    Affects Versions: 4.1.0.Final
         Environment: Java 1.6, OSGi Equinox
            Reporter: Jan Cuzy
            Assignee: Hardy Ferentschik


I'm using the programmatic API to declare the constrains on class. 

{noformat} ConstraintMapping constraintMapping = ...
HibernateValidatorConfiguration config = ...
config.addMapping(constraintMapping);
ValidatorFactory factory = config.buildValidatorFactory();{noformat} 

Right now I have 73 with 745 configured constrains on them (This is rather beginning of the project, in the future numbers will be higher). It takes approx. 6 seconds to create the validator factory. I was investigating a bit and found a method initProgrammaticConfiguration in ValidatorFactoryImpl class, which calls vary often the method getConstraintConfig() on the ConstraintMapping class. 
This method creates always new 
{noformat}<A extends Annotation> Map<Class<?>, List<ConstraintDefWrapper<?>>>{noformat} 
based on values in its member variable 
{noformat} private final Map<Class<?>, List<ConstraintDef<?>>> constraintConfig{noformat} 

Is there any problem to cache the created map as a meber in the ConstraintMapping class? 
I've done 
{noformat} 
public class ConstraintMapping {
    private final Map<Class<?>, List<ConstraintDef<?>>> constraintConfig;
    private Map<Class<?>, List<ConstraintDefWrapper<?>>> newDefinitions;
...
{noformat} 

then 


{noformat}     public final <A extends Annotation> Map<Class<?>, List<ConstraintDefWrapper<?>>> getConstraintConfig() {
        *if (newDefinitions == null) {*
            *newDefinitions = new HashMap<Class<?>, List<ConstraintDefWrapper<?>>>();*
            for (Map.Entry<Class<?>, List<ConstraintDef<?>>> entry : constraintConfig.entrySet()) {

                List<ConstraintDefWrapper<?>> newList = new ArrayList<ConstraintDefWrapper<?>>();
                for (ConstraintDef<?> definition : entry.getValue()) {
                    Class<?> beanClass = definition.beanType;
                    @SuppressWarnings("unchecked")
                    ConstraintDefWrapper<A> defAccessor = new ConstraintDefWrapper<A>(beanClass,
                        (Class<A>) definition.constraintType, definition.property, definition.elementType,
                        definition.parameters, this);
                    newList.add(defAccessor);
                }
                newDefinitions.put(entry.getKey(), newList);
            }
        *}*
        return newDefinitions;
    }{noformat}


and then maybe also 
{noformat}   protected final void addConstraintConfig(ConstraintDef<?> definition) {
        Class<?> beanClass = definition.beanType;
        configuredClasses.add(beanClass);
        if (constraintConfig.containsKey(beanClass)) {
            constraintConfig.get(beanClass).add(definition);
        }
        else {
            List<ConstraintDef<?>> definitionList = new ArrayList<ConstraintDef<?>>();
            definitionList.add(definition);
            constraintConfig.put(beanClass, definitionList);
            *newDefinitions = null;*
        }
    }
{noformat}

and the time which is needed to create the factory is 200ms.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list