From f096445ccc17eccd2d89df23c1865b0f8a08373c Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 10 Jul 2019 19:22:09 -0700 Subject: [PATCH] HHH-11032 performance improvements of PersistentBag.equalsSnapshot --- .../collection/internal/PersistentBag.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentBag.java b/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentBag.java index 3c4f9380ef..75f6762cb8 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentBag.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentBag.java @@ -12,7 +12,6 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -134,34 +133,35 @@ public class PersistentBag extends AbstractPersistentCollection implements List @Override @SuppressWarnings( "unchecked" ) public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { - Type elementType = persister.getElementType(); - List sn = (List) getSnapshot(); + final Type elementType = persister.getElementType(); + final List sn = (List) getSnapshot(); if ( sn.size() != bag.size() ) { return false; } // HHH-11032 - Group objects by Type.getHashCode() to reduce the complexity of the search - Map> instanceCountBag = groupByEqualityHash( bag, elementType ); - Map> instanceCountSn = groupByEqualityHash( sn, elementType ); - if ( instanceCountBag.size() != instanceCountSn.size() ) { + final Map> hashToInstancesBag = groupByEqualityHash( bag, elementType ); + final Map> HashToInstancesSn = groupByEqualityHash( sn, elementType ); + if ( hashToInstancesBag.size() != HashToInstancesSn.size() ) { return false; } - for ( Object elt : bag ) { - List candidatesBag = getItemsWithSameHash( instanceCountBag, elt, elementType ); - List candidatesSn = getItemsWithSameHash( instanceCountSn, elt, elementType ); - - if ( countOccurrences( elt, candidatesBag, elementType ) != countOccurrences( elt, candidatesSn, elementType ) ) { + for ( Map.Entry> hashToInstancesBagEntry : hashToInstancesBag.entrySet() ) { + final int hash = hashToInstancesBagEntry.getKey(); + final List instancesBag = hashToInstancesBagEntry.getValue(); + final List instancesSn = HashToInstancesSn.getOrDefault( hash, Collections.emptyList() ); + if ( instancesBag.size() != instancesSn.size() ) { return false; } + for ( Object instance : instancesBag ) { + if ( countOccurrences( instance, instancesBag, elementType ) != countOccurrences( instance, instancesSn, elementType ) ) { + return false; + } + } } return true; } - private List getItemsWithSameHash(Map> instanceCountBag, Object elt, Type elementType) { - return instanceCountBag.getOrDefault( elementType.getHashCode( elt ), Collections.emptyList() ); - } - /** * Groups items in searchedBag according to persistence "equality" as defined in Type.isSame and Type.getHashCode *