HHH-11032 performance improvements of PersistentBag.equalsSnapshot

This commit is contained in:
Gail Badner 2019-07-10 19:22:09 -07:00 committed by Sanne Grinovero
parent 4c2a43b86f
commit f096445ccc
1 changed files with 15 additions and 15 deletions

View File

@ -12,7 +12,6 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
@ -134,34 +133,35 @@ public class PersistentBag extends AbstractPersistentCollection implements List
@Override @Override
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
List<Object> sn = (List<Object>) getSnapshot(); final List<Object> sn = (List<Object>) getSnapshot();
if ( sn.size() != bag.size() ) { if ( sn.size() != bag.size() ) {
return false; return false;
} }
// HHH-11032 - Group objects by Type.getHashCode() to reduce the complexity of the search // HHH-11032 - Group objects by Type.getHashCode() to reduce the complexity of the search
Map<Integer, List<Object>> instanceCountBag = groupByEqualityHash( bag, elementType ); final Map<Integer, List<Object>> hashToInstancesBag = groupByEqualityHash( bag, elementType );
Map<Integer, List<Object>> instanceCountSn = groupByEqualityHash( sn, elementType ); final Map<Integer, List<Object>> HashToInstancesSn = groupByEqualityHash( sn, elementType );
if ( instanceCountBag.size() != instanceCountSn.size() ) { if ( hashToInstancesBag.size() != HashToInstancesSn.size() ) {
return false; return false;
} }
for ( Object elt : bag ) { for ( Map.Entry<Integer, List<Object>> hashToInstancesBagEntry : hashToInstancesBag.entrySet() ) {
List<Object> candidatesBag = getItemsWithSameHash( instanceCountBag, elt, elementType ); final int hash = hashToInstancesBagEntry.getKey();
List<Object> candidatesSn = getItemsWithSameHash( instanceCountSn, elt, elementType ); final List<Object> instancesBag = hashToInstancesBagEntry.getValue();
final List<Object> instancesSn = HashToInstancesSn.getOrDefault( hash, Collections.emptyList() );
if ( countOccurrences( elt, candidatesBag, elementType ) != countOccurrences( elt, candidatesSn, elementType ) ) { if ( instancesBag.size() != instancesSn.size() ) {
return false; return false;
} }
for ( Object instance : instancesBag ) {
if ( countOccurrences( instance, instancesBag, elementType ) != countOccurrences( instance, instancesSn, elementType ) ) {
return false;
}
}
} }
return true; return true;
} }
private List<Object> getItemsWithSameHash(Map<Integer, List<Object>> 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 * Groups items in searchedBag according to persistence "equality" as defined in Type.isSame and Type.getHashCode
* *