Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pooled version of lexer.Upgrade #289

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lexer/peek.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lexer

import "sync"

// PeekingLexer supports arbitrary lookahead as well as cloning.
type PeekingLexer struct {
Checkpoint
Expand All @@ -26,6 +28,10 @@ func Upgrade(lex Lexer, elide ...TokenType) (*PeekingLexer, error) {
r := &PeekingLexer{
elide: make(map[TokenType]bool, len(elide)),
}
return fillInPeekingLexer(lex, r, elide...)
}

func fillInPeekingLexer(lex Lexer, r *PeekingLexer, elide ...TokenType) (*PeekingLexer, error) {
for _, rn := range elide {
r.elide[rn] = true
}
Expand All @@ -43,6 +49,43 @@ func Upgrade(lex Lexer, elide ...TokenType) (*PeekingLexer, error) {
return r, nil
}

var peekingLexerPool = sync.Pool{
New: func() interface{} {
return &PeekingLexer{
elide: make(map[TokenType]bool, 4),
}
},
}

// UpgradePooled will upgrade a Lexer to a PeekingLexer with arbitrary
// lookahead. Faster if you need to lex thousands of similar documents.
//
// "elide" is a slice of token types to elide from processing.
//
// You must call `PutBackPooledPeekingLexer` once done with the
// returned lexer in all cases (ok or error). If you use the lexer with
// the parser (`Parser.ParseFromLexer`), note that the parsed results
// might refer back to lexer tokens, in which case you should not call
// PutBackPooledPeekingLexer until you have finished with the parser
// results as well.
func UpgradePooled(lex Lexer, elide ...TokenType) (*PeekingLexer, error) {
r := peekingLexerPool.Get().(*PeekingLexer)
// reset the state of the PeekingLexer to empty (preserving any allocated capacity)
r.Checkpoint = Checkpoint{}
// note: this preserves capacity
r.tokens = r.tokens[:0]
for k := range r.elide {
delete(r.elide, k)
}
return fillInPeekingLexer(lex, r, elide...)
}

func PutBackPooledPeekingLexer(r *PeekingLexer) {
if r != nil {
peekingLexerPool.Put(r)
}
}

// Range returns the slice of tokens between the two cursor points.
func (p *PeekingLexer) Range(rawStart, rawEnd RawCursor) []Token {
return p.tokens[rawStart:rawEnd]
Expand Down
48 changes: 48 additions & 0 deletions lexer/peek_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lexer_test

import (
"math/rand"
"testing"

require "github.com/alecthomas/assert/v2"
Expand Down Expand Up @@ -33,6 +34,19 @@ func TestUpgrade(t *testing.T) {
require.Equal(t, tokens, l.Range(0, 3))
}

func TestUpgradePooled(t *testing.T) {
t0 := lexer.Token{Type: 1, Value: "moo"}
ts := lexer.Token{Type: 3, Value: " "}
t1 := lexer.Token{Type: 2, Value: "blah"}
tokens := []lexer.Token{t0, ts, t1}
l, err := lexer.UpgradePooled(&staticLexer{tokens: tokens}, 3)
defer lexer.PutBackPooledPeekingLexer(l)
require.NoError(t, err)
require.Equal(t, t0, *l.Peek())
require.Equal(t, t0, *l.Peek())
require.Equal(t, tokens, l.Range(0, 3))
}

func TestPeekingLexer_Peek_Next_Checkpoint(t *testing.T) {
slexdef := lexer.MustSimple([]lexer.SimpleRule{
{"Ident", `\w+`},
Expand Down Expand Up @@ -71,3 +85,37 @@ func BenchmarkPeekingLexer_Peek(b *testing.B) {
}
require.Equal(b, lexer.Token{Type: 2, Value: "y"}, *t)
}

func generateTokenArray(sz int) []lexer.Token {
tokens := make([]lexer.Token, sz)
for i := range tokens {
tokens[i].Type = lexer.TokenType(rand.Int() % 3) /* #nosec */
tokens[i].Value = string([]byte{byte('a' + (rand.Int() % 26))}) /* #nosec */
}
return tokens
}

func BenchmarkPeekingLexer_Upgrade(b *testing.B) {
tokens := generateTokenArray(10000)
testLexer := &staticLexer{tokens: tokens}
b.ResetTimer()
for i := 0; i < b.N; i++ {
l, err := lexer.Upgrade(testLexer)
require.NoError(b, err)
l.Next()
l.Peek()
}
}

func BenchmarkPeekingLexer_UpgradePooled(b *testing.B) {
tokens := generateTokenArray(10000)
testLexer := &staticLexer{tokens: tokens}
b.ResetTimer()
for i := 0; i < b.N; i++ {
l, err := lexer.UpgradePooled(testLexer)
require.NoError(b, err)
l.Next()
l.Peek()
lexer.PutBackPooledPeekingLexer(l)
}
}