From 35c58e987726e31192f466f46f1ac6b0d6d54227 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 29 Oct 2020 16:41:20 +0000 Subject: [PATCH] HHH-14305 Memory optimisations for AbstractManagedType#declaredPluralAttributes --- .../domain/internal/AbstractManagedType.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java index 29acda0036..7eccc87c3e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java @@ -7,6 +7,7 @@ package org.hibernate.metamodel.model.domain.internal; import java.io.Serializable; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -50,7 +51,7 @@ public abstract class AbstractManagedType private final Map> declaredAttributes = new HashMap<>(); private final Map> declaredSingularAttributes = new HashMap<>(); - private final Map> declaredPluralAttributes = new HashMap<>(); + private volatile Map> declaredPluralAttributes; private transient InFlightAccess inFlightAccess; @@ -258,7 +259,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings({ "unchecked" }) public Set> getPluralAttributes() { - HashSet attributes = new HashSet>( declaredPluralAttributes.values() ); + HashSet attributes = declaredAttributes == null ? new HashSet>() : new HashSet>( declaredPluralAttributes.values() ); if ( getSuperType() != null ) { attributes.addAll( getSuperType().getPluralAttributes() ); } @@ -267,7 +268,8 @@ public abstract class AbstractManagedType @Override public Set> getDeclaredPluralAttributes() { - return new HashSet>( declaredPluralAttributes.values() ); + return declaredPluralAttributes == null ? + Collections.EMPTY_SET : new HashSet>( declaredPluralAttributes.values() ); } @Override @@ -284,7 +286,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public PluralPersistentAttribute getPluralAttribute(String name) { - return declaredPluralAttributes.get( name ); + return declaredPluralAttributes == null ? null : declaredPluralAttributes.get( name ); } private void basicCollectionCheck(PluralAttribute attribute, String name) { @@ -297,7 +299,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings( "unchecked") public CollectionAttribute getDeclaredCollection(String name) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); basicCollectionCheck( attribute, name ); return ( CollectionAttribute ) attribute; } @@ -323,7 +325,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings( "unchecked") public SetPersistentAttribute getDeclaredSet(String name) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); basicSetCheck( attribute, name ); return (SetPersistentAttribute) attribute; } @@ -349,7 +351,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public ListPersistentAttribute getDeclaredList(String name) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); basicListCheck( attribute, name ); return (ListPersistentAttribute) attribute; } @@ -375,7 +377,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public MapPersistentAttribute getDeclaredMap(String name) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); basicMapCheck( attribute, name ); return (MapPersistentAttribute) attribute; } @@ -383,7 +385,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings({ "unchecked" }) public BagPersistentAttribute getCollection(String name, Class elementType) { - PluralAttribute attribute = declaredPluralAttributes.get( name ); + PluralAttribute attribute = getPluralAttribute( name ); if ( attribute == null && getSuperType() != null ) { attribute = getSuperType().getPluralAttribute( name ); } @@ -394,7 +396,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public CollectionAttribute getDeclaredCollection(String name, Class elementType) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); checkCollectionElementType( attribute, name, elementType ); return (CollectionAttribute) attribute; } @@ -423,7 +425,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings({ "unchecked" }) public SetAttribute getSet(String name, Class elementType) { - PluralAttribute attribute = declaredPluralAttributes.get( name ); + PluralAttribute attribute = getPluralAttribute( name ); if ( attribute == null && getSuperType() != null ) { attribute = getSuperType().getPluralAttribute( name ); } @@ -438,7 +440,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public SetAttribute getDeclaredSet(String name, Class elementType) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); checkSetElementType( attribute, name, elementType ); return ( SetAttribute ) attribute; } @@ -446,7 +448,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings({ "unchecked" }) public ListAttribute getList(String name, Class elementType) { - PluralAttribute attribute = declaredPluralAttributes.get( name ); + PluralAttribute attribute = getPluralAttribute( name ); if ( attribute == null && getSuperType() != null ) { attribute = getSuperType().getPluralAttribute( name ); } @@ -461,7 +463,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public ListAttribute getDeclaredList(String name, Class elementType) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); checkListElementType( attribute, name, elementType ); return ( ListAttribute ) attribute; } @@ -492,7 +494,7 @@ public abstract class AbstractManagedType @Override @SuppressWarnings("unchecked") public MapAttribute getDeclaredMap(String name, Class keyType, Class valueType) { - final PluralAttribute attribute = declaredPluralAttributes.get( name ); + final PluralPersistentAttribute attribute = getPluralAttribute( name ); checkMapValueType( attribute, name, valueType ); final MapAttribute mapAttribute = ( MapAttribute ) attribute; checkMapKeyType( mapAttribute, name, keyType ); @@ -530,7 +532,10 @@ public abstract class AbstractManagedType break; } case PLURAL_ATTRIBUTE : { - declaredPluralAttributes.put(attribute.getName(), (PluralPersistentAttribute) attribute ); + if ( AbstractManagedType.this.declaredPluralAttributes == null ) { + AbstractManagedType.this.declaredPluralAttributes = new HashMap<>(); + } + AbstractManagedType.this.declaredPluralAttributes.put( attribute.getName(), (PluralPersistentAttribute) attribute ); break; } default : {