Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Decrease execution times by 1000% [DT-7499] #277

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf: Runs from ~14,000ms to ~1,400ms [DT-7499]
  • Loading branch information
nicolegros authored and jocgir committed Nov 26, 2024
commit cccad9506cdb5c1ce21f64939c88a4a929dd97bf
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"runtime/debug"
"strings"
"time"

"github.com/coveooss/gotemplate/v3/collections"
"github.com/coveooss/gotemplate/v3/hcl"
Expand Down Expand Up @@ -37,6 +38,7 @@ See: https://coveooss.github.io/gotemplate for complete documentation.
`

func runGotemplate() (exitCode int) {
start := time.Now()
defer func() {
if rec := recover(); rec != nil {
errPrintf(color.RedString("Recovered %v\n"), rec)
Expand Down Expand Up @@ -310,6 +312,8 @@ func runGotemplate() (exitCode int) {
if !*printOutput {
utils.TerraformFormat(resultFiles...)
}
duration := time.Since(start)
fmt.Println(fmt.Sprintf("Took %dms", duration.Milliseconds()))
return
}

Expand Down
13 changes: 6 additions & 7 deletions template/extra_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"reflect"
"strings"
"time"

"github.com/coveooss/gotemplate/v3/collections"
"github.com/coveooss/gotemplate/v3/utils"
Expand Down Expand Up @@ -430,16 +431,14 @@ func (t *Template) exec(command string, args ...interface{}) (interface{}, error
}

func (t *Template) runTemplate(source string, args ...interface{}) (result, filename string, err error) {
start := time.Now()

if source == "" {
return
}
var out bytes.Buffer

// Keep the parent context to make it available
parentContext := t.userContext()
// Clone the current context to ensure that the sub template has a distinct set of values
t = t.GetNewContext("", false)
context := t.Context().Clone()
context := t.context.(collections.IDictionary)
if context.Len() == 0 {
context.Set("CONTEXT", context)
}
Expand All @@ -454,8 +453,6 @@ func (t *Template) runTemplate(source string, args ...interface{}) (result, file
context.Set("ARGS", args)
}

// Make the parent context available
context.Set("_", parentContext)
t.context = context

// We first try to find a template named <source>
Expand Down Expand Up @@ -510,6 +507,8 @@ func (t *Template) runTemplate(source string, args ...interface{}) (result, file
// templating, In that case, we do not consider the original filename as unaltered source.
filename = ""
}
duration := time.Since(start)
fmt.Println(fmt.Sprintf("Took 'runTemplate()' %dms", duration.Milliseconds()))
return
}

Expand Down