From 75978560aa1a17e5131e010509afb1b039861e3d Mon Sep 17 00:00:00 2001 From: Patrick Schmitt Date: Fri, 6 Oct 2023 10:55:02 +0200 Subject: [PATCH] Issue 1957: Presize HashMap allocations in UnitOfWorkImpl add initial size to map creation where expected size is known or can be estimated Signed-off-by: Patrick Schmitt --- .../internal/sessions/UnitOfWorkImpl.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java index 2c84a161995..12c597698c9 100644 --- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java +++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java @@ -50,6 +50,7 @@ import java.util.Map; import java.util.Set; import java.util.Vector; +import java.util.stream.Collectors; import org.eclipse.persistence.annotations.CacheKeyType; import org.eclipse.persistence.config.ReferenceMode; @@ -708,8 +709,9 @@ public UnitOfWorkChangeSet calculateChanges(Map registeredObjects, UnitOfWorkCha // Second calculate changes for all registered objects. Iterator objects = allObjects.keySet().iterator(); - Map changedObjects = new IdentityHashMap(); - Map visitedNodes = new IdentityHashMap(); + int allObjectsSize = allObjects.size(); + Map changedObjects = new IdentityHashMap(allObjectsSize); + Map visitedNodes = new IdentityHashMap(allObjectsSize); while (objects.hasNext()) { Object object = objects.next(); @@ -785,7 +787,7 @@ public UnitOfWorkChangeSet calculateChanges(Map registeredObjects, UnitOfWorkCha if (this.shouldDiscoverNewObjects && !changedObjects.isEmpty()) { // Third discover any new objects from the new or changed objects. - Map newObjects = new IdentityHashMap(); + Map newObjects = new IdentityHashMap(changedObjects.size()); // Bug 294259 - Do not replace the existingObjects list // Iterate over the changed objects only. discoverUnregisteredNewObjects(changedObjects, newObjects, getUnregisteredExistingObjects(), visitedNodes); @@ -806,8 +808,9 @@ public UnitOfWorkChangeSet calculateChanges(Map registeredObjects, UnitOfWorkCha // Remove any orphaned privately owned objects from the UnitOfWork and ChangeSets, // these are the objects remaining in the UnitOfWork privateOwnedObjects map if (hasPrivateOwnedObjects()) { - Map visitedObjects = new IdentityHashMap(); - for (Set privateOwnedObjects : getPrivateOwnedObjects().values()) { + Collection values = getPrivateOwnedObjects().values(); + Map visitedObjects = new IdentityHashMap(values.stream().collect(Collectors.summingInt(Set::size))); + for (Set privateOwnedObjects : values) { for (Object objectToRemove : privateOwnedObjects) { performRemovePrivateOwnedObjectFromChangeSet(objectToRemove, visitedObjects); } @@ -5028,8 +5031,9 @@ protected void setNewObjectsCloneToOriginal(Map newObjects) { protected void setupPrimaryKeyToNewObjects() { primaryKeyToNewObjects = null; if (hasNewObjects()) { - primaryKeyToNewObjects = new HashMap<>(); - getNewObjectsCloneToOriginal().forEach((object, o2) -> { + Map newObjects = getNewObjectsCloneToOriginal(); + primaryKeyToNewObjects = new HashMap<>(newObjects.size()); + newObjects.forEach((object, o2) -> { addNewObjectToPrimaryKeyToNewObjects(object); }); }