Skip to content

(perf) pool queryStringLex to reuse bufio.Reader across query parses#2300

Merged
abhinavdangeti merged 1 commit intoblevesearch:masterfrom
huynhanx03:dev
Apr 9, 2026
Merged

(perf) pool queryStringLex to reuse bufio.Reader across query parses#2300
abhinavdangeti merged 1 commit intoblevesearch:masterfrom
huynhanx03:dev

Conversation

@huynhanx03
Copy link
Copy Markdown
Contributor

@huynhanx03 huynhanx03 commented Mar 27, 2026

Summary

  • Pool queryStringLex (including its bufio.Reader with 4096-byte internal buffer) via sync.Pool to avoid allocating a new instance on every parseQuerySyntax call
  • Every query string parse, including sub-queries in boolean/conjunction/disjunction, goes through this path, making it a high-frequency allocation hotspot
  • Eliminates repeated bufio.NewReader allocation by reusing pooled instances via bufio.Reader.Reset()

Changes

  • search/query/query_string_lex.go: Add sync.Pool with getQueryStringLex() and putQueryStringLex() helpers. Original newQueryStringLex() unchanged for backward compatibility.
  • search/query/query_string_parser.go: parseQuerySyntax() uses pooled lexer instead of allocating new one.
  • search/query/query_string_parser_test.go: Updated BenchmarkLexer to exercise the pooled path.

Benchmark

goos: darwin
goarch: arm64
cpu: Apple M1

name            old ns/op   new ns/op   delta
BenchmarkLexer  1,682       1,233       -26.7%

name            old B/op    new B/op    delta
BenchmarkLexer  5,344       1,104       -79.3%

name            old allocs  new allocs  delta
BenchmarkLexer  54          52          -3.7%

Checklist

  • Code compiles correctly and linting passes locally
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

@huynhanx03
Copy link
Copy Markdown
Contributor Author

Hi @capemox , could you review, thanks.

@capemox
Copy link
Copy Markdown
Member

capemox commented Mar 30, 2026

Hi @huynhanx03, thanks for your contribution! Could you also benchmark and compare performance on a multi-goroutine setup?

@huynhanx03
Copy link
Copy Markdown
Contributor Author

Hi @capemox , could you review, thanks.

Changes

  • search/query/query_string_parser_test.go: Added BenchmarkLexer_Parallel using b.RunParallel to exercise the pooled path under concurrent goroutine contention.

Benchmark

goos: darwin
goarch: arm64
cpu: Apple M1

name                     old ns/op   new ns/op   delta
BenchmarkLexer_Parallel  1,283       631.3       -50.8%

name                     old B/op    new B/op    delta
BenchmarkLexer_Parallel  5,344       1,104       -79.3%

name                     old allocs  new allocs  delta
BenchmarkLexer_Parallel  54          52          -3.7%

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 30, 2026

Coverage Status

coverage: 52.523% (+0.02%) from 52.502%
when pulling b50a318 on huynhanx03:dev
into 0d388d7 on blevesearch:master.

Comment thread search/query/query_string_lex.go Outdated
@huynhanx03
Copy link
Copy Markdown
Contributor Author

Hi @capemox , could you review, thanks.

@huynhanx03
Copy link
Copy Markdown
Contributor Author

Hi @abhinavdangeti , could you review and approve? Thanks!

Copy link
Copy Markdown
Member

@abhinavdangeti abhinavdangeti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution @huynhanx03 .

@abhinavdangeti abhinavdangeti merged commit 0afa561 into blevesearch:master Apr 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants