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

syntax: add benchmarks for scanning and parsing #238

Open
wants to merge 1 commit into
base: master
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
27 changes: 24 additions & 3 deletions starlark/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"go.starlark.net/starlark"
"go.starlark.net/starlarktest"
"go.starlark.net/syntax"
)

func Benchmark(b *testing.B) {
Expand Down Expand Up @@ -58,10 +59,11 @@ func Benchmark(b *testing.B) {
}

// BenchmarkProgram measures operations relevant to compiled programs.
// TODO(adonovan): use a bigger testdata program.
func BenchmarkProgram(b *testing.B) {
// Measure time to read a source file (approx 600us but depends on hardware and file system).
// TODO(adonovan): use a bigger testdata program.
filename := starlarktest.DataFile("starlark", "testdata/paths.star")

// Measure time to read a source file (approx 600us but depends on hardware and file system).
var src []byte
b.Run("read", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand All @@ -73,7 +75,26 @@ func BenchmarkProgram(b *testing.B) {
}
})

// Measure time to turn a source filename into a compiled program (approx 450us).
// Measure time to scan (approx 170us).
b.Run("scan", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := syntax.ScanAndDiscard(filename, src, 0); err != nil {
b.Fatal(err)
}
}
})

// Measure time to parse (approx 300us).
b.Run("parse", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := syntax.Parse(filename, src, 0); err != nil {
b.Fatal(err)
}
}
})

// Measure time to turn a source filename into a compiled program,
// that is, read + scan + parse + resolve + compile (approx 450us).
var prog *starlark.Program
b.Run("compile", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down
17 changes: 17 additions & 0 deletions syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ func Parse(filename string, src interface{}, mode Mode) (f *File, err error) {
return f, nil
}

// ScanAndDiscard tokenizes the input data and discards the tokens.
// Parameters are as for Parse.
// It exists only for internal benchmarking purposes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate this needs to be part of the external API for benchmarking. Could the "scan" benchmark be moved to the syntax package instead?

func ScanAndDiscard(filename string, src interface{}, mode Mode) error {
in, err := newScanner(filename, src, mode&RetainComments != 0)
if err != nil {
return err
}
p := parser{in: in}
defer p.in.recover(&err)
p.nextToken() // read first lookahead token
for p.tok != EOF {
p.nextToken()
}
return nil
}

// ParseCompoundStmt parses a single compound statement:
// a blank line, a def, for, while, or if statement, or a
// semicolon-separated list of simple statements followed
Expand Down