Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 4 additions & 16 deletions pkg/templates/compile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package templates

import (
"bytes"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -103,20 +102,9 @@ func updateRequestOptions(template *Template) {

// parseFromSource parses a template from source with caching support
func parseFromSource(filePath string, preprocessor Preprocessor, options *protocols.ExecutorOptions, parser *Parser) (*Template, error) {
var reader io.ReadCloser
if !options.DoNotCache {
_, raw, err := parser.parsedTemplatesCache.Has(filePath)
if err == nil && raw != nil {
reader = io.NopCloser(bytes.NewReader(raw))
}
}

var err error
if reader == nil {
reader, err = utils.ReaderFromPathOrURL(filePath, options.Catalog)
if err != nil {
return nil, err
}
reader, err := utils.ReaderFromPathOrURL(filePath, options.Catalog)
if err != nil {
return nil, err
}

defer func() {
Expand Down Expand Up @@ -158,7 +146,7 @@ func parseFromSource(filePath string, preprocessor Preprocessor, options *protoc

template.Path = filePath
if !options.DoNotCache {
parser.compiledTemplatesCache.Store(filePath, template, nil, err)
parser.compiledTemplatesCache.StoreWithoutRaw(filePath, template, err)
}

return template, nil
Expand Down
17 changes: 13 additions & 4 deletions pkg/templates/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ import (
type Parser struct {
ShouldValidate bool
NoStrictSyntax bool
// this cache can be copied safely between ephemeral instances

// parsedTemplatesCache stores lightweight parsed template structures
// (without raw bytes).
// Used for validation and filtering. This cache can be copied safely
// between ephemeral instances.
parsedTemplatesCache *Cache
// this cache might potentially contain references to heap objects
// it's recommended to always empty it at the end of execution

// compiledTemplatesCache stores fully compiled templates with all protocol
// requests.
// This cache contains references to heap objects and should be purged when
// no longer needed.
compiledTemplatesCache *Cache

sync.Mutex
}

Expand Down Expand Up @@ -179,7 +187,8 @@ func (p *Parser) ParseTemplate(templatePath string, catalog catalog.Catalog) (an
return nil, err
}

p.parsedTemplatesCache.Store(templatePath, template, nil, nil) // don't keep raw bytes to save memory
p.parsedTemplatesCache.StoreWithoutRaw(templatePath, template, nil)

return template, nil
}

Expand Down
Loading