Skip to content

Commit

Permalink
Reduce memory allocations
Browse files Browse the repository at this point in the history
Instead of allocating new maps, the existing ones can be reused

Signed-off-by: Philipp Gille <[email protected]>
  • Loading branch information
philippgille committed Aug 26, 2021
1 parent 419b91d commit 2e47524
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions sdk/interpolate/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func Do(input string, vars map[string]string) (string, error) {
var processedExpression = map[string]void{}
sm := interpolateRegex.FindAllStringSubmatch(input, -1)
if len(sm) > 0 {
var usedVariables = make(map[string]void, len(vars))
var usedHelpers = make(map[string]void, len(InterpolateHelperFuncs))
for i := 0; i < len(sm); i++ {
if len(sm[i]) > 0 {
var expression = strings.TrimSpace(sm[i][1])
Expand All @@ -92,8 +94,6 @@ func Do(input string, vars map[string]string) (string, error) {
}
processedExpression[expression] = void{}

var usedVariables = make(map[string]void, len(vars))
var usedHelpers = make(map[string]void, len(InterpolateHelperFuncs))
var quotedStuff = []string{}
var trimmedExpression = strings.TrimPrefix(expression, "{{")
trimmedExpression = strings.TrimSuffix(trimmedExpression, "}}")
Expand Down Expand Up @@ -125,23 +125,25 @@ func Do(input string, vars map[string]string) (string, error) {
}
}

var defaultIsUsed bool
if _, ok := usedHelpers["default"]; ok {
defaultIsUsed = true
}

unknownVariables := make([]string, 0, 1000)
for v := range usedVariables {
if _, is := flatData[v]; !is {
unknownVariables = append(unknownVariables, v)
}
delete(usedVariables, v)
}

unknownHelpers := make([]string, 0, 1000)
for h := range usedHelpers {
if _, is := InterpolateHelperFuncs[h]; !is {
unknownHelpers = append(unknownHelpers, h)
}
}

var defaultIsUsed bool
if _, ok := usedHelpers["default"]; ok {
defaultIsUsed = true
delete(usedHelpers, h)
}

if !defaultIsUsed && (len(unknownVariables) > 0 || len(unknownHelpers) > 0) {
Expand Down

0 comments on commit 2e47524

Please sign in to comment.