HHH-11032 : performance improvements of PersistentBag.equalsSnapshot

This commit is contained in:
Gail Badner 2019-07-19 19:17:19 -07:00 committed by Sanne Grinovero
parent f096445ccc
commit fa2f03593a
1 changed files with 39 additions and 13 deletions

View File

@ -11,7 +11,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@ -62,7 +61,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
*/
@Deprecated
public PersistentBag(SessionImplementor session) {
this( ( SharedSessionContractImplementor) session );
this( (SharedSessionContractImplementor) session );
}
/**
* Constructs a PersistentBag
@ -141,20 +140,37 @@ public class PersistentBag extends AbstractPersistentCollection implements List
// HHH-11032 - Group objects by Type.getHashCode() to reduce the complexity of the search
final Map<Integer, List<Object>> hashToInstancesBag = groupByEqualityHash( bag, elementType );
final Map<Integer, List<Object>> HashToInstancesSn = groupByEqualityHash( sn, elementType );
if ( hashToInstancesBag.size() != HashToInstancesSn.size() ) {
final Map<Integer, List<Object>> hashToInstancesSn = groupByEqualityHash( sn, elementType );
if ( hashToInstancesBag.size() != hashToInstancesSn.size() ) {
return false;
}
// First iterate over the hashToInstancesBag entries to see if the number
// of List values is different for any hash value.
for ( Map.Entry<Integer, List<Object>> hashToInstancesBagEntry : hashToInstancesBag.entrySet() ) {
final int hash = hashToInstancesBagEntry.getKey();
final Integer hash = hashToInstancesBagEntry.getKey();
final List<Object> instancesBag = hashToInstancesBagEntry.getValue();
final List<Object> instancesSn = HashToInstancesSn.getOrDefault( hash, Collections.emptyList() );
if ( instancesBag.size() != instancesSn.size() ) {
final List<Object> instancesSn = hashToInstancesSn.get( hash );
if ( instancesSn == null || ( instancesBag.size() != instancesSn.size() ) ) {
return false;
}
}
// We already know that both hashToInstancesBag and hashToInstancesSn have:
// 1) the same hash values;
// 2) the same number of values with the same hash value.
// Now check if the number of occurrences of each element is the same.
for ( Map.Entry<Integer, List<Object>> hashToInstancesBagEntry : hashToInstancesBag.entrySet() ) {
final Integer hash = hashToInstancesBagEntry.getKey();
final List<Object> instancesBag = hashToInstancesBagEntry.getValue();
final List<Object> instancesSn = hashToInstancesSn.get( hash );
for ( Object instance : instancesBag ) {
if ( countOccurrences( instance, instancesBag, elementType ) != countOccurrences( instance, instancesSn, elementType ) ) {
if ( !expectOccurrences(
instance,
instancesBag,
elementType,
countOccurrences( instance, instancesSn, elementType ) ) ) {
return false;
}
}
@ -176,18 +192,28 @@ public class PersistentBag extends AbstractPersistentCollection implements List
return ( (Collection) snapshot ).isEmpty();
}
private int countOccurrences(Object element, List list, Type elementType)
throws HibernateException {
final Iterator iter = list.iterator();
private int countOccurrences(Object element, List<Object> list, Type elementType) {
int result = 0;
while ( iter.hasNext() ) {
if ( elementType.isSame( element, iter.next() ) ) {
for ( Object listElement : list ) {
if ( elementType.isSame( element, listElement ) ) {
result++;
}
}
return result;
}
private boolean expectOccurrences(Object element, List<Object> list, Type elementType, int expected) {
int result = 0;
for ( Object listElement : list ) {
if ( elementType.isSame( element, listElement ) ) {
if ( result++ > expected ) {
return false;
}
}
}
return result == expected;
}
@Override
@SuppressWarnings("unchecked")
public Serializable getSnapshot(CollectionPersister persister)