Skip to content

Commit deda159

Browse files
authored
add benchmarks for CBOR decoding performance (#44)
1 parent cc9ec25 commit deda159

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

binary/cbor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func EncodeAsCbor(w io.Writer, e Term) error {
511511
// DecodeAsCbor decodes CBOR from the io.Reader and returns the resulting Expr
512512
func DecodeAsCbor(r io.Reader) (Term, error) {
513513
var b interface{}
514-
dm, err := cbor.DecOptions{}.DecMode()
514+
dm, err := cbor.DecOptions{MaxNestedLevels: 64}.DecMode()
515515
if err != nil {
516516
return nil, err
517517
}

binary/performance_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package binary_test
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"path"
8+
"testing"
9+
10+
"github.com/philandstuff/dhall-golang/v3/binary"
11+
"github.com/philandstuff/dhall-golang/v3/core"
12+
"github.com/philandstuff/dhall-golang/v3/imports"
13+
"github.com/philandstuff/dhall-golang/v3/internal"
14+
"github.com/philandstuff/dhall-golang/v3/term"
15+
)
16+
17+
func BenchmarkDecodeLargeExpression(b *testing.B) {
18+
file, err := os.Open("../dhall-lang/tests/parser/success/largeExpressionB.dhallb")
19+
if err != nil {
20+
b.Fatal(err)
21+
}
22+
defer file.Close()
23+
fileinfo, err := file.Stat()
24+
if err != nil {
25+
b.Fatal(err)
26+
}
27+
b.SetBytes(fileinfo.Size())
28+
for n := 0; n < b.N; n++ {
29+
file.Seek(0, io.SeekStart)
30+
binary.DecodeAsCbor(file)
31+
}
32+
}
33+
34+
func BenchmarkDecodePrelude(b *testing.B) {
35+
file := internal.NewLocalImport("../dhall-lang/Prelude/package.dhall", term.Code)
36+
t, err := imports.Load(file)
37+
if err != nil {
38+
b.Fatal(err)
39+
}
40+
v := core.Eval(t)
41+
cache, err := imports.StandardCache()
42+
if err != nil {
43+
b.Fatal(err)
44+
}
45+
hash, err := binary.SemanticHash(v)
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
// ensure Prelude is saved into the cache
50+
cache.Save(hash, core.Quote(v))
51+
52+
cacheDir, err := imports.DhallCacheDir()
53+
if err != nil {
54+
b.Fatal(err)
55+
}
56+
hash16 := fmt.Sprintf("%x", hash)
57+
cborfile, err := os.Open(path.Join(cacheDir, hash16))
58+
if err != nil {
59+
b.Fatal(err)
60+
}
61+
fileinfo, err := cborfile.Stat()
62+
if err != nil {
63+
b.Fatal(err)
64+
}
65+
b.SetBytes(fileinfo.Size())
66+
b.ResetTimer()
67+
for n := 0; n < b.N; n++ {
68+
cborfile.Seek(0, io.SeekStart)
69+
binary.DecodeAsCbor(cborfile)
70+
}
71+
}

imports/cache.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@ func (l LocalCache) Save(hash []byte, e term.Term) {
8484
// StandardCache is the standard DhallCache implementation. It is a
8585
// LocalCache in the standard Dhall cache directory.
8686
func StandardCache() (DhallCache, error) {
87-
cacheDir, err := dhallCacheDir()
87+
cacheDir, err := DhallCacheDir()
8888
if err != nil {
8989
return nil, err
9090
}
9191
return NewLocalCache(cacheDir), nil
9292
}
9393

94-
func dhallCacheDir() (string, error) {
94+
// DhallCacheDir returns the path to the Dhall cache directory, as
95+
// specified in the Dhall standard.
96+
func DhallCacheDir() (string, error) {
9597
cacheDir, err := os.UserCacheDir()
9698
if err != nil {
9799
return "", err

0 commit comments

Comments
 (0)