From 44bdc9bae81322204c800d6ed8b5dc84726b1ae5 Mon Sep 17 00:00:00 2001 From: Rob Figueiredo Date: Wed, 6 Apr 2022 17:05:01 -0400 Subject: [PATCH] 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. --- soyhtml/exec.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/soyhtml/exec.go b/soyhtml/exec.go index eab0ce8..47e96a6 100644 --- a/soyhtml/exec.go +++ b/soyhtml/exec.go @@ -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()