Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] save inlinings #6981

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"
"time"

Expand Down Expand Up @@ -2366,6 +2367,8 @@ func (r *Rego) partial(ctx context.Context, ectx *EvalContext) (*PartialQueries,
unknowns = []*ast.Term{ast.NewTerm(ast.InputRootRef)}
}

tracer := topdown.NewBufferTracer()

q := topdown.NewQuery(ectx.compiledQuery.query).
WithQueryCompiler(ectx.compiledQuery.compiler).
WithCompiler(r.compiler).
Expand All @@ -2385,7 +2388,9 @@ func (r *Rego) partial(ctx context.Context, ectx *EvalContext) (*PartialQueries,
WithInterQueryBuiltinCache(ectx.interQueryBuiltinCache).
WithStrictBuiltinErrors(ectx.strictBuiltinErrors).
WithSeed(ectx.seed).
WithPrintHook(ectx.printHook)
WithPrintHook(ectx.printHook).
WithQueryTracer(tracer)


if !ectx.time.IsZero() {
q = q.WithTime(ectx.time)
Expand Down Expand Up @@ -2467,7 +2472,7 @@ func (r *Rego) partial(ctx context.Context, ectx *EvalContext) (*PartialQueries,
support[i].SetRegoVersion(r.regoVersion)
}
}

topdown.PrettyTraceWithLocation(os.Stdout, *tracer)
pq := &PartialQueries{
Queries: queries,
Support: support,
Expand Down
45 changes: 42 additions & 3 deletions topdown/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ type eval struct {
tracingOpts tracing.Options
findOne bool
strictObjects bool
inliningCacheList inliningCacheList
}

type inliningCache struct {
rule *ast.Rule
term *ast.Term
termbindings *bindings
}

type inliningCacheList struct {
list []inliningCache
}

func (e *eval) Run(iter evalIterator) error {
Expand Down Expand Up @@ -160,14 +171,17 @@ func (e *eval) closure(query ast.Body) *eval {
return &cpy
}

func (e *eval) child(query ast.Body) *eval {
func (e *eval) child(query ast.Body, a ...bool) *eval {
cpy := *e
cpy.index = 0
cpy.query = query
cpy.queryID = cpy.queryIDFact.Next()
cpy.bindings = newBindings(cpy.queryID, e.instr)
cpy.parent = e
cpy.findOne = false
if len(a) == 0 {
cpy.inliningCacheList = inliningCacheList{}
}
return &cpy
}

Expand Down Expand Up @@ -3003,7 +3017,11 @@ func (e evalVirtualComplete) eval(iter unifyIterator) error {
return e.partialEvalSupport(iter)
}

return e.partialEval(iter)
cpy := e
if e.e.parent != nil {
cpy.e = e.e.parent
}
return cpy.partialEval(iter)
}

func (e evalVirtualComplete) evalValue(iter unifyIterator, findOne bool) error {
Expand Down Expand Up @@ -3115,13 +3133,34 @@ func (e evalVirtualComplete) evalValueRule(iter unifyIterator, rule *ast.Rule, p
func (e evalVirtualComplete) partialEval(iter unifyIterator) error {

for _, rule := range e.ir.Rules {
found := false

for _ , cache := range e.e.inliningCacheList.list {
if cache.rule.Equal(rule) {
found = true
err := e.evalTerm(iter, cache.term, cache.termbindings)
if err != nil {
return err
}
}
}

if found {
fmt.Printf("skipped %s\n", rule.Ref().String())
continue
}

child := e.e.child(rule.Body)
child.traceEnter(rule)

err := child.eval(func(child *eval) error {
child.traceExit(rule)
term, termbindings := child.bindings.apply(rule.Head.Value)

e.e.inliningCacheList.list = append(e.e.inliningCacheList.list, inliningCache{
rule: rule,
term: term,
termbindings: termbindings,
})
err := e.evalTerm(iter, term, termbindings)
if err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions topdown/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,14 @@ func (q *Query) PartialRun(ctx context.Context) (partials []ast.Body, support []
inliningControl: &inliningControl{
shallow: q.shallowInlining,
},
genvarprefix: q.genvarprefix,
runtime: q.runtime,
indexing: q.indexing,
earlyExit: q.earlyExit,
builtinErrors: &builtinErrors{},
printHook: q.printHook,
strictObjects: q.strictObjects,
genvarprefix: q.genvarprefix,
runtime: q.runtime,
indexing: q.indexing,
earlyExit: q.earlyExit,
builtinErrors: &builtinErrors{},
printHook: q.printHook,
strictObjects: q.strictObjects,
inliningCacheList: inliningCacheList{},
}

if len(q.disableInlining) > 0 {
Expand Down Expand Up @@ -387,7 +388,6 @@ func (q *Query) PartialRun(ctx context.Context) (partials []ast.Body, support []
p := copypropagation.New(livevars).WithCompiler(q.compiler)

err = e.Run(func(e *eval) error {

// Build output from saved expressions.
body := ast.NewBody()

Expand Down Expand Up @@ -526,6 +526,7 @@ func (q *Query) Iter(ctx context.Context, iter func(QueryResult) error) error {
printHook: q.printHook,
tracingOpts: q.tracingOpts,
strictObjects: q.strictObjects,
inliningCacheList: inliningCacheList{},
}
e.caller = e
q.metrics.Timer(metrics.RegoQueryEval).Start()
Expand Down
Loading