Skip to content

Commit 667e034

Browse files
committed
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
1 parent 1a246c0 commit 667e034

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
258258
*/
259259
public static String substituteNamedParameters(ParsedSql parsedSql, @Nullable SqlParameterSource paramSource) {
260260
String originalSql = parsedSql.getOriginalSql();
261-
StringBuilder actualSql = new StringBuilder();
262261
List<String> paramNames = parsedSql.getParameterNames();
262+
if (paramNames.isEmpty()) {
263+
return originalSql;
264+
}
265+
StringBuilder actualSql = new StringBuilder(originalSql.length());
263266
int lastIndex = 0;
264267
for (int i = 0; i < paramNames.size(); i++) {
265268
String paramName = paramNames.get(i);
@@ -283,26 +286,26 @@ public static String substituteNamedParameters(ParsedSql parsedSql, @Nullable Sq
283286
Object entryItem = entryIter.next();
284287
if (entryItem instanceof Object[]) {
285288
Object[] expressionList = (Object[]) entryItem;
286-
actualSql.append("(");
289+
actualSql.append('(');
287290
for (int m = 0; m < expressionList.length; m++) {
288291
if (m > 0) {
289292
actualSql.append(", ");
290293
}
291-
actualSql.append("?");
294+
actualSql.append('?');
292295
}
293-
actualSql.append(")");
296+
actualSql.append(')');
294297
}
295298
else {
296-
actualSql.append("?");
299+
actualSql.append('?');
297300
}
298301
}
299302
}
300303
else {
301-
actualSql.append("?");
304+
actualSql.append('?');
302305
}
303306
}
304307
else {
305-
actualSql.append("?");
308+
actualSql.append('?');
306309
}
307310
lastIndex = endIndex;
308311
}

0 commit comments

Comments
 (0)