Skip to content

Commit

Permalink
HHH-14305 Memory optimisations for AbstractManagedType#declaredPlural…
Browse files Browse the repository at this point in the history
…Attributes
  • Loading branch information
Sanne committed Nov 1, 2020
1 parent b927de9 commit 13398a8
Showing 1 changed file with 21 additions and 16 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,7 @@ public abstract class AbstractManagedType<J>

private final Map<String, PersistentAttributeDescriptor<J, ?>> declaredAttributes = new HashMap<>();
private final Map<String, SingularPersistentAttribute<J, ?>> declaredSingularAttributes = new HashMap<>();
private final Map<String, PluralPersistentAttribute<J, ?, ?>> declaredPluralAttributes = new HashMap<>();
private volatile Map<String, PluralPersistentAttribute<J, ?, ?>> declaredPluralAttributes;

private transient InFlightAccess<J> inFlightAccess;

Expand Down Expand Up @@ -258,7 +259,7 @@ protected <Y> boolean isPrimitiveVariant(SingularAttribute<?,?> attribute, Class
@Override
@SuppressWarnings({ "unchecked" })
public Set<PluralAttribute<? super J, ?, ?>> getPluralAttributes() {
HashSet attributes = new HashSet<PluralAttribute<? super J, ?, ?>>( declaredPluralAttributes.values() );
HashSet attributes = declaredAttributes == null ? new HashSet<PluralAttribute<? super J, ?, ?>>() : new HashSet<PluralAttribute<? super J, ?, ?>>( declaredPluralAttributes.values() );
if ( getSuperType() != null ) {
attributes.addAll( getSuperType().getPluralAttributes() );
}
Expand All @@ -267,7 +268,8 @@ protected <Y> boolean isPrimitiveVariant(SingularAttribute<?,?> attribute, Class

@Override
public Set<PluralAttribute<J, ?, ?>> getDeclaredPluralAttributes() {
return new HashSet<PluralAttribute<J,?,?>>( declaredPluralAttributes.values() );
return declaredPluralAttributes == null ?
Collections.EMPTY_SET : new HashSet<PluralAttribute<J,?,?>>( declaredPluralAttributes.values() );
}

@Override
Expand All @@ -284,7 +286,7 @@ protected <Y> boolean isPrimitiveVariant(SingularAttribute<?,?> attribute, Class
@Override
@SuppressWarnings("unchecked")
public PluralPersistentAttribute<? super J, ?, ?> getPluralAttribute(String name) {
return declaredPluralAttributes.get( name );
return declaredPluralAttributes == null ? null : declaredPluralAttributes.get( name );
}

private void basicCollectionCheck(PluralAttribute<? super J, ?, ?> attribute, String name) {
Expand All @@ -297,7 +299,7 @@ private void basicCollectionCheck(PluralAttribute<? super J, ?, ?> attribute, St
@Override
@SuppressWarnings( "unchecked")
public CollectionAttribute<J, ?> getDeclaredCollection(String name) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
basicCollectionCheck( attribute, name );
return ( CollectionAttribute<J, ?> ) attribute;
}
Expand All @@ -323,7 +325,7 @@ private void basicSetCheck(PluralAttribute<? super J, ?, ?> attribute, String na
@Override
@SuppressWarnings( "unchecked")
public SetPersistentAttribute<J, ?> getDeclaredSet(String name) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
basicSetCheck( attribute, name );
return (SetPersistentAttribute) attribute;
}
Expand All @@ -349,7 +351,7 @@ private void basicListCheck(PluralAttribute<? super J, ?, ?> attribute, String n
@Override
@SuppressWarnings("unchecked")
public ListPersistentAttribute<J, ?> getDeclaredList(String name) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
basicListCheck( attribute, name );
return (ListPersistentAttribute) attribute;
}
Expand All @@ -375,15 +377,15 @@ private void basicMapCheck(PluralAttribute<? super J, ?, ?> attribute, String na
@Override
@SuppressWarnings("unchecked")
public MapPersistentAttribute<J, ?, ?> getDeclaredMap(String name) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
basicMapCheck( attribute, name );
return (MapPersistentAttribute) attribute;
}

@Override
@SuppressWarnings({ "unchecked" })
public <E> BagPersistentAttribute<? super J, E> getCollection(String name, Class<E> elementType) {
PluralAttribute<? super J, ?, ?> attribute = declaredPluralAttributes.get( name );
PluralAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSuperType() != null ) {
attribute = getSuperType().getPluralAttribute( name );
}
Expand All @@ -394,7 +396,7 @@ public <E> BagPersistentAttribute<? super J, E> getCollection(String name, Class
@Override
@SuppressWarnings("unchecked")
public <E> CollectionAttribute<J, E> getDeclaredCollection(String name, Class<E> elementType) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
checkCollectionElementType( attribute, name, elementType );
return (CollectionAttribute) attribute;
}
Expand Down Expand Up @@ -423,7 +425,7 @@ private <E> void checkTypeForPluralAttributes(
@Override
@SuppressWarnings({ "unchecked" })
public <E> SetAttribute<? super J, E> getSet(String name, Class<E> elementType) {
PluralAttribute<? super J, ?, ?> attribute = declaredPluralAttributes.get( name );
PluralAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSuperType() != null ) {
attribute = getSuperType().getPluralAttribute( name );
}
Expand All @@ -438,15 +440,15 @@ private <E> void checkSetElementType(PluralAttribute<? super J, ?, ?> attribute,
@Override
@SuppressWarnings("unchecked")
public <E> SetAttribute<J, E> getDeclaredSet(String name, Class<E> elementType) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
checkSetElementType( attribute, name, elementType );
return ( SetAttribute<J, E> ) attribute;
}

@Override
@SuppressWarnings({ "unchecked" })
public <E> ListAttribute<? super J, E> getList(String name, Class<E> elementType) {
PluralAttribute<? super J, ?, ?> attribute = declaredPluralAttributes.get( name );
PluralAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSuperType() != null ) {
attribute = getSuperType().getPluralAttribute( name );
}
Expand All @@ -461,7 +463,7 @@ private <E> void checkListElementType(PluralAttribute<? super J, ?, ?> attribute
@Override
@SuppressWarnings("unchecked")
public <E> ListAttribute<J, E> getDeclaredList(String name, Class<E> elementType) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
checkListElementType( attribute, name, elementType );
return ( ListAttribute<J, E> ) attribute;
}
Expand Down Expand Up @@ -492,7 +494,7 @@ private <K,V> void checkMapKeyType(MapAttribute<? super J, K, V> mapAttribute, S
@Override
@SuppressWarnings("unchecked")
public <K, V> MapAttribute<J, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType) {
final PluralAttribute<J,?,?> attribute = declaredPluralAttributes.get( name );
final PluralPersistentAttribute<? super J, ?, ?> attribute = getPluralAttribute( name );
checkMapValueType( attribute, name, valueType );
final MapAttribute<J, K, V> mapAttribute = ( MapAttribute<J, K, V> ) attribute;
checkMapKeyType( mapAttribute, name, keyType );
Expand Down Expand Up @@ -530,7 +532,10 @@ public void addAttribute(PersistentAttributeDescriptor<J, ?> attribute) {
break;
}
case PLURAL_ATTRIBUTE : {
declaredPluralAttributes.put(attribute.getName(), (PluralPersistentAttribute<J,?,?>) attribute );
if ( AbstractManagedType.this.declaredPluralAttributes == null ) {
AbstractManagedType.this.declaredPluralAttributes = new HashMap<>();
}
AbstractManagedType.this.declaredPluralAttributes.put( attribute.getName(), (PluralPersistentAttribute<J,?,?>) attribute );
break;
}
default : {
Expand Down

0 comments on commit 13398a8

Please sign in to comment.