From f41b11ad3f5124a08e358f959b9fc9af16100aeb Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Fri, 5 Jul 2019 15:13:49 +0100 Subject: [PATCH] HHH-13496 Identified some String appending in loops --- .../hibernate/dialect/Teradata14Dialect.java | 3 ++- .../hql/internal/ast/tree/MapEntryNode.java | 18 +++++++++++------- .../hibernate/internal/util/StringHelper.java | 9 +++++---- .../collection/BasicCollectionJoinWalker.java | 18 +++++++++++------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Teradata14Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Teradata14Dialect.java index 867673ce7f..00ce90322b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Teradata14Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Teradata14Dialect.java @@ -254,7 +254,8 @@ public class Teradata14Dialect extends TeradataDialect { final StringBuilder buf = new StringBuilder() .append( "create index " ) .append( indexNameForCreation ) - .append( "(" + colBuf ) + .append( '(' ) + .append( colBuf ) .append( " on " ) .append( tableName ); diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/MapEntryNode.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/MapEntryNode.java index d775376864..44d4b58a17 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/MapEntryNode.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/MapEntryNode.java @@ -92,16 +92,20 @@ public class MapEntryNode extends AbstractMapComponentNode implements Aggregated determineKeySelectExpressions( collectionPersister, selections ); determineValueSelectExpressions( collectionPersister, selections ); - String text = ""; - String[] columns = new String[selections.size()]; - for ( int i = 0; i < selections.size(); i++ ) { + final int columnNumber = selections.size(); + StringBuilder text = new StringBuilder( columnNumber * 12 ); //Some guess + String[] columns = new String[columnNumber]; + for ( int i = 0; i < columnNumber; i++ ) { SelectExpression selectExpression = (SelectExpression) selections.get( i ); - text += ( ", " + selectExpression.getExpression() + " as " + selectExpression.getAlias() ); + if ( i != 0 ) { + text.append( ", " ); + } + text.append( selectExpression.getExpression() ); + text.append( " as " ); + text.append( selectExpression.getAlias() ); columns[i] = selectExpression.getExpression(); } - - text = text.substring( 2 ); //strip leading ", " - setText( text ); + setText( text.toString() ); setResolved(); return columns; } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java index 4533d5b696..b369873c3d 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java @@ -318,14 +318,15 @@ public final class StringHelper { */ public static String collapseQualifier(String qualifier, boolean includeDots) { StringTokenizer tokenizer = new StringTokenizer( qualifier, "." ); - String collapsed = Character.toString( tokenizer.nextToken().charAt( 0 ) ); + StringBuilder sb = new StringBuilder(); + sb.append( Character.toString( tokenizer.nextToken().charAt( 0 ) ) ); while ( tokenizer.hasMoreTokens() ) { if ( includeDots ) { - collapsed += '.'; + sb.append( '.' ); } - collapsed += tokenizer.nextToken().charAt( 0 ); + sb.append( tokenizer.nextToken().charAt( 0 ) ); } - return collapsed; + return sb.toString(); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java b/hibernate-core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java index 6c841743b2..b0e4b1142f 100755 --- a/hibernate-core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java @@ -8,8 +8,10 @@ package org.hibernate.loader.collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.hibernate.FetchMode; +import org.hibernate.Filter; import org.hibernate.LockMode; import org.hibernate.MappingException; import org.hibernate.engine.spi.CascadeStyle; @@ -75,8 +77,9 @@ public class BasicCollectionJoinWalker extends CollectionJoinWalker { batchSize ); - String manyToManyOrderBy = ""; - String filter = collectionPersister.filterFragment( alias, getLoadQueryInfluencers().getEnabledFilters() ); + StringBuilder sb = null; + final Map enabledFilters = getLoadQueryInfluencers().getEnabledFilters(); + String filter = collectionPersister.filterFragment( alias, enabledFilters ); if ( collectionPersister.isManyToMany() ) { // from the collection of associations, locate OJA for the // ManyToOne corresponding to this persister to fully @@ -84,16 +87,17 @@ public class BasicCollectionJoinWalker extends CollectionJoinWalker { // use its alias here // TODO : is there a better way here? Iterator itr = associations.iterator(); + sb = new StringBuilder( 20 ); AssociationType associationType = ( AssociationType ) collectionPersister.getElementType(); while ( itr.hasNext() ) { OuterJoinableAssociation oja = ( OuterJoinableAssociation ) itr.next(); if ( oja.getJoinableType() == associationType ) { // we found it filter += collectionPersister.getManyToManyFilterFragment( - oja.getRHSAlias(), - getLoadQueryInfluencers().getEnabledFilters() + oja.getRHSAlias(), + enabledFilters ); - manyToManyOrderBy += collectionPersister.getManyToManyOrderByString( oja.getRHSAlias() ); + sb.append( collectionPersister.getManyToManyOrderByString( oja.getRHSAlias() ) ); } } } @@ -103,7 +107,7 @@ public class BasicCollectionJoinWalker extends CollectionJoinWalker { Select select = new Select( getDialect() ) .setSelectClause( collectionPersister.selectFragment(alias, collectionSuffixes[0] ) + - selectString(associations) + selectString( associations ) ) .setFromClause( collectionPersister.getTableName(), alias ) .setWhereClause( whereString.toString() ) @@ -112,7 +116,7 @@ public class BasicCollectionJoinWalker extends CollectionJoinWalker { ojf.toWhereFragmentString() ); - select.setOrderByClause( orderBy( associations, mergeOrderings( collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy ) ) ); + select.setOrderByClause( orderBy( associations, mergeOrderings( collectionPersister.getSQLOrderByString( alias ), sb == null ? "" : sb.toString() ) ) ); if ( getFactory().getSettings().isCommentsEnabled() ) { select.setComment( "load collection " + collectionPersister.getRole() );