Skip to content

Commit

Permalink
HHH-14305 Memory optimisations for ReaderCollector implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Nov 1, 2020
1 parent 7aca99f commit 7f4373f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,45 +264,62 @@ private static void applyKeyRestriction(SelectStatementBuilder select, String al
}

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,8 @@ public AbstractRowReader(ReaderCollector readerCollector) {
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)
Expand Down

0 comments on commit 7f4373f

Please sign in to comment.