Skip to content

Commit

Permalink
internal/core: expose topological sorting API
Browse files Browse the repository at this point in the history
Previously, the new topological sorting could only be turned on and off
through the CUE_EXPERIMENT env var.

Add topoSort field to Runtime and OpContext, and populate initially via
CUE_EXPERIMENT. Change export so it looks at OpContext.TopoSort instead
of CUE_EXPERIMENT.

This makes topological sorting configurable in the same way as evaluator
version.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: If3cbf748898bf87d1ced7ec014bdf9553a34afe5
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1203603
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
cuematthew committed Nov 5, 2024
1 parent 1476fc7 commit 8ff9db9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
4 changes: 3 additions & 1 deletion cue/cuecontext/cuecontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cueexperiment"
)
Expand Down Expand Up @@ -82,7 +83,8 @@ func TestEvalVersion(t *testing.T) {
defer func() { cueexperiment.Flags.EvalV3 = saved }()

test := func(c *cue.Context, want internal.EvaluatorVersion) {
got, _ := (*runtime.Runtime)(c).Settings()
opCtx := adt.NewContext((*runtime.Runtime)(c), nil)
got := opCtx.Version
if got != want {
t.Errorf("got %v; want %v", got, want)
}
Expand Down
14 changes: 8 additions & 6 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ type Runtime interface {
// type if available.
LoadType(t reflect.Type) (src ast.Expr, expr Expr, ok bool)

Settings() (internal.EvaluatorVersion, cuedebug.Config)
// ConfigureOpCtx configures the [*OpContext] with details such as
// evaluator version, debug options etc.
ConfigureOpCtx(ctx *OpContext)
}

type Config struct {
Expand All @@ -180,16 +182,15 @@ func New(v *Vertex, cfg *Config) *OpContext {
if cfg.Runtime == nil {
panic("nil Runtime")
}
version, flags := cfg.Runtime.Settings()

ctx := &OpContext{
Runtime: cfg.Runtime,
Format: cfg.Format,
vertex: v,
Version: version,
stats: stats.Counts{EvalVersion: version},
Config: flags,
taskContext: schedConfig,
}
cfg.Runtime.ConfigureOpCtx(ctx)
ctx.stats.EvalVersion = ctx.Version
if v != nil {
ctx.e = &Environment{Up: nil, Vertex: v}
}
Expand All @@ -213,7 +214,8 @@ type OpContext struct {
Format func(Runtime, Node) string

cuedebug.Config
Version internal.EvaluatorVersion // Copied from Runtime
Version internal.EvaluatorVersion // Copied from Runtime
TopoSort bool // Copied from Runtime

taskContext

Expand Down
2 changes: 0 additions & 2 deletions internal/core/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func Evaluate(r adt.Runtime, v *adt.Vertex) {

func New(r adt.Runtime) *Unifier {
ctx := NewContext(r, nil)
// TODO: we could access these directly if we can use runtime.Runtime directly.
ctx.Version, ctx.Config = r.Settings()
return &Unifier{r: r, e: ctx}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/core/export/toposort.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/toposort"
"cuelang.org/go/internal/cueexperiment"
)

// TODO: topological sort should go arguably in a more fundamental place as it
Expand All @@ -30,7 +29,7 @@ import (
// features than for which there are arcs and also includes features for
// optional fields. It assumes the Structs fields are initialized and evaluated.
func VertexFeatures(c *adt.OpContext, v *adt.Vertex) []adt.Feature {
if cueexperiment.Flags.TopoSort {
if c.TopoSort {
return toposort.VertexFeatures(c, v)
} else {
return vertexFeatures(c, v)
Expand Down
17 changes: 16 additions & 1 deletion internal/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package runtime
import (
"cuelang.org/go/cue/build"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/cuedebug"
"cuelang.org/go/internal/cueexperiment"
)
Expand All @@ -31,7 +32,8 @@ type Runtime struct {
// the kind in a file-level @extern(kind) attribute.
interpreters map[string]Interpreter

version internal.EvaluatorVersion
version internal.EvaluatorVersion
topoSort bool

flags cuedebug.Config
}
Expand All @@ -40,6 +42,12 @@ func (r *Runtime) Settings() (internal.EvaluatorVersion, cuedebug.Config) {
return r.version, r.flags
}

func (r *Runtime) ConfigureOpCtx(ctx *adt.OpContext) {
ctx.Version = r.version
ctx.TopoSort = r.topoSort
ctx.Config = r.flags
}

func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) {
r.loaded[b] = x
}
Expand Down Expand Up @@ -73,6 +81,12 @@ func (r *Runtime) SetVersion(v internal.EvaluatorVersion) {
r.version = v
}

// SetTopologicalSort sets whether or not to use topological sorting
// for the Runtime.
func (r *Runtime) SetTopologicalSort(b bool) {
r.topoSort = b
}

// SetDebugOptions sets the debug flags to use for the Runtime. This should only
// be set before first use.
func (r *Runtime) SetDebugOptions(flags *cuedebug.Config) {
Expand Down Expand Up @@ -103,4 +117,5 @@ func (r *Runtime) Init() {
} else {
r.version = internal.DefaultVersion
}
r.topoSort = cueexperiment.Flags.TopoSort
}
8 changes: 1 addition & 7 deletions internal/core/toposort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ import (
"cuelang.org/go/cue/format"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/export"
"cuelang.org/go/internal/cueexperiment"
"cuelang.org/go/internal/cuetdtest"
"cuelang.org/go/internal/cuetxtar"
)

func TestTopologicalSort(t *testing.T) {
cueexperiment.Init()
saved := cueexperiment.Flags.TopoSort
defer func() { cueexperiment.Flags.TopoSort = saved }()

cueexperiment.Flags.TopoSort = true

test := cuetxtar.TxTarTest{
Root: "testdata",
Name: "toposort",
Expand All @@ -41,6 +34,7 @@ func TestTopologicalSort(t *testing.T) {

test.Run(t, func(t *cuetxtar.Test) {
run := t.Runtime()
run.SetTopologicalSort(true)
inst := t.Instance()

v, err := run.Build(nil, inst)
Expand Down

0 comments on commit 8ff9db9

Please sign in to comment.