Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: maintain type cache per index (runtime)
Browse files Browse the repository at this point in the history
Not doing so results in crashes in upcoming
Go codec implementation. In general this
seems like a good idea.

Note that it may be better to do no caching at all,
and leave it up to the go codec to cache. But for
now we leave it until cuego is deprecated.

Also hide Runtime.Context.

Change-Id: I5179bc974503e78c8ae02f083e4b42a5ffb2cd39
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2709
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Aug 3, 2019
1 parent eac8f9a commit 48d8732
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
14 changes: 9 additions & 5 deletions cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/gob"
"path"
"strconv"
"sync"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
Expand All @@ -33,8 +34,8 @@ import (
// Any operation that involves two Values or Instances should originate from
// the same Runtime.
type Runtime struct {
Context *build.Context // TODO: remove
idx *index
ctx *build.Context // TODO: remove
idx *index
}

func dummyLoad(token.Pos, string) *build.Instance { return nil }
Expand Down Expand Up @@ -131,7 +132,7 @@ func (inst *Instance) MarshalBinary() (b []byte, err error) {
// name in position information. The source may import builtin packages. Use
// Build to allow importing non-builtin packages.
func (r *Runtime) Compile(filename string, source interface{}) (*Instance, error) {
ctx := r.Context
ctx := r.ctx
if ctx == nil {
ctx = build.NewContext()
}
Expand All @@ -145,7 +146,7 @@ func (r *Runtime) Compile(filename string, source interface{}) (*Instance, error
// CompileFile compiles the given source file into an Instance. The source may
// import builtin packages. Use Build to allow importing non-builtin packages.
func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
ctx := r.Context
ctx := r.ctx
if ctx == nil {
ctx = build.NewContext()
}
Expand All @@ -164,7 +165,7 @@ func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
// may import builtin packages. Use Build to allow importing non-builtin
// packages.
func (r *Runtime) CompileExpr(expr ast.Expr) (*Instance, error) {
ctx := r.Context
ctx := r.ctx
if ctx == nil {
ctx = build.NewContext()
}
Expand Down Expand Up @@ -250,6 +251,9 @@ type index struct {
offset label
parent *index
freeze bool

mutex sync.Mutex
typeCache sync.Map // map[reflect.Type]evaluated
}

const sharedOffset = 0x40000000
Expand Down
16 changes: 5 additions & 11 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"reflect"
"sort"
"strings"
"sync"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/parser"
Expand Down Expand Up @@ -381,16 +380,11 @@ func toUint(ctx *context, src source, x uint64) evaluated {
return n
}

var (
typeCache sync.Map // map[reflect.Type]evaluated
mutex sync.Mutex
)

func convertGoType(r *Runtime, t reflect.Type) value {
ctx := r.index().newContext()
// TODO: this can be much more efficient.
mutex.Lock()
defer mutex.Unlock()
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
return goTypeToValue(ctx, true, t)
}

Expand All @@ -405,7 +399,7 @@ var (
// TODO: if this value will always be unified with a concrete type in Go, then
// many of the fields may be omitted.
func goTypeToValue(ctx *context, allowNullDefault bool, t reflect.Type) (e value) {
if e, ok := typeCache.Load(t); ok {
if e, ok := ctx.typeCache.Load(t); ok {
return e.(value)
}

Expand Down Expand Up @@ -477,7 +471,7 @@ func goTypeToValue(ctx *context, allowNullDefault bool, t reflect.Type) (e value
// resolve field tags to allow field tags to refer to the struct fields.
tags := map[label]string{}
obj := newStruct(baseValue{})
typeCache.Store(t, obj)
ctx.typeCache.Store(t, obj)

for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
Expand Down Expand Up @@ -559,7 +553,7 @@ func goTypeToValue(ctx *context, allowNullDefault bool, t reflect.Type) (e value
store:
// TODO: store error if not nil?
if e != nil {
typeCache.Store(t, e)
ctx.typeCache.Store(t, e)
}
return e
}
Expand Down

0 comments on commit 48d8732

Please sign in to comment.