Skip to content

Commit

Permalink
improve underlying lexer performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ysugimoto committed Oct 19, 2024
1 parent d535a57 commit 8abec01
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 116 deletions.
107 changes: 3 additions & 104 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func New(r io.Reader, opts ...OptionFunc) *Lexer {
r: bufio.NewReader(r),
line: 1,
buffer: new(bytes.Buffer),
stack: make([]string, 0, 512),
peeks: make([]token.Token, 0, 8),
file: o.Filename,
customs: o.Customs,
}
Expand Down Expand Up @@ -416,111 +418,8 @@ func (l *Lexer) skipWhitespace() {
}
}

func (l *Lexer) readString() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

l.readChar()
for {
if l.char == '"' || l.char == 0x00 {
break
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readBracketString() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

l.readChar()
for {
if l.char == 0x00 {
break
}
if l.char == '"' {
if l.peekChar() == '}' {
l.readChar()
break
}
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readNumber() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for isDigit(l.char) {
buf.WriteRune(l.char)
l.readChar()
}
return buf.String()
}

func (l *Lexer) readEOL() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for {
buf.WriteRune(l.char)
if l.peekChar() == 0x00 || l.peekChar() == '\n' {
break
}
l.readChar()
}
return buf.String()
}

func (l *Lexer) readMultiComment() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for {
if l.char == 0x00 {
break
}
if l.char == '*' && l.peekChar() == '/' {
buf.WriteRune(l.char)
l.readChar()
buf.WriteRune(l.char)
break
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readIdentifier() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for l.isLetter(l.char) {
buf.WriteRune(l.char)
l.readChar()
}
return buf.String()
}

func (l *Lexer) isLetter(r rune) bool {
// Letter allows "-", "." and ":" character to parse ident http header name like `req.http.X-Forwarded-For`
// but in some name definitions (e.g. acl name, backend name), parser must check "-" IS NOT included.
// return 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || r == '_' || r == '-' || r == '.' || r == ':'
// Letter allows [a-zA-Z_] character to parse ident of http header name like `req`, `http`, `X-Forwarded-For`.
return r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r == '_'
}

Expand Down
12 changes: 0 additions & 12 deletions lexer/pool.go

This file was deleted.

113 changes: 113 additions & 0 deletions lexer/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package lexer

import (
"bytes"
"sync"
)

var pool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}

func (l *Lexer) readString() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck

Check failure on line 15 in lexer/reader.go

View workflow job for this annotation

GitHub Actions / build

Error return value is not checked (errcheck)
defer pool.Put(buf)
buf.Reset()

l.readChar()
for {
if l.char == '"' || l.char == 0x00 {
break
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readBracketString() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck

Check failure on line 32 in lexer/reader.go

View workflow job for this annotation

GitHub Actions / build

Error return value is not checked (errcheck)
defer pool.Put(buf)
buf.Reset()

l.readChar()
for {
if l.char == 0x00 {
break
}
if l.char == '"' {
if l.peekChar() == '}' {
l.readChar()
break
}
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readNumber() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck

Check failure on line 55 in lexer/reader.go

View workflow job for this annotation

GitHub Actions / build

Error return value is not checked (errcheck)
defer pool.Put(buf)
buf.Reset()

for isDigit(l.char) {
buf.WriteRune(l.char)
l.readChar()
}
return buf.String()
}

func (l *Lexer) readEOL() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for {
buf.WriteRune(l.char)
if l.peekChar() == 0x00 || l.peekChar() == '\n' {
break
}
l.readChar()
}
return buf.String()
}

func (l *Lexer) readMultiComment() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for {
if l.char == 0x00 {
break
}
if l.char == '*' && l.peekChar() == '/' {
buf.WriteRune(l.char)
l.readChar()
buf.WriteRune(l.char)
break
}
buf.WriteRune(l.char)
l.readChar()
}

return buf.String()
}

func (l *Lexer) readIdentifier() string {
buf := pool.Get().(*bytes.Buffer) // notlint:errcheck
defer pool.Put(buf)
buf.Reset()

for l.isLetter(l.char) {
buf.WriteRune(l.char)
l.readChar()
}
return buf.String()
}

0 comments on commit 8abec01

Please sign in to comment.