Skip to content

Commit 74fbc24

Browse files
authored
perf(node): avoid unnecessary string allocation in Group.Accept (#548)
* perf(node): avoid unnecessary string allocation in Group.Accept * perf(node): avoid unnecessary string allocation in Group.Accept
1 parent 2c1e339 commit 74fbc24

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

node/node.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func (g Group) Accept(translator driver.Translator, p eval.Parameter) (query str
121121

122122
// Process each node in the group
123123
for i, node := range g {
124-
preLen := builder.Len()
125124
q, a, err := node.Accept(translator, p)
126125
if err != nil {
127126
return "", nil, err
@@ -133,10 +132,9 @@ func (g Group) Accept(translator driver.Translator, p eval.Parameter) (query str
133132

134133
// Add space between Nodes, but only if something was written
135134
// and it's not the last node and doesn't already end with space.
136-
if i < lastIdx && builder.Len() > preLen {
137-
if s := builder.String(); s[len(s)-1] != ' ' {
138-
builder.WriteByte(' ')
139-
}
135+
// Check q directly instead of builder.String() to avoid string allocation.
136+
if i < lastIdx && len(q) > 0 && q[len(q)-1] != ' ' {
137+
builder.WriteByte(' ')
140138
}
141139
}
142140

0 commit comments

Comments
 (0)