Skip to content

Commit 44bdc9b

Browse files
author
Rob Figueiredo
committed
soyhtml/exec: hoist implicit "for loop" key creation out of loop
Also hoist the __lastIndex, which is invariant throughout the loop. This avoids unnecessarily creating garbage with each loop iteration.
1 parent 51cb83d commit 44bdc9b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

soyhtml/exec.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,15 @@ func (s *state) walk(node ast.Node) {
144144
break
145145
}
146146
s.context.push()
147+
var (
148+
keyVar = node.Var
149+
keyInd = node.Var + "__index"
150+
keyLast = node.Var + "__lastIndex"
151+
)
152+
s.context.set(keyLast, data.Int(len(list)-1))
147153
for i, item := range list {
148-
s.context.set(node.Var, item)
149-
s.context.set(node.Var+"__index", data.Int(i))
150-
s.context.set(node.Var+"__lastIndex", data.Int(len(list)-1))
154+
s.context.set(keyVar, item)
155+
s.context.set(keyInd, data.Int(i))
151156
s.walk(node.Body)
152157
}
153158
s.context.pop()

0 commit comments

Comments
 (0)