Commit a04d4f02 authored by Robert Griesemer's avatar Robert Griesemer

go/parser: ~15% faster parsing

- only compute current line position if needed
  (i.e., if a comment is present)

- added benchmark

benchmark         old ns/op    new ns/op    delta
BenchmarkParse     10902990      9313330  -14.58%

benchmark          old MB/s     new MB/s  speedup
BenchmarkParse         5.31         6.22    1.17x

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6270043
parent c9e698bd
......@@ -296,14 +296,14 @@ func (p *parser) consumeCommentGroup(n int) (comments *ast.CommentGroup, endline
func (p *parser) next() {
p.leadComment = nil
p.lineComment = nil
line := p.file.Line(p.pos) // current line
prev := p.pos
p.next0()
if p.tok == token.COMMENT {
var comment *ast.CommentGroup
var endline int
if p.file.Line(p.pos) == line {
if p.file.Line(p.pos) == p.file.Line(prev) {
// The comment is on same line as the previous token; it
// cannot be a lead comment but may be a line comment.
comment, endline = p.consumeCommentGroup(0)
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parser
import (
"go/token"
"io/ioutil"
"testing"
)
var src = readFile("parser.go")
func readFile(filename string) []byte {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return data
}
func BenchmarkParse(b *testing.B) {
b.SetBytes(int64(len(src)))
for i := 0; i < b.N; i++ {
if _, err := ParseFile(token.NewFileSet(), "", src, ParseComments); err != nil {
b.Fatalf("benchmark failed due to parse error: %s", err)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment