Skip to content

Commit

Permalink
Use new setting to cache AST conversions from store
Browse files Browse the repository at this point in the history
This doesn't have a huge impact on `regal lint` (although it does
result in less allocations), but could potentially make a real difference
for the LSP policies where ASTs from the whole workspace is provided under
`data.workspace`.

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Nov 12, 2024
1 parent 82da7a9 commit 2a7b2aa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/lsp/completions/providers/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/topdown"

rbundle "github.com/styrainc/regal/bundle"
rio "github.com/styrainc/regal/internal/io"
Expand Down Expand Up @@ -139,13 +138,14 @@ func prepareQuery(ctx context.Context, store storage.Store, query string) (*rego

func prepareRegoArgs(store storage.Store, query ast.Body) []func(*rego.Rego) {
return []func(*rego.Rego){
rego.StoreReadAST(true),
rego.Store(store),
rego.ParsedQuery(query),
rego.ParsedBundle("regal", &rbundle.LoadedBundle),
rego.Function2(builtins.RegalParseModuleMeta, builtins.RegalParseModule),
rego.Function1(builtins.RegalLastMeta, builtins.RegalLast),
// TODO: remove later
rego.EnablePrintStatements(true),
rego.PrintHook(topdown.NewPrintHook(os.Stderr)),
// Uncomment for development
// rego.EnablePrintStatements(true),
// rego.PrintHook(topdown.NewPrintHook(os.Stderr)),
}
}
1 change: 1 addition & 0 deletions internal/lsp/rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func initialize() {
rego.ParsedBundle("regal", &rbundle.LoadedBundle),
rego.Function2(builtins.RegalParseModuleMeta, builtins.RegalParseModule),
rego.Function1(builtins.RegalLastMeta, builtins.RegalLast),
rego.StoreReadAST(true),
}, args...)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ func (l Linter) prepareRegoArgs(query ast.Body) ([]func(*rego.Rego), error) {
}

regoArgs = append(regoArgs,
rego.StoreReadAST(true),
rego.Metrics(l.metrics),
rego.ParsedQuery(query),
rego.ParsedBundle("regal_eval_params", &dataBundle),
Expand Down
14 changes: 14 additions & 0 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,17 @@ import data.unresolved`,
t.Fatalf("unexpected files: %v", got)
}
}

func BenchmarkRegalLintingItself(b *testing.B) {
linter := NewLinter().WithInputPaths([]string{"../../bundle"}).WithEnableAll(true)

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_, err := linter.Lint(context.Background())
if err != nil {
b.Fatal(err)
}
}
}

0 comments on commit 2a7b2aa

Please sign in to comment.