[hibernate-issues] [Hibernate-JIRA] Commented: (HV-384) Performence problem when using programatic constrain declaration
Gunnar Morling (JIRA)
noreply at atlassian.com
Fri Dec 10 18:11:15 EST 2010
[ http://opensource.atlassian.com/projects/hibernate/browse/HV-384?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39339#action_39339 ]
Gunnar Morling commented on HV-384:
-----------------------------------
I think this should easily be fixed by pulling the call to getConstraintConfig() out of the loops in ValidatorFactoryImpl#initProgrammaticConfiguration() and storing it in a local variable. That way getConstraintConfig() would only be called once.
Hardy, you can assign this to me if you like.
> 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
> Components: engine
> Affects Versions: 4.1.0.Final
> Environment: Java 1.6, OSGi Equinox
> Reporter: Jan Cuzy
> Assignee: Hardy Ferentschik
> Fix For: 4.2.0.Beta1
>
>
> 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