Skip to content

Commit

Permalink
Merge pull request #2 from jfroundjian/ParserPoolCacheLimit
Browse files Browse the repository at this point in the history
feat: Add configurable size limit to recycled Parsers in pool
  • Loading branch information
jensneuse authored Sep 10, 2024
2 parents 5fc60fe + ce629e2 commit bb15f94
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
11 changes: 0 additions & 11 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,6 @@ func TestParseRawString(t *testing.T) {
})
}

func TestParserPool(t *testing.T) {
var pp ParserPool
for i := 0; i < 10; i++ {
p := pp.Get()
if _, err := p.Parse("null"); err != nil {
t.Fatalf("cannot parse null: %s", err)
}
pp.Put(p)
}
}

func TestValueInvalidTypeConversion(t *testing.T) {
var p Parser

Expand Down
13 changes: 13 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ func (pp *ParserPool) Put(p *Parser) {
pp.pool.Put(p)
}

// PutIfSizeLessThan PutIfLessThan Put returns p to pp only if the number of values in the cache is less than maxSize.
// If set to <= 0, no size limit is applied.
//
// p and objects recursively returned from p cannot be used after p is put into pp or released
func (pp *ParserPool) PutIfSizeLessThan(p *Parser, maxSize int) {
// Release the parser if the cache is too big
if maxSize > 0 && cap(p.c.vs) > maxSize {
return
}

pp.pool.Put(p)
}

// ArenaPool may be used for pooling Arenas for similarly typed JSONs.
type ArenaPool struct {
pool sync.Pool
Expand Down
50 changes: 50 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Pool is no-op under race detector, so all these tests do not work.
//go:build !race

package astjson

import (
"fmt"
"sync"
"testing"
)

func TestParserPool(t *testing.T) {
var pp ParserPool
for i := 0; i < 10; i++ {
p := pp.Get()
if _, err := p.Parse("null"); err != nil {
t.Fatalf("cannot parse null: %s", err)
}
pp.Put(p)
}
}

func TestParserPoolMaxSize(t *testing.T) {
var numNew, numNewLimit int
ppr := &ParserPool{
sync.Pool{New: func() interface{} { numNew++; return new(Parser) }},
}
pprLimit := &ParserPool{
sync.Pool{New: func() interface{} { numNewLimit++; return new(Parser) }},
}

parse := func(ppr *ParserPool, maxSize int, index int) {
var json = fmt.Sprintf(`{"%d":"test"}`, index)
pr := ppr.Get()
_, _ = pr.Parse(json)
ppr.PutIfSizeLessThan(pr, maxSize)
}
for i := 0; i < 10; i++ {
parse(ppr, 0, i)
parse(pprLimit, 1, i)
}

if numNew != 1 {
t.Fatalf("Expected exactly 1 calls to Pool New with no Max Size (not %d)", numNew)
}

if numNewLimit != 10 {
t.Fatalf("Expected exactly 10 calls to Pool with a Max Size (not %d)", numNewLimit)
}
}

0 comments on commit bb15f94

Please sign in to comment.