From 41111b06cd7fed7a0271e5a649692e7061bf46bd Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Wed, 30 Jul 2025 14:10:08 +0100 Subject: [PATCH] HHH-19668 Reduce size of collections held in ParameterInterpretationImpl before storing them in the cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instances of ParameterInterpretationImpl are being held in the interpretation cache for native queries. We had reports of these elements having a substantial impact on memory consumption. A simple improvement is to ensure the collections being held on by the container object are minimized; some more substantial improvements could also be investigated but I’ll leave those for another time. --- .../internal/util/collections/CollectionHelper.java | 6 ++++++ .../org/hibernate/query/sql/internal/NativeQueryImpl.java | 8 +++++--- .../query/sql/internal/ParameterRecognizerImpl.java | 5 ++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java index a6486608c77b..f1dea4a5e78d 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java @@ -343,6 +343,9 @@ public static Set toSmallSet(Set set) { * The goal is to save memory. */ public static Map toSmallMap(final Map map) { + if ( map == null ) { + return emptyMap(); + } return switch ( map.size() ) { case 0 -> emptyMap(); case 1 -> { @@ -363,6 +366,9 @@ public static Map toSmallMap(final Map map) { * The goal is to save memory. */ public static List toSmallList(ArrayList arrayList) { + if ( arrayList == null ) { + return emptyList(); + } return switch ( arrayList.size() ) { case 0 -> emptyList(); case 1 -> singletonList( arrayList.get( 0 ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java index 1af5735720e7..1ce0bbdd8bab 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java @@ -128,6 +128,8 @@ import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty; import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty; import static org.hibernate.internal.util.collections.CollectionHelper.makeCopy; +import static org.hibernate.internal.util.collections.CollectionHelper.toSmallList; +import static org.hibernate.internal.util.collections.CollectionHelper.toSmallMap; import static org.hibernate.internal.util.type.PrimitiveWrapperHelper.getDescriptorByPrimitiveType; import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE; import static org.hibernate.query.results.internal.Builders.resultClassBuilder; @@ -1742,9 +1744,9 @@ private static class ParameterInterpretationImpl implements ParameterInterpretat public ParameterInterpretationImpl(ParameterRecognizerImpl parameterRecognizer) { this.sqlString = parameterRecognizer.getAdjustedSqlString(); - this.parameterList = parameterRecognizer.getParameterList(); - this.positionalParameters = parameterRecognizer.getPositionalQueryParameters(); - this.namedParameters = parameterRecognizer.getNamedQueryParameters(); + this.parameterList = toSmallList( parameterRecognizer.getParameterList() ); + this.positionalParameters = toSmallMap( parameterRecognizer.getPositionalQueryParameters() ); + this.namedParameters = toSmallMap( parameterRecognizer.getNamedQueryParameters() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java index d8561de73130..71b66daec418 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ParameterRecognizerImpl.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.hibernate.QueryException; @@ -36,7 +35,7 @@ private enum ParameterStyle { private int ordinalParameterImplicitPosition; - private List parameterList; + private ArrayList parameterList; private final StringBuilder sqlStringBuffer = new StringBuilder(); public ParameterRecognizerImpl() { @@ -81,7 +80,7 @@ public Map> getPositionalQueryParameters() return positionalQueryParameters; } - public List getParameterList() { + public ArrayList getParameterList() { return parameterList; }