提交 5a1c0bb5 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reduce memory allocation in Parser.setSQL()

上级 fcd4160d
...@@ -2687,9 +2687,14 @@ public class Parser { ...@@ -2687,9 +2687,14 @@ public class Parser {
} }
private void setSQL(Prepared command, String start, int startIndex) { private void setSQL(Prepared command, String start, int startIndex) {
String sql = StringUtils.trimSubstring(originalSQL, startIndex, lastParseIndex); int endIndex = lastParseIndex;
String sql;
if (start != null) { if (start != null) {
sql = start + " " + sql; StringBuilder builder = new StringBuilder(start.length() + endIndex - startIndex + 1)
.append(start).append(' ');
sql = StringUtils.trimSubstring(builder, originalSQL, startIndex, endIndex).toString();
} else {
sql = StringUtils.trimSubstring(originalSQL, startIndex, endIndex);
} }
command.setSQL(sql); command.setSQL(sql);
} }
......
...@@ -1624,9 +1624,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -1624,9 +1624,10 @@ public class JdbcDatabaseMetaData extends TraceObject implements
int spaceIndex = f.indexOf(' '); int spaceIndex = f.indexOf(' ');
if (spaceIndex >= 0) { if (spaceIndex >= 0) {
// remove 'Function' from 'INSERT Function' // remove 'Function' from 'INSERT Function'
f = StringUtils.trimSubstring(f, 0, spaceIndex); StringUtils.trimSubstring(buff.builder(), f, 0, spaceIndex);
} else {
buff.append(f);
} }
buff.append(f);
} }
} }
rs.close(); rs.close();
......
...@@ -872,7 +872,7 @@ public class StringUtils { ...@@ -872,7 +872,7 @@ public class StringUtils {
} }
/** /**
* Trim a character from a substring. Equivalent of * Trim a whitespace from a substring. Equivalent of
* {@code substring(beginIndex).trim()}. * {@code substring(beginIndex).trim()}.
* *
* @param s the string * @param s the string
...@@ -884,7 +884,7 @@ public class StringUtils { ...@@ -884,7 +884,7 @@ public class StringUtils {
} }
/** /**
* Trim a character from a substring. Equivalent of * Trim a whitespace from a substring. Equivalent of
* {@code substring(beginIndex, endIndex).trim()}. * {@code substring(beginIndex, endIndex).trim()}.
* *
* @param s the string * @param s the string
...@@ -902,6 +902,27 @@ public class StringUtils { ...@@ -902,6 +902,27 @@ public class StringUtils {
return s.substring(beginIndex, endIndex); return s.substring(beginIndex, endIndex);
} }
/**
* Trim a whitespace from a substring and append it to a specified string
* builder. Equivalent of
* {@code builder.append(substring(beginIndex, endIndex).trim())}.
*
* @param builder string builder to append to
* @param s the string
* @param beginIndex start index of substring (inclusive)
* @param endIndex end index of substring (exclusive)
* @return the specified builder
*/
public static StringBuilder trimSubstring(StringBuilder builder, String s, int beginIndex, int endIndex) {
while (beginIndex < endIndex && s.charAt(beginIndex) <= ' ') {
beginIndex++;
}
while (beginIndex < endIndex && s.charAt(endIndex - 1) <= ' ') {
endIndex--;
}
return builder.append(s, beginIndex, endIndex);
}
/** /**
* Get the string from the cache if possible. If the string has not been * Get the string from the cache if possible. If the string has not been
* found, it is added to the cache. If there is such a string in the cache, * found, it is added to the cache. If there is such a string in the cache,
......
...@@ -281,14 +281,22 @@ public class TestStringUtils extends TestBase { ...@@ -281,14 +281,22 @@ public class TestStringUtils extends TestBase {
} }
private void testTrimSubstring() { private void testTrimSubstring() {
assertEquals("", StringUtils.trimSubstring("", 0, 0)); testTrimSubstringImpl("", "", 0, 0);
assertEquals("", StringUtils.trimSubstring(" ", 0, 0)); testTrimSubstringImpl("", " ", 0, 0);
assertEquals("", StringUtils.trimSubstring(" ", 4, 4)); testTrimSubstringImpl("", " ", 4, 4);
assertEquals("select", StringUtils.trimSubstring(" select from", 1, 7)); testTrimSubstringImpl("select", " select from", 1, 7);
assertEquals("a b", StringUtils.trimSubstring(" a b ", 1, 4)); testTrimSubstringImpl("a b", " a b ", 1, 4);
testTrimSubstringImpl("a b", " a b ", 1, 5);
testTrimSubstringImpl("b", " a b ", 2, 5);
new AssertThrows(StringIndexOutOfBoundsException.class) { @Override new AssertThrows(StringIndexOutOfBoundsException.class) { @Override
public void test() { StringUtils.trimSubstring(" with (", 1, 8); } public void test() { StringUtils.trimSubstring(" with (", 1, 8); }
}; };
} }
private void testTrimSubstringImpl(String expected, String string, int startIndex, int endIndex) {
assertEquals(expected, StringUtils.trimSubstring(string, startIndex, endIndex));
assertEquals(expected, StringUtils
.trimSubstring(new StringBuilder(endIndex - startIndex), string, startIndex, endIndex).toString());
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论