From 6979d88440131da2de1624cc26c6a45c7a6acad1 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Sun, 8 Oct 2023 01:10:27 -0400 Subject: [PATCH] feat: add cache in focus, env --- README.md | 12 ++++++------ jsonata.go | 56 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c19b1e2..01f1e5c 100644 --- a/README.md +++ b/README.md @@ -41,13 +41,13 @@ goos: linux goarch: amd64 pkg: github.com/siyul-park/jsonata-go cpu: AMD EPYC 7282 16-Core Processor -BenchmarkExpression_Compile-4 241 4289580 ns/op 1533227 B/op 19943 allocs/op -BenchmarkExpression_Evaluate-4 4311 238447 ns/op 62422 B/op 872 allocs/op -BenchmarkExpression_Assign-4 627997 1706 ns/op 522 B/op 9 allocs/op -BenchmarkExpression_RegisterFunction-4 296378 4030 ns/op 1845 B/op 23 allocs/op -BenchmarkExpression_Ast-4 15912 76499 ns/op 29610 B/op 413 allocs/op +BenchmarkExpression_Compile-4 207 5672824 ns/op 1533080 B/op 19941 allocs/op +BenchmarkExpression_Evaluate-4 3902 260193 ns/op 62461 B/op 873 allocs/op +BenchmarkExpression_Assign-4 822211 1542 ns/op 521 B/op 9 allocs/op +BenchmarkExpression_RegisterFunction-4 243468 4395 ns/op 1846 B/op 23 allocs/op +BenchmarkExpression_Ast-4 14838 80954 ns/op 29617 B/op 413 allocs/op PASS -ok github.com/siyul-park/jsonata-go 6.962s +ok github.com/siyul-park/jsonata-go 8.251s ``` ## More information diff --git a/jsonata.go b/jsonata.go index 2c05f27..39247e8 100644 --- a/jsonata.go +++ b/jsonata.go @@ -32,13 +32,19 @@ type ( } Environment struct { - vm *goja.Runtime - value *goja.Object + async bool + timestamp time.Time + vm *goja.Runtime + value *goja.Object + mu sync.Mutex } Focus struct { - vm *goja.Runtime - value *goja.Object + environment *Environment + input any + vm *goja.Runtime + value *goja.Object + mu sync.Mutex } Expression struct { @@ -103,7 +109,6 @@ func Compile(str string, opts ...Options) (*Expression, error) { return &Expression{ vm: vm, value: exp.ToObject(vm), - mu: sync.Mutex{}, }, nil } } @@ -181,22 +186,49 @@ func (e *Expression) Ast() (*ExprNode, error) { } func (f *Focus) Environment() *Environment { - return &Environment{ - value: f.vm.Get("environment").ToObject(f.vm), - vm: f.vm, + f.mu.Lock() + defer f.mu.Unlock() + + if f.environment == nil { + f.environment = &Environment{ + value: f.vm.Get("environment").ToObject(f.vm), + vm: f.vm, + } } + + return f.environment } func (f *Focus) Input() any { - return f.vm.Get("input").Export() + f.mu.Lock() + defer f.mu.Unlock() + + if f.input == nil { + f.input = f.vm.Get("input").Export() + } + return f.input } func (e *Environment) Async() bool { - return e.value.Get("async").ToBoolean() + e.mu.Lock() + defer e.mu.Unlock() + + if !e.async { + e.async = e.value.Get("async").ToBoolean() + } + + return e.async } func (e *Environment) Timestamp() time.Time { - return e.value.Get("timestamp").Export().(time.Time) + e.mu.Lock() + defer e.mu.Unlock() + + if e.timestamp == (time.Time{}) { + e.timestamp = e.value.Get("timestamp").Export().(time.Time) + } + + return e.timestamp } func (e *Environment) Bind(s string, a any) error { @@ -205,7 +237,7 @@ func (e *Environment) Bind(s string, a any) error { return err } -func (e Environment) Lookup(s string) (any, error) { +func (e *Environment) Lookup(s string) (any, error) { lookup, _ := goja.AssertFunction(e.value.Get("lookup")) if v, err := lookup(goja.Undefined(), e.vm.ToValue(s)); err != nil { return nil, err