Use bytes.Buffer for tomlLexer.buffer (#166)

* Use bytes.Buffer for tomlLexer.buffer
* Add BenchmarkLexer

Fix #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)
This commit is contained in:
Albert Nigmatzianov
2017-05-30 19:27:36 +02:00
committed by Thomas Pelletier
parent 048765b449
commit 26ae43fdee
2 changed files with 36 additions and 6 deletions
+7 -6
View File
@@ -6,6 +6,7 @@
package toml
import (
"bytes"
"errors"
"fmt"
"io"
@@ -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
@@ -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
}
@@ -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 {
@@ -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()
@@ -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()