Skip to content

Commit

Permalink
feat: add cache in focus, env
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 8, 2023
1 parent 79ce0e2 commit 6979d88
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 44 additions & 12 deletions jsonata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -103,7 +109,6 @@ func Compile(str string, opts ...Options) (*Expression, error) {
return &Expression{
vm: vm,
value: exp.ToObject(vm),
mu: sync.Mutex{},
}, nil
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 6979d88

Please sign in to comment.