Skip to content

Commit

Permalink
HV-1566 Cached type variable index for performance purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and gsmet committed Feb 8, 2018
1 parent 90372a0 commit 4ba2676
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
Expand Up @@ -680,7 +680,7 @@ private void doValidate(Object value, String nodeName) {
ValueContext<?, Object> cascadedValueContext = buildNewLocalExecutionContext( valueContext, value );

if ( cascadingMetaData.getDeclaredContainerClass() != null ) {
cascadedValueContext.setTypeParameter( cascadingMetaData.getDeclaredContainerClass(), cascadingMetaData.getDeclaredTypeParameter() );
cascadedValueContext.setTypeParameter( cascadingMetaData.getDeclaredContainerClass(), cascadingMetaData.getDeclaredTypeParameterIndex() );
}

// Cascade validation
Expand All @@ -692,7 +692,7 @@ private void doValidate(Object value, String nodeName) {
if ( cascadingMetaData.hasContainerElementsMarkedForCascading() ) {
ValueContext<?, Object> cascadedTypeArgumentValueContext = buildNewLocalExecutionContext( valueContext, value );
if ( cascadingMetaData.getTypeParameter() != null ) {
cascadedValueContext.setTypeParameter( cascadingMetaData.getDeclaredContainerClass(), cascadingMetaData.getDeclaredTypeParameter() );
cascadedValueContext.setTypeParameter( cascadingMetaData.getDeclaredContainerClass(), cascadingMetaData.getDeclaredTypeParameterIndex() );
}

if ( nodeName != null ) {
Expand Down
Expand Up @@ -171,20 +171,19 @@ public final void markCurrentPropertyAsIterableAndSetIndex(Integer index) {
* Sets the container element information.
*
* @param containerClass the class of the container
* @param typeParameter the actual type parameter (i.e. not one of our internal type parameters)
* @param typeParameterIndex the index of the actual type parameter
*
* @see TypeVariables#getContainerClass(TypeVariable)
* @see TypeVariables#getActualTypeParameter(TypeVariable)
* @see AnnotatedObject
* @see ArrayElement
*/
public final void setTypeParameter(Class<?> containerClass, TypeVariable<?> typeParameter) {
public final void setTypeParameter(Class<?> containerClass, Integer typeParameterIndex) {
if ( containerClass == null ) {
return;
}

propertyPath.setLeafNodeTypeParameter( containerClass,
typeParameter != null ? TypeVariables.getTypeParameterIndex( typeParameter ) : null );
propertyPath.setLeafNodeTypeParameter( containerClass, typeParameterIndex );
}

public final void setCurrentGroup(Class<?> currentGroup) {
Expand Down
Expand Up @@ -56,6 +56,12 @@ public class ContainerCascadingMetaData implements CascadingMetaData {
*/
private final TypeVariable<?> declaredTypeParameter;

/**
* Index of the declared type parameter: it is the one used in the node of the property path.
* It is cached here for performance reasons.
*/
private final Integer declaredTypeParameterIndex;

/**
* Possibly the cascading type parameters corresponding to this type parameter if it is a parameterized type.
*/
Expand Down Expand Up @@ -112,6 +118,7 @@ private ContainerCascadingMetaData(ValueExtractorManager valueExtractorManager,
this.typeParameter = typeParameter;
this.declaredContainerClass = declaredContainerClass;
this.declaredTypeParameter = declaredTypeParameter;
this.declaredTypeParameterIndex = TypeVariables.getTypeParameterIndex( declaredTypeParameter );
this.containerElementTypesCascadingMetaData = containerElementTypesCascadingMetaData;
this.cascading = cascading;
this.groupConversionHelper = groupConversionHelper;
Expand All @@ -137,6 +144,7 @@ private ContainerCascadingMetaData(ValueExtractorManager valueExtractorManager,
this.typeParameter = AnnotatedObject.INSTANCE;
this.declaredContainerClass = null;
this.declaredTypeParameter = null;
this.declaredTypeParameterIndex = null;
this.containerElementTypesCascadingMetaData = containerElementTypesCascadingMetaData;
this.cascading = true;
this.groupConversionHelper = groupConversionHelper;
Expand All @@ -150,6 +158,7 @@ private ContainerCascadingMetaData(ValueExtractorManager valueExtractorManager,
this.typeParameter = typeParameter;
this.declaredContainerClass = declaredContainerClass;
this.declaredTypeParameter = declaredTypeParameter;
this.declaredTypeParameterIndex = TypeVariables.getTypeParameterIndex( declaredTypeParameter );
this.containerElementTypesCascadingMetaData = Collections.emptyList();
this.cascading = true;
this.groupConversionHelper = groupConversionHelper;
Expand Down Expand Up @@ -179,6 +188,10 @@ public TypeVariable<?> getDeclaredTypeParameter() {
return declaredTypeParameter;
}

public Integer getDeclaredTypeParameterIndex() {
return declaredTypeParameterIndex;
}

@Override
public boolean isCascading() {
return cascading;
Expand Down
Expand Up @@ -209,7 +209,7 @@ private void doValidate(Object value, String nodeName) {

Class<?> containerClass = currentValueExtractionPathNode.getContainerClass();
if ( containerClass != null ) {
valueContext.setTypeParameter( containerClass, currentValueExtractionPathNode.getTypeParameter() );
valueContext.setTypeParameter( containerClass, currentValueExtractionPathNode.getTypeParameterIndex() );
}

if ( nodeName != null ) {
Expand Down Expand Up @@ -245,18 +245,21 @@ static final class ContainerClassTypeParameterAndExtractor {

private final Class<?> containerClass;
private final TypeVariable<?> typeParameter;
private final Integer typeParameterIndex;
private final ValueExtractorDescriptor valueExtractorDescriptor;

ContainerClassTypeParameterAndExtractor(Class<?> containerClass, TypeVariable<?> typeParameter, ValueExtractorDescriptor valueExtractorDescriptor) {
ContainerClassTypeParameterAndExtractor(Class<?> containerClass, TypeVariable<?> typeParameter, Integer typeParameterIndex, ValueExtractorDescriptor valueExtractorDescriptor) {
this.containerClass = containerClass;
this.typeParameter = typeParameter;
this.typeParameterIndex = typeParameterIndex;
this.valueExtractorDescriptor = valueExtractorDescriptor;
}

@Override
public String toString() {
return "ContainerClassTypeParameterAndExtractor [containerClass=" + containerClass +
", typeParameter=" + typeParameter +
", typeParameterIndex=" + typeParameterIndex +
", valueExtractorDescriptor=" + valueExtractorDescriptor + "]";
}
}
Expand All @@ -267,18 +270,21 @@ private interface ValueExtractionPathNode {
ValueExtractionPathNode getNext();
Class<?> getContainerClass();
TypeVariable<?> getTypeParameter();
Integer getTypeParameterIndex();
ValueExtractorDescriptor getValueExtractorDescriptor();
}

private static final class SingleValueExtractionPathNode implements ValueExtractionPathNode {

private final Class<?> containerClass;
private final TypeVariable<?> typeParameter;
private final Integer typeParameterIndex;
private final ValueExtractorDescriptor valueExtractorDescriptor;

public SingleValueExtractionPathNode(ContainerClassTypeParameterAndExtractor typeParameterAndExtractor) {
this.containerClass = typeParameterAndExtractor.containerClass;
this.typeParameter = typeParameterAndExtractor.typeParameter;
this.typeParameterIndex = typeParameterAndExtractor.typeParameterIndex;
this.valueExtractorDescriptor = typeParameterAndExtractor.valueExtractorDescriptor;
}

Expand Down Expand Up @@ -307,6 +313,11 @@ public TypeVariable<?> getTypeParameter() {
return typeParameter;
}

@Override
public Integer getTypeParameterIndex() {
return typeParameterIndex;
}

@Override
public ValueExtractorDescriptor getValueExtractorDescriptor() {
return valueExtractorDescriptor;
Expand All @@ -326,12 +337,14 @@ private static final class LinkedValueExtractionPathNode implements ValueExtract
private final ValueExtractionPathNode next;
private final Class<?> containerClass;
private final TypeVariable<?> typeParameter;
private final Integer typeParameterIndex;
private final ValueExtractorDescriptor valueExtractorDescriptor;

private LinkedValueExtractionPathNode( ValueExtractionPathNode previous, List<ContainerClassTypeParameterAndExtractor> elements) {
ContainerClassTypeParameterAndExtractor first = elements.get( 0 );
this.containerClass = first.containerClass;
this.typeParameter = first.typeParameter;
this.typeParameterIndex = first.typeParameterIndex;
this.valueExtractorDescriptor = first.valueExtractorDescriptor;
this.previous = previous;

Expand Down Expand Up @@ -368,6 +381,11 @@ public TypeVariable<?> getTypeParameter() {
return typeParameter;
}

@Override
public Integer getTypeParameterIndex() {
return typeParameterIndex;
}

@Override
public ValueExtractorDescriptor getValueExtractorDescriptor() {
return valueExtractorDescriptor;
Expand Down
Expand Up @@ -120,14 +120,14 @@ private static <A extends Annotation> Type addValueExtractorDescriptorForWrapped
}

if ( selectedValueExtractorDescriptor.getExtractedType().isPresent() ) {
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, null, selectedValueExtractorDescriptor ) );
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, null, null, selectedValueExtractorDescriptor ) );
return selectedValueExtractorDescriptor.getExtractedType().get();
}
else {
Class<?> wrappedValueType = getWrappedValueType( typeResolutionHelper, location.getTypeForValidatorResolution(), selectedValueExtractorDescriptor );
TypeVariable<?> typeParameter = getContainerClassTypeParameter( declaredType, selectedValueExtractorDescriptor );

valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, typeParameter, selectedValueExtractorDescriptor ) );
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, typeParameter, TypeVariables.getTypeParameterIndex( typeParameter ), selectedValueExtractorDescriptor ) );

return wrappedValueType;
}
Expand All @@ -145,9 +145,11 @@ private static void addValueExtractorDescriptorForTypeArgumentLocation( ValueExt
throw LOG.getNoValueExtractorFoundForTypeException( declaredType, typeParameter );
}

TypeVariable<?> actualTypeParameter = TypeVariables.getActualTypeParameter( typeParameter );
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor(
TypeVariables.getContainerClass( typeParameter ),
TypeVariables.getActualTypeParameter( typeParameter ),
actualTypeParameter,
TypeVariables.getTypeParameterIndex( actualTypeParameter ),
valueExtractorDescriptor ) );
}

Expand Down
Expand Up @@ -70,7 +70,7 @@ public static String getTypeParameterName(Class<?> clazz, int typeParameterIndex
}

public static Integer getTypeParameterIndex(TypeVariable<?> typeParameter) {
if ( isArrayElement( typeParameter ) ) {
if ( typeParameter == null || isArrayElement( typeParameter ) ) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions performance/pom.xml
Expand Up @@ -27,7 +27,7 @@
-->
<profilingOptions>-showversion</profilingOptions>
<history.directory>${project.basedir}/history</history.directory>
<jmh.version>1.17.5</jmh.version>
<jmh.version>1.20</jmh.version>

<!-- default values, overridden per profile -->
<beanvalidation-impl.name>none</beanvalidation-impl.name>
Expand Down Expand Up @@ -202,7 +202,7 @@
</activation>
<properties>
<beanvalidation-impl.name>Hibernate Validator</beanvalidation-impl.name>
<beanvalidation-impl.version>6.0.5.Final</beanvalidation-impl.version>
<beanvalidation-impl.version>6.0.7.Final</beanvalidation-impl.version>
</properties>
<dependencies>
<dependency>
Expand Down

0 comments on commit 4ba2676

Please sign in to comment.