Skip to content

Commit

Permalink
soyhtml/exec: hoist implicit "for loop" key creation out of loop
Browse files Browse the repository at this point in the history
Also hoist the __lastIndex, which is invariant throughout the loop.

This avoids unnecessarily creating garbage with each loop iteration.
  • Loading branch information
Rob Figueiredo committed Apr 6, 2022
1 parent 51cb83d commit 44bdc9b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions soyhtml/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,15 @@ func (s *state) walk(node ast.Node) {
break
}
s.context.push()
var (
keyVar = node.Var
keyInd = node.Var + "__index"
keyLast = node.Var + "__lastIndex"
)
s.context.set(keyLast, data.Int(len(list)-1))
for i, item := range list {
s.context.set(node.Var, item)
s.context.set(node.Var+"__index", data.Int(i))
s.context.set(node.Var+"__lastIndex", data.Int(len(list)-1))
s.context.set(keyVar, item)
s.context.set(keyInd, data.Int(i))
s.walk(node.Body)
}
s.context.pop()
Expand Down

0 comments on commit 44bdc9b

Please sign in to comment.