Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[4.0] fix #1957: Presize HashMap allocations - backport from master #2179

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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;
Expand Down Expand Up @@ -711,8 +712,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();

Expand Down Expand Up @@ -788,7 +790,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);
Expand All @@ -809,7 +811,8 @@ 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();
Collection<Set> values = getPrivateOwnedObjects().values();
Map visitedObjects = new IdentityHashMap(values.stream().collect(Collectors.summingInt(Set::size)));
for (Set privateOwnedObjects : getPrivateOwnedObjects().values()) {
for (Object objectToRemove : privateOwnedObjects) {
performRemovePrivateOwnedObjectFromChangeSet(objectToRemove, visitedObjects);
Expand Down Expand Up @@ -5029,7 +5032,8 @@ protected void setNewObjectsCloneToOriginal(Map newObjects) {
protected void setupPrimaryKeyToNewObjects() {
primaryKeyToNewObjects = null;
if (hasNewObjects()) {
primaryKeyToNewObjects = new HashMap<>();
Map newObjects = getNewObjectsCloneToOriginal();
primaryKeyToNewObjects = new HashMap<>(newObjects.size());
getNewObjectsCloneToOriginal().forEach((object, o2) -> {
addNewObjectToPrimaryKeyToNewObjects(object);
});
Expand Down
Loading