Skip to content

Commit

Permalink
Use bytes.Buffer for tomlLexer.buffer (pelletier#166)
Browse files Browse the repository at this point in the history
* Use bytes.Buffer for tomlLexer.buffer
* Add BenchmarkLexer

Fix pelletier#165

name     old time/op    new time/op    delta
Lexer-4     343µs ± 1%     331µs ± 1%  -3.56%  (p=0.000 n=20+19)

name     old alloc/op   new alloc/op   delta
Lexer-4    55.8kB ± 0%    50.8kB ± 0%  -8.86%  (p=0.000 n=20+20)

name     old allocs/op  new allocs/op  delta
Lexer-4     2.01k ± 0%     1.84k ± 0%  -8.46%  (p=0.000 n=20+20)
  • Loading branch information
n10v authored and pelletier committed May 30, 2017
1 parent 048765b commit 26ae43f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package toml

import (
"bytes"
"errors"
"fmt"
"io"
Expand All @@ -24,7 +25,7 @@ type tomlLexStateFn func() tomlLexStateFn
// Define lexer
type tomlLexer struct {
input *buffruneio.Reader // Textual source
buffer []rune // Runes composing the current token
buffer bytes.Buffer // Runes composing the current token
tokens chan token
depth int
line int
Expand Down Expand Up @@ -53,13 +54,13 @@ func (l *tomlLexer) next() rune {
r := l.read()

if r != eof {
l.buffer = append(l.buffer, r)
l.buffer.WriteRune(r)
}
return r
}

func (l *tomlLexer) ignore() {
l.buffer = make([]rune, 0)
l.buffer.Reset()
l.line = l.endbufferLine
l.col = l.endbufferCol
}
Expand All @@ -85,7 +86,7 @@ func (l *tomlLexer) emitWithValue(t tokenType, value string) {
}

func (l *tomlLexer) emit(t tokenType) {
l.emitWithValue(t, string(l.buffer))
l.emitWithValue(t, l.buffer.String())
}

func (l *tomlLexer) peek() rune {
Expand Down Expand Up @@ -536,7 +537,7 @@ func (l *tomlLexer) lexInsideTableArrayKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if len(l.buffer) > 0 {
if l.buffer.Len() > 0 {
l.emit(tokenKeyGroupArray)
}
l.next()
Expand All @@ -559,7 +560,7 @@ func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if len(l.buffer) > 0 {
if l.buffer.Len() > 0 {
l.emit(tokenKeyGroup)
}
l.next()
Expand Down
29 changes: 29 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package toml

import (
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -748,3 +749,31 @@ func TestLexUnknownRvalue(t *testing.T) {
{Position{1, 5}, tokenError, `no value can start with \`},
})
}

func BenchmarkLexer(b *testing.B) {
sample := `title = "Hugo: A Fast and Flexible Website Generator"
baseurl = "http://gohugo.io/"
MetaDataFormat = "yaml"
pluralizeListTitles = false
[params]
description = "Documentation of Hugo, a fast and flexible static site generator built with love by spf13, bep and friends in Go"
author = "Steve Francia (spf13) and friends"
release = "0.22-DEV"
[[menu.main]]
name = "Download Hugo"
pre = "<i class='fa fa-download'></i>"
url = "https://github.com/spf13/hugo/releases"
weight = -200
`
rd := strings.NewReader(sample)

b.ResetTimer()
for i := 0; i < b.N; i++ {
rd.Seek(0, os.SEEK_SET)
ch := lexToml(rd)
for _ = range ch {
}
}
}

0 comments on commit 26ae43f

Please sign in to comment.