Skip to content

Commit 4668f59

Browse files
authored
Merge pull request #3418 from nieqiurong/202502222213
Reuse `SqlNode` instance created for virtually empty text node
2 parents 1a145cd + f28e63a commit 4668f59

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.concurrent.ConcurrentHashMap;
2223

2324
import org.apache.ibatis.builder.BaseBuilder;
2425
import org.apache.ibatis.builder.BuilderException;
@@ -38,6 +39,7 @@ public class XMLScriptBuilder extends BaseBuilder {
3839
private boolean isDynamic;
3940
private final Class<?> parameterType;
4041
private final Map<String, NodeHandler> nodeHandlerMap = new HashMap<>();
42+
private static final Map<String, SqlNode> emptyNodeCache = new ConcurrentHashMap<>();
4143

4244
public XMLScriptBuilder(Configuration configuration, XNode context) {
4345
this(configuration, context, null);
@@ -80,6 +82,10 @@ protected MixedSqlNode parseDynamicTags(XNode node) {
8082
XNode child = node.newXNode(children.item(i));
8183
if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
8284
String data = child.getStringBody("");
85+
if (data.trim().isEmpty()) {
86+
contents.add(emptyNodeCache.computeIfAbsent(data, EmptySqlNode::new));
87+
continue;
88+
}
8389
TextSqlNode textSqlNode = new TextSqlNode(data);
8490
if (textSqlNode.isDynamic()) {
8591
contents.add(textSqlNode);
@@ -248,4 +254,18 @@ private SqlNode getDefaultSqlNode(List<SqlNode> defaultSqlNodes) {
248254
}
249255
}
250256

257+
private static class EmptySqlNode implements SqlNode {
258+
private final String whitespaces;
259+
260+
public EmptySqlNode(String whitespaces) {
261+
super();
262+
this.whitespaces = whitespaces;
263+
}
264+
265+
@Override
266+
public boolean apply(DynamicContext context) {
267+
context.appendSql(whitespaces);
268+
return true;
269+
}
270+
}
251271
}

0 commit comments

Comments
 (0)