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

Commit

Permalink
internal/core/runtime: decouple cue API from Runtime
Browse files Browse the repository at this point in the history
The main goal here is to decouple the caching, and
that to the cue.Runtime. That will ultimately make it
easier to not cache Instances, which can hog memory
allocation.

Change-Id: I0cf9678b867e6afd18d1e5abb6961702e37170c9
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8222
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 16, 2021
1 parent bcd752a commit e1e7031
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
20 changes: 7 additions & 13 deletions cue/build.go
Expand Up @@ -193,33 +193,27 @@ func (r *Runtime) FromExpr(expr ast.Expr) (*Instance, error) {
})
}

type importIndex map[*build.Instance]*Instance

// index maps conversions from label names to internal codes.
//
// All instances belonging to the same package should share this index.
type index struct {
*runtime.Runtime
loaded importIndex
loaded map[*build.Instance]*Instance
}

// NewRuntime creates a *runtime.Runtime with builtins preloaded.
func NewRuntime() *runtime.Runtime {
r := runtime.New()
i := &index{
Runtime: r,
loaded: importIndex{},
}
r.Data = i
return r
i := newIndex()
i.Runtime.Data = i
return i.Runtime
}

// newIndex creates a new index.
func newIndex() *index {
r := runtime.New()
i := &index{
Runtime: r,
loaded: importIndex{},
loaded: map[*build.Instance]*Instance{},
}
r.Data = i
return i
Expand All @@ -230,6 +224,6 @@ func isBuiltin(s string) bool {
}

func (idx *index) loadInstance(p *build.Instance) *Instance {
idx.Runtime.Build(p)
return idx.getImportFromBuild(p)
v, _ := idx.Runtime.Build(p)
return idx.getImportFromBuild(p, v)
}
9 changes: 2 additions & 7 deletions cue/instance.go
Expand Up @@ -59,18 +59,13 @@ func (x *index) addInst(p *Instance) *Instance {
return p
}

func (x *index) getImportFromBuild(p *build.Instance) *Instance {
func (x *index) getImportFromBuild(p *build.Instance, v *adt.Vertex) *Instance {
inst := x.loaded[p]

if inst != nil {
return inst
}

v := x.GetNodeFromInstance(p)
if v == nil {
return nil
}

inst = &Instance{
ImportPath: p.ImportPath,
Dir: p.Dir,
Expand All @@ -95,7 +90,7 @@ func (x *index) getImportFromNode(v *adt.Vertex) *Instance {
return nil
}

return x.getImportFromBuild(p)
return x.getImportFromBuild(p, v)
}

func (x *index) getImportFromPath(id string) *Instance {
Expand Down
2 changes: 1 addition & 1 deletion cue/types.go
Expand Up @@ -1857,7 +1857,7 @@ func (v Value) instance() *Instance {
if v.v == nil {
return nil
}
return v.ctx().getImportFromNode(v.v)
return v.idx.getImportFromNode(v.v)
}

// Reference returns the instance and path referred to by this value such that
Expand Down
2 changes: 1 addition & 1 deletion internal/core/runtime/build.go
Expand Up @@ -28,7 +28,7 @@ import (
// Build builds b and all its transitive dependencies, insofar they have not
// been build yet.
func (x *Runtime) Build(b *build.Instance) (v *adt.Vertex, errs errors.Error) {
if v := x.GetNodeFromInstance(b); v != nil {
if v := x.getNodeFromInstance(b); v != nil {
return v, b.Err
}
// TODO: clear cache of old implementation.
Expand Down
2 changes: 1 addition & 1 deletion internal/core/runtime/imports.go
Expand Up @@ -95,7 +95,7 @@ func (r *Runtime) GetInstanceFromNode(key *adt.Vertex) *build.Instance {
return r.index.imports[key]
}

func (r *Runtime) GetNodeFromInstance(key *build.Instance) *adt.Vertex {
func (r *Runtime) getNodeFromInstance(key *build.Instance) *adt.Vertex {
return r.index.importsByBuild[key]
}

Expand Down

0 comments on commit e1e7031

Please sign in to comment.