From 667e0341fa17149ed531ae4fae28900f7812fcd9 Mon Sep 17 00:00:00 2001 From: benoit Date: Tue, 7 Nov 2017 18:07:42 +0100 Subject: [PATCH] Improve memory allocations when substituting named parameters. Create the buffer with at least the original sql length to avoid multiple re-allocations Add a fast path if the original sql doesn't contain any parameters --- .../core/namedparam/NamedParameterUtils.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java index 638a6ae5a4..8b4b0df5cd 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java @@ -258,8 +258,11 @@ public abstract class NamedParameterUtils { */ public static String substituteNamedParameters(ParsedSql parsedSql, @Nullable SqlParameterSource paramSource) { String originalSql = parsedSql.getOriginalSql(); - StringBuilder actualSql = new StringBuilder(); List paramNames = parsedSql.getParameterNames(); + if (paramNames.isEmpty()) { + return originalSql; + } + StringBuilder actualSql = new StringBuilder(originalSql.length()); int lastIndex = 0; for (int i = 0; i < paramNames.size(); i++) { String paramName = paramNames.get(i); @@ -283,26 +286,26 @@ public abstract class NamedParameterUtils { Object entryItem = entryIter.next(); if (entryItem instanceof Object[]) { Object[] expressionList = (Object[]) entryItem; - actualSql.append("("); + actualSql.append('('); for (int m = 0; m < expressionList.length; m++) { if (m > 0) { actualSql.append(", "); } - actualSql.append("?"); + actualSql.append('?'); } - actualSql.append(")"); + actualSql.append(')'); } else { - actualSql.append("?"); + actualSql.append('?'); } } } else { - actualSql.append("?"); + actualSql.append('?'); } } else { - actualSql.append("?"); + actualSql.append('?'); } lastIndex = endIndex; }