mirror of
https://github.com/golang/go
synced 2024-11-25 19:27:56 -07:00
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
This commit is contained in:
parent
c9e698bdfb
commit
a04d4f02a4
@ -296,14 +296,14 @@ func (p *parser) consumeCommentGroup(n int) (comments *ast.CommentGroup, endline
|
|||||||
func (p *parser) next() {
|
func (p *parser) next() {
|
||||||
p.leadComment = nil
|
p.leadComment = nil
|
||||||
p.lineComment = nil
|
p.lineComment = nil
|
||||||
line := p.file.Line(p.pos) // current line
|
prev := p.pos
|
||||||
p.next0()
|
p.next0()
|
||||||
|
|
||||||
if p.tok == token.COMMENT {
|
if p.tok == token.COMMENT {
|
||||||
var comment *ast.CommentGroup
|
var comment *ast.CommentGroup
|
||||||
var endline int
|
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
|
// The comment is on same line as the previous token; it
|
||||||
// cannot be a lead comment but may be a line comment.
|
// cannot be a lead comment but may be a line comment.
|
||||||
comment, endline = p.consumeCommentGroup(0)
|
comment, endline = p.consumeCommentGroup(0)
|
||||||
|
30
src/pkg/go/parser/performance_test.go
Normal file
30
src/pkg/go/parser/performance_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user