From 8ff9db92e83237fd865764b05de287e457df859a Mon Sep 17 00:00:00 2001 From: Matthew Sackman Date: Tue, 5 Nov 2024 14:08:22 +0000 Subject: [PATCH] internal/core: expose topological sorting API 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 Change-Id: If3cbf748898bf87d1ced7ec014bdf9553a34afe5 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1203603 TryBot-Result: CUEcueckoo Reviewed-by: Marcel van Lohuizen --- cue/cuecontext/cuecontext_test.go | 4 +++- internal/core/adt/context.go | 14 ++++++++------ internal/core/eval/eval.go | 2 -- internal/core/export/toposort.go | 3 +-- internal/core/runtime/runtime.go | 17 ++++++++++++++++- internal/core/toposort/sort_test.go | 8 +------- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/cue/cuecontext/cuecontext_test.go b/cue/cuecontext/cuecontext_test.go index 144e34d3e5d..6fd85a70409 100644 --- a/cue/cuecontext/cuecontext_test.go +++ b/cue/cuecontext/cuecontext_test.go @@ -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" ) @@ -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) } diff --git a/internal/core/adt/context.go b/internal/core/adt/context.go index f9e1e132f4e..9be16e766df 100644 --- a/internal/core/adt/context.go +++ b/internal/core/adt/context.go @@ -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 { @@ -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} } @@ -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 diff --git a/internal/core/eval/eval.go b/internal/core/eval/eval.go index dc0f9bf4004..6c486d125d0 100644 --- a/internal/core/eval/eval.go +++ b/internal/core/eval/eval.go @@ -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} } diff --git a/internal/core/export/toposort.go b/internal/core/export/toposort.go index 78a66b33ed3..9550a946f9f 100644 --- a/internal/core/export/toposort.go +++ b/internal/core/export/toposort.go @@ -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 @@ -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) diff --git a/internal/core/runtime/runtime.go b/internal/core/runtime/runtime.go index 43dcbefbae4..3724a027ef4 100644 --- a/internal/core/runtime/runtime.go +++ b/internal/core/runtime/runtime.go @@ -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" ) @@ -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 } @@ -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 } @@ -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) { @@ -103,4 +117,5 @@ func (r *Runtime) Init() { } else { r.version = internal.DefaultVersion } + r.topoSort = cueexperiment.Flags.TopoSort } diff --git a/internal/core/toposort/sort_test.go b/internal/core/toposort/sort_test.go index 2bbf7c9690c..c82c25696a7 100644 --- a/internal/core/toposort/sort_test.go +++ b/internal/core/toposort/sort_test.go @@ -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", @@ -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)