Skip to content

Commit

Permalink
genopawasm: only unpack wasm and callgraph bytes once (open-policy-ag…
Browse files Browse the repository at this point in the history
…ent#3644)

The result will always be the same, and on multiple calls to
rego.New() with the wasm target, repeating this work can be
avoided.

A quick benchmark shows this in action, first main, then this
branch:

    BenchmarkWasmCompilation-16    	     133	   8345008 ns/op	 7521141 B/op	   28041 allocs/op
    BenchmarkWasmCompilation-16    	     256	   4529514 ns/op	 3666712 B/op	   27670 allocs/op

Once Go 1.17 is out and we can do something incompatible with
1.15, we can switch to using the go:embed directive instead.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus authored Jul 13, 2021
1 parent bac1755 commit 57b2a71
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
37 changes: 26 additions & 11 deletions internal/cmd/genopawasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,42 @@ import (
"bytes"
"compress/gzip"
"io/ioutil"
"sync"
)
var (
bytesOnce sync.Once
bs []byte
callGraphCSVOnce sync.Once
callGraphCSV []byte
)
// Bytes returns the OPA-WASM bytecode.
func Bytes() ([]byte, error) {
gr, err := gzip.NewReader(bytes.NewBuffer(gzipped))
if err != nil {
return nil, err
}
return ioutil.ReadAll(gr)
var err error
bytesOnce.Do(func() {
gr, err := gzip.NewReader(bytes.NewBuffer(gzipped))
if err != nil {
return
}
bs, err = ioutil.ReadAll(gr)
})
return bs, err
}
// CallGraphCSV returns a CSV representation of the
// OPA-WASM bytecode's call graph: 'caller,callee'
func CallGraphCSV() ([]byte, error) {
cg, err := gzip.NewReader(bytes.NewBuffer(gzippedCallGraphCSV))
if err != nil {
return nil, err
}
return ioutil.ReadAll(cg)
var err error
callGraphCSVOnce.Do(func() {
cg, err := gzip.NewReader(bytes.NewBuffer(gzippedCallGraphCSV))
if err != nil {
return
}
callGraphCSV, err = ioutil.ReadAll(cg)
})
return callGraphCSV, err
}
`))

if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/wasm/sdk/opa/opa_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ a = true`, "data.p.a = x")
}
}

func BenchmarkWasmCompilation(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compileRegoToWasm("a = true", "data.p.a = x", false)
}
}

func BenchmarkWASMArrayIteration(b *testing.B) {
sizes := []int{10, 100, 1000, 10000}
for _, n := range sizes {
Expand Down

0 comments on commit 57b2a71

Please sign in to comment.