Lucene QueryParser in multiple threads: synchronize or construct new each time?
Asked Answered
I

1

6

I have a web application where users submit queries to a Lucene index. The queries are parsed by a Lucene QueryParser. I learned the hard way that QueryParser is not thread-safe.

Is it better to use a single QueryParser instance, and synchronize on calls to its parse() method? Or is it better to construct a new instance for each query? (Or would I be better served by a pool of QueryParsers?)

I know that in general questions like this depend on the particulars and require profiling, but maybe someone out there can say definitively "QueryParsers are extremely inexpensive/expensive to construct"?

Inhale answered 2/4, 2011 at 18:22 Comment(0)
L
7

Create a new one each time. These are lightweight objects and the JVM handles object creation and garbage collection very well. Definitely do not use an object pool.

Lennox answered 2/4, 2011 at 19:7 Comment(2)
+1 yes, definitely no reason to pool (or share a single) QueryParser object. Further reading: Java theory and practice: Urban performance legends, revisitedAdallard
Thanks! @WhiteFang34, that link is about the low cost of allocation in general, but it's surely worth pointing that there are some types of objects where construction is not cheap because a lot of work is done beyond the allocation itself. In the Lucene world one is often reminded to share a single IndexSearcher for performance reasons.Inhale

© 2022 - 2024 — McMap. All rights reserved.