Skip to content

Commit a22944f

Browse files
committed
HHH-18410 Make use of getter/setter cache as much as possible
1 parent ea6dfd7 commit a22944f

27 files changed

+214
-120
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
@@ -182,14 +182,14 @@ private void visitEmbeddedAttributeMapping(
182182
if ( attribute.isPluralAttributeMapping() ) {
183183
addCollectionKey(
184184
attribute.asPluralAttributeMapping(),
185-
attribute.getPropertyAccess().getGetter().get( object ),
185+
descriptor.getValue( object, i ),
186186
persistenceContext
187187
);
188188
}
189189
else if ( attribute.isEmbeddedAttributeMapping() ) {
190190
visitEmbeddedAttributeMapping(
191191
attribute.asEmbeddedAttributeMapping(),
192-
attribute.getPropertyAccess().getGetter().get( object ),
192+
descriptor.getValue( object, i ),
193193
persistenceContext
194194
);
195195
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ public static <T> void cascade(
8686
LOG.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() );
8787
}
8888
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
89-
final EntityEntry entry = persistenceContext.getEntry( parent );
90-
if ( entry != null && entry.getLoadedState() == null && entry.getStatus() == Status.MANAGED && persister.getBytecodeEnhancementMetadata()
91-
.isEnhancedForLazyLoading() ) {
92-
return;
89+
final boolean enhancedForLazyLoading = persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
90+
final EntityEntry entry;
91+
if ( enhancedForLazyLoading ) {
92+
entry = persistenceContext.getEntry( parent );
93+
if ( entry != null && entry.getLoadedState() == null && entry.getStatus() == Status.MANAGED ) {
94+
return;
95+
}
96+
}
97+
else {
98+
entry = null;
9399
}
94100
final Type[] types = persister.getPropertyTypes();
95101
final String[] propertyNames = persister.getPropertyNames();
@@ -107,6 +113,7 @@ public static <T> void cascade(
107113
if ( style.doCascade( action ) ) {
108114
final Object child;
109115
if ( isUninitializedProperty ) {
116+
assert enhancedForLazyLoading;
110117
// parent is a bytecode enhanced entity.
111118
// Cascade to an uninitialized, lazy value only if
112119
// parent is managed in the PersistenceContext.
@@ -216,7 +223,7 @@ private static <T> void cascadeProperty(
216223
final String propertyName,
217224
final T anything,
218225
final boolean isCascadeDeleteEnabled) throws HibernateException {
219-
226+
220227
if ( child != null ) {
221228
if ( type.isAssociationType() ) {
222229
final AssociationType associationType = (AssociationType) type;
@@ -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(final CascadePoint cascadePoint, AssociationType associationType) {

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
@@ -412,7 +412,7 @@ public EntityHolder claimEntityHolderIfPossible(
412412
}
413413

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

@@ -525,7 +525,7 @@ public Object removeEntity(EntityKey key) {
525525
}
526526

527527
@Override
528-
public EntityHolderImpl removeEntityHolder(EntityKey key) {
528+
public @Nullable EntityHolderImpl removeEntityHolder(EntityKey key) {
529529
final EntityHolderImpl holder;
530530
if ( entitiesByKey != null ) {
531531
holder = entitiesByKey.remove( key );
@@ -1316,8 +1316,8 @@ public int getNumberOfManagedEntities() {
13161316
*/
13171317
@Override
13181318
@Deprecated
1319-
public Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries() {
1320-
return getOrInitializeCollectionEntries();
1319+
public @Nullable Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries() {
1320+
return collectionEntries;
13211321
}
13221322

13231323
@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
@@ -521,11 +521,11 @@ EntityHolder claimEntityHolderIfPossible(
521521
JdbcValuesSourceProcessingState processingState,
522522
EntityInitializer initializer);
523523

524-
EntityHolder getEntityHolder(EntityKey key);
524+
@Nullable EntityHolder getEntityHolder(EntityKey key);
525525

526526
boolean containsEntityHolder(EntityKey key);
527527

528-
EntityHolder removeEntityHolder(EntityKey key);
528+
@Nullable EntityHolder removeEntityHolder(EntityKey key);
529529

530530
@Incubating
531531
void postLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> loadedConsumer);
@@ -564,7 +564,7 @@ EntityHolder claimEntityHolderIfPossible(
564564
* Doubly internal
565565
*/
566566
@Internal
567-
Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries();
567+
@Nullable Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries();
568568

569569
/**
570570
* 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;
@@ -191,7 +194,12 @@ private void prepareCollectionFlushes(PersistenceContext persistenceContext) thr
191194
// and reset reached, doupdate, etc.
192195

193196
LOG.debug( "Dirty checking collections" );
194-
persistenceContext.forEachCollectionEntry( (pc,ce) -> ce.preFlush( pc ), true );
197+
final Map<PersistentCollection<?>, CollectionEntry> collectionEntries = persistenceContext.getCollectionEntries();
198+
if ( collectionEntries != null ) {
199+
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> entry : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
200+
entry.getValue().preFlush( entry.getKey() );
201+
}
202+
}
195203
}
196204

197205
/**
@@ -270,14 +278,20 @@ private int flushCollections(final EventSource session, final PersistenceContext
270278
throws HibernateException {
271279
LOG.trace( "Processing unreferenced collections" );
272280

273-
final int count = persistenceContext.getCollectionEntriesSize();
274-
275-
persistenceContext.forEachCollectionEntry(
276-
(persistentCollection, collectionEntry) -> {
277-
if ( !collectionEntry.isReached() && !collectionEntry.isIgnore() ) {
278-
Collections.processUnreachableCollection( persistentCollection, session );
279-
}
280-
}, true );
281+
final Map<PersistentCollection<?>, CollectionEntry> collectionEntries = persistenceContext.getCollectionEntries();
282+
final int count;
283+
if ( collectionEntries == null ) {
284+
count = 0;
285+
}
286+
else {
287+
count = collectionEntries.size();
288+
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
289+
final CollectionEntry ce = me.getValue();
290+
if ( !ce.isReached() && !ce.isIgnore() ) {
291+
Collections.processUnreachableCollection( me.getKey(), session );
292+
}
293+
}
294+
}
281295

282296
// Schedule updates to collections:
283297

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
@@ -352,8 +352,7 @@ protected Object processCollection(Object collection, CollectionType collectionT
352352
.getMappingMetamodel()
353353
.getCollectionDescriptor( collectionType.getRole() );
354354
final CollectionEntry collectionEntry = getSession().getPersistenceContextInternal()
355-
.getCollectionEntries()
356-
.get( coll );
355+
.getCollectionEntry( coll );
357356
if ( !coll.equalsSnapshot( persister ) ) {
358357
collectionEntry.resetStoredSnapshot( coll, coll.getSnapshot( persister ) );
359358
}

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
@@ -185,7 +185,7 @@ public <X, Y> int forEachJdbcValue(
185185
else {
186186
for ( int i = 0; i < size; i++ ) {
187187
final AttributeMapping attributeMapping = embeddableTypeDescriptor.getAttributeMapping( i );
188-
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
188+
final Object o = embeddableTypeDescriptor.getValue( value, i );
189189
if ( attributeMapping instanceof ToOneAttributeMapping ) {
190190
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
191191
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)