Skip to content

Commit

Permalink
internal/core/eval: add Stats counters
Browse files Browse the repository at this point in the history
Change-Id: I54ee7f30666a44285259f222fdc5a53ac95d1648
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6642
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 22, 2020
1 parent 7903529 commit 17ade83
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
34 changes: 34 additions & 0 deletions internal/core/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ package eval

import (
"fmt"
"html/template"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
Expand All @@ -34,6 +36,15 @@ import (
"cuelang.org/go/internal/core/debug"
)

// TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
//
// - Reuse work from previous cycles. For instance, if we can guarantee that a
// value is always correct for partial results, we can just process the arcs
// going from Partial to Finalized, without having to reevaluate the value.
//
// - Test closedness far more thoroughly.
//

func Evaluate(r adt.Runtime, v *adt.Vertex) {
format := func(n adt.Node) string {
return debug.NodeString(r, n, printConfig)
Expand All @@ -51,6 +62,25 @@ func New(r adt.Runtime) *Evaluator {
return &Evaluator{r: r, index: r}
}

type Stats struct {
DisjunctCount int
UnifyCount int
}

var stats = template.Must(template.New("stats").Parse(`{{"" -}}
Unifications: {{.UnifyCount}}
Disjuncts: {{.DisjunctCount}}`))

func (s *Stats) String() string {
buf := strings.Builder{}
_ = stats.Execute(&buf, s)
return buf.String()
}

func (e *Evaluator) Stats() *Stats {
return &e.stats
}

// TODO: Note: NewContext takes essentially a cue.Value. By making this
// type more central, we can perhaps avoid context creation.

Expand Down Expand Up @@ -83,6 +113,8 @@ type Evaluator struct {
r adt.Runtime
index adt.StringIndexer
closeID uint32

stats Stats
}

func (e *Evaluator) nextID() uint32 {
Expand Down Expand Up @@ -255,7 +287,9 @@ func (e *Evaluator) evalVertex(c *adt.OpContext, v *adt.Vertex, state adt.Vertex
}
saved := *v

e.stats.UnifyCount++
for i := 0; ; i++ {
e.stats.DisjunctCount++

// Clear any remaining error.
if err := c.Err(); err != nil {
Expand Down
20 changes: 9 additions & 11 deletions internal/core/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"testing"

"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/validate"
Expand Down Expand Up @@ -57,9 +56,12 @@ func TestEval(t *testing.T) {
t.Fatal(err)
}

ctx := eval.NewContext(r, v)
e := eval.New(r)
ctx := e.NewContext(v)
v.Finalize(ctx)

t.Log(e.Stats())

if b := validate.Validate(r, v, &validate.Config{
AllErrors: true,
}); b != nil {
Expand Down Expand Up @@ -89,9 +91,6 @@ var needFix = map[string]string{

"cycle/025_cannot_resolve_references_that_would_be_ambiguous": "cycle",
"compile/scope": "cycle",

"resolve/034_closing_structs": "close()",
"fulleval/053_issue312": "close()",
}

// TestX is for debugging. Do not delete.
Expand Down Expand Up @@ -121,12 +120,11 @@ module: "example.com"
}
t.Error(debug.NodeString(r, v, nil))

ctx := eval.NewContext(r, v)

ctx.Unify(ctx, v, adt.Finalized)
// if err != nil {
// t.Fatal(err)
// }
e := eval.New(r)
ctx := e.NewContext(v)
v.Finalize(ctx)

t.Error(debug.NodeString(r, v, nil))

t.Log(e.Stats())
}

0 comments on commit 17ade83

Please sign in to comment.