HHH-14305 Memory optimisations for ReaderCollector implementations

This commit is contained in:
Sanne Grinovero 2020-11-01 21:18:17 +00:00
parent 7aca99f30d
commit 7f4373f7b9
2 changed files with 33 additions and 20 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.loader.plan.exec.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -263,45 +264,62 @@ public abstract class AbstractLoadQueryDetails implements LoadQueryDetails {
}
protected abstract static class ReaderCollectorImpl implements ReaderCollector {
private final List<EntityReferenceInitializer> entityReferenceInitializers = new ArrayList<EntityReferenceInitializer>();
private List<EntityReferenceInitializer> entityReferenceInitializers;
private List<CollectionReferenceInitializer> arrayReferenceInitializers;
private List<CollectionReferenceInitializer> collectionReferenceInitializers;
@Override
public void add(CollectionReferenceInitializer collectionReferenceInitializer) {
if ( collectionReferenceInitializer.getCollectionReference().getCollectionPersister().isArray() ) {
if ( arrayReferenceInitializers == null ) {
arrayReferenceInitializers = new ArrayList<CollectionReferenceInitializer>();
}
arrayReferenceInitializers.add( collectionReferenceInitializer );
arrayReferenceInitializers = addTo( arrayReferenceInitializers, collectionReferenceInitializer );
}
else {
if ( collectionReferenceInitializers == null ) {
collectionReferenceInitializers = new ArrayList<CollectionReferenceInitializer>();
}
collectionReferenceInitializers.add( collectionReferenceInitializer );
collectionReferenceInitializers = addTo( collectionReferenceInitializers, collectionReferenceInitializer );
}
}
/**
* LISP-style list growing, as there is a strong likelihood we'll be dealing with lists of zero or one element
* we can save some memory.
* @param host
* @param element
* @param <V>
* @return possibly a new list instance, containing both the original elements and the new elements.
*/
private static <V> List<V> addTo(List<V> host, V element) {
List<V> output = host;
if ( output == null ) {
output = Collections.singletonList( element );
}
else if ( output.size() == 1 ) {
output = new ArrayList<V>( output );
output.add( element );
}
else {
output.add( element );
}
return output;
}
@Override
public void add(EntityReferenceInitializer entityReferenceInitializer) {
entityReferenceInitializers.add( entityReferenceInitializer );
entityReferenceInitializers = addTo( entityReferenceInitializers, entityReferenceInitializer );
}
@Override
public final List<EntityReferenceInitializer> getEntityReferenceInitializers() {
return entityReferenceInitializers;
return entityReferenceInitializers == null ? Collections.EMPTY_LIST : entityReferenceInitializers;
}
@Override
public List<CollectionReferenceInitializer> getArrayReferenceInitializers() {
return arrayReferenceInitializers;
return arrayReferenceInitializers == null ? Collections.EMPTY_LIST : arrayReferenceInitializers;
}
@Override
public List<CollectionReferenceInitializer> getNonArrayCollectionReferenceInitializers() {
return collectionReferenceInitializers;
return collectionReferenceInitializers == null ? Collections.EMPTY_LIST : collectionReferenceInitializers;
}
}
}

View File

@ -72,13 +72,8 @@ public abstract class AbstractRowReader implements RowReader {
entityReferenceInitializers = Collections.<EntityReferenceInitializer>emptyList();
entityInitializerByEntityReference = Collections.<EntityReference,EntityReferenceInitializer>emptyMap();
}
this.arrayReferenceInitializers = CollectionHelper.isNotEmpty( readerCollector.getArrayReferenceInitializers() )
? new ArrayList<CollectionReferenceInitializer>( readerCollector.getArrayReferenceInitializers() )
: Collections.<CollectionReferenceInitializer>emptyList();
this.collectionReferenceInitializers =
CollectionHelper.isNotEmpty ( readerCollector.getNonArrayCollectionReferenceInitializers() )
? new ArrayList<CollectionReferenceInitializer>( readerCollector.getNonArrayCollectionReferenceInitializers() )
: Collections.<CollectionReferenceInitializer>emptyList();
this.arrayReferenceInitializers = readerCollector.getArrayReferenceInitializers();
this.collectionReferenceInitializers = readerCollector.getNonArrayCollectionReferenceInitializers();
}
protected abstract Object readLogicalRow(ResultSet resultSet, ResultSetProcessingContextImpl context)