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
This commit is contained in:
benoit 2017-11-07 18:07:42 +01:00
parent 1a246c0a67
commit 667e0341fa
1 changed files with 10 additions and 7 deletions

View File

@ -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<String> 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;
}