Skip to content

Commit b0a0ca1

Browse files
committed
HHH-18410 Make use of getter/setter cache as much as possible
1 parent 420faa7 commit b0a0ca1

28 files changed

+239
-158
lines changed

hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ private void visitEmbeddedAttributeMapping(
193193
if ( attribute.isPluralAttributeMapping() ) {
194194
addCollectionKey(
195195
attribute.asPluralAttributeMapping(),
196-
attribute.getPropertyAccess().getGetter().get( object ),
196+
descriptor.getValue( object, i ),
197197
persistenceContext
198198
);
199199
}
200200
else if ( attribute.isEmbeddedAttributeMapping() ) {
201201
visitEmbeddedAttributeMapping(
202202
attribute.asEmbeddedAttributeMapping(),
203-
attribute.getPropertyAccess().getGetter().get( object ),
203+
descriptor.getValue( object, i ),
204204
persistenceContext
205205
);
206206
}

hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ public static <T> void cascade(
9090
LOG.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() );
9191
}
9292
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
93-
final EntityEntry entry = persistenceContext.getEntry( parent );
94-
if ( entry != null
95-
&& entry.getLoadedState() == null
96-
&& entry.getStatus() == Status.MANAGED
97-
&& persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
98-
return;
93+
final boolean enhancedForLazyLoading = persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
94+
final EntityEntry entry;
95+
if ( enhancedForLazyLoading ) {
96+
entry = persistenceContext.getEntry( parent );
97+
if ( entry != null
98+
&& entry.getLoadedState() == null
99+
&& entry.getStatus() == Status.MANAGED ) {
100+
return;
101+
}
102+
}
103+
else {
104+
entry = null;
99105
}
100106
final Type[] types = persister.getPropertyTypes();
101107
final String[] propertyNames = persister.getPropertyNames();
@@ -113,6 +119,7 @@ public static <T> void cascade(
113119
if ( style.doCascade( action ) ) {
114120
final Object child;
115121
if ( isUninitializedProperty ) {
122+
assert enhancedForLazyLoading;
116123
// parent is a bytecode enhanced entity.
117124
// Cascade to an uninitialized, lazy value only if
118125
// parent is managed in the PersistenceContext.
@@ -378,7 +385,7 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
378385
* @return True if the attribute represents a logical one to one association
379386
*/
380387
private static boolean isLogicalOneToOne(Type type) {
381-
return type.isEntityType() && ( (EntityType) type ).isLogicalOneToOne();
388+
return type instanceof EntityType && ( (EntityType) type ).isLogicalOneToOne();
382389
}
383390

384391
private static boolean cascadeAssociationNow(

hibernate-core/src/main/java/org/hibernate/engine/internal/Collections.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ public static void processReachableCollection(
145145
Object entity,
146146
SessionImplementor session) {
147147
collection.setOwner( entity );
148-
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
149-
final CollectionEntry ce = persistenceContext.getCollectionEntry( collection );
148+
final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( collection );
150149

151150
if ( ce == null ) {
152151
// refer to comment in StatefulPersistenceContext.addCollection()

hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public EntityHolder claimEntityHolderIfPossible(
414414
}
415415

416416
@Override
417-
public EntityHolderImpl getEntityHolder(EntityKey key) {
417+
public @Nullable EntityHolderImpl getEntityHolder(EntityKey key) {
418418
return entitiesByKey == null ? null : entitiesByKey.get( key );
419419
}
420420

@@ -534,7 +534,7 @@ public Object removeEntity(EntityKey key) {
534534
}
535535

536536
@Override
537-
public EntityHolderImpl removeEntityHolder(EntityKey key) {
537+
public @Nullable EntityHolderImpl removeEntityHolder(EntityKey key) {
538538
final EntityHolderImpl holder;
539539
if ( entitiesByKey != null ) {
540540
holder = entitiesByKey.remove( key );
@@ -1333,8 +1333,8 @@ public int getNumberOfManagedEntities() {
13331333
*/
13341334
@Override
13351335
@Deprecated
1336-
public Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries() {
1337-
return getOrInitializeCollectionEntries();
1336+
public @Nullable Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries() {
1337+
return collectionEntries;
13381338
}
13391339

13401340
@Override

hibernate-core/src/main/java/org/hibernate/engine/spi/PersistenceContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ EntityHolder claimEntityHolderIfPossible(
524524
@Incubating
525525
EntityHolder addEntityHolder(EntityKey key, Object entity);
526526

527-
EntityHolder getEntityHolder(EntityKey key);
527+
@Nullable EntityHolder getEntityHolder(EntityKey key);
528528

529529
boolean containsEntityHolder(EntityKey key);
530530

531-
EntityHolder removeEntityHolder(EntityKey key);
531+
@Nullable EntityHolder removeEntityHolder(EntityKey key);
532532

533533
@Incubating
534534
void postLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> loadedConsumer);
@@ -567,7 +567,7 @@ EntityHolder claimEntityHolderIfPossible(
567567
* Doubly internal
568568
*/
569569
@Internal
570-
Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries();
570+
@Nullable Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries();
571571

572572
/**
573573
* Execute some action on each entry of the collectionEntries map, optionally iterating on a defensive copy.

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import org.hibernate.action.internal.CollectionRemoveAction;
1515
import org.hibernate.action.internal.CollectionUpdateAction;
1616
import org.hibernate.action.internal.QueuedOperationCollectionAction;
17+
import org.hibernate.collection.spi.PersistentCollection;
1718
import org.hibernate.engine.internal.Cascade;
1819
import org.hibernate.engine.internal.CascadePoint;
1920
import org.hibernate.engine.internal.Collections;
2021
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
2122
import org.hibernate.engine.spi.ActionQueue;
2223
import org.hibernate.engine.spi.CascadingAction;
2324
import org.hibernate.engine.spi.CascadingActions;
25+
import org.hibernate.engine.spi.CollectionEntry;
2426
import org.hibernate.engine.spi.CollectionKey;
2527
import org.hibernate.engine.spi.EntityEntry;
2628
import org.hibernate.engine.spi.PersistenceContext;
@@ -35,6 +37,7 @@
3537
import org.hibernate.event.spi.PersistContext;
3638
import org.hibernate.internal.CoreMessageLogger;
3739
import org.hibernate.internal.util.EntityPrinter;
40+
import org.hibernate.internal.util.collections.IdentityMap;
3841
import org.hibernate.persister.entity.EntityPersister;
3942

4043
import org.jboss.logging.Logger;
@@ -218,7 +221,12 @@ private void prepareCollectionFlushes(PersistenceContext persistenceContext) thr
218221
// and reset reached, doupdate, etc.
219222

220223
LOG.debug( "Dirty checking collections" );
221-
persistenceContext.forEachCollectionEntry( (pc,ce) -> ce.preFlush( pc ), true );
224+
final Map<PersistentCollection<?>, CollectionEntry> collectionEntries = persistenceContext.getCollectionEntries();
225+
if ( collectionEntries != null ) {
226+
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> entry : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
227+
entry.getValue().preFlush( entry.getKey() );
228+
}
229+
}
222230
}
223231

224232
/**
@@ -297,14 +305,20 @@ private int flushCollections(final EventSource session, final PersistenceContext
297305
throws HibernateException {
298306
LOG.trace( "Processing unreferenced collections" );
299307

300-
final int count = persistenceContext.getCollectionEntriesSize();
301-
302-
persistenceContext.forEachCollectionEntry(
303-
(persistentCollection, collectionEntry) -> {
304-
if ( !collectionEntry.isReached() && !collectionEntry.isIgnore() ) {
305-
Collections.processUnreachableCollection( persistentCollection, session );
306-
}
307-
}, true );
308+
final Map<PersistentCollection<?>, CollectionEntry> collectionEntries = persistenceContext.getCollectionEntries();
309+
final int count;
310+
if ( collectionEntries == null ) {
311+
count = 0;
312+
}
313+
else {
314+
count = collectionEntries.size();
315+
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
316+
final CollectionEntry ce = me.getValue();
317+
if ( !ce.isReached() && !ce.isIgnore() ) {
318+
Collections.processUnreachableCollection( me.getKey(), session );
319+
}
320+
}
321+
}
308322

309323
// Schedule updates to collections:
310324

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ protected Object processCollection(Object collection, CollectionType collectionT
353353
.getMappingMetamodel()
354354
.getCollectionDescriptor( collectionType.getRole() );
355355
final CollectionEntry collectionEntry = getSession().getPersistenceContextInternal()
356-
.getCollectionEntries()
357-
.get( coll );
356+
.getCollectionEntry( coll );
358357
if ( !coll.equalsSnapshot( persister ) ) {
359358
collectionEntry.resetStoredSnapshot( coll, coll.getSnapshot( persister ) );
360359
}

hibernate-core/src/main/java/org/hibernate/internal/util/collections/IdentityMap.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class IdentityMap<K,V> implements Map<K,V> {
2424

2525
private final LinkedHashMap<IdentityKey<K>,V> map;
2626

27-
private transient Entry<IdentityKey<K>,V>[] entryArray = null;
27+
private transient Entry<K,V>[] entryArray = null;
2828

2929
/**
3030
* Return a new instance of this class, with iteration
@@ -151,15 +151,14 @@ public Set<Entry<K,V>> entrySet() {
151151
return set;
152152
}
153153

154-
@SuppressWarnings( {"unchecked"})
155-
public Entry[] entryArray() {
154+
public Entry<K,V>[] entryArray() {
156155
if ( entryArray == null ) {
157156
entryArray = new Entry[ map.size() ];
158157
final Iterator<Entry<IdentityKey<K>, V>> itr = map.entrySet().iterator();
159158
int i = 0;
160159
while ( itr.hasNext() ) {
161160
final Entry<IdentityKey<K>, V> me = itr.next();
162-
entryArray[i++] = new IdentityMapEntry( me.getKey().key, me.getValue() );
161+
entryArray[i++] = new IdentityMapEntry<>( me.getKey().key, me.getValue() );
163162
}
164163
}
165164
return entryArray;

hibernate-core/src/main/java/org/hibernate/metamodel/internal/AbstractCompositeIdentifierMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public <X, Y> int forEachJdbcValue(
195195
else {
196196
for ( int i = 0; i < size; i++ ) {
197197
final AttributeMapping attributeMapping = embeddableTypeDescriptor.getAttributeMapping( i );
198-
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
198+
final Object o = embeddableTypeDescriptor.getValue( value, i );
199199
if ( attributeMapping instanceof ToOneAttributeMapping ) {
200200
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
201201
final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/AttributeMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ default String getPartName() {
5656
* Convenient access to getting the value for this attribute from the declarer
5757
*/
5858
default Object getValue(Object container) {
59-
return getPropertyAccess().getGetter().get( container );
59+
return getDeclaringType().getValue( container, getStateArrayPosition() );
6060
}
6161

6262
/**
6363
* Convenient access to setting the value for this attribute on the declarer
6464
*/
6565
default void setValue(Object container, Object value) {
66-
getPropertyAccess().getSetter().set( container, value );
66+
getDeclaringType().setValue( container, getStateArrayPosition(), value );
6767
}
6868

6969
/**

0 commit comments

Comments
 (0)