Improve snowball stemming by adding an insert-only cache - #16356
Conversation
Avoid redundant Snowball stemmer invocations on repeated tokens via insert-only CharArrayMap cache; 2.3-2.7x faster. Default 1024 entries (~75 KB), disableable via constructor.
|
I don't think it's worth the extra complexity, to be honest. If you have costly components in your indexing pipeline then there's another idea you can try - wrap the costly pipeline into a top-level component which then returns all the cached attributes for a surface token image. The "cache" here can be dumb and just clear itself when it saturates (which is much simpler than any "real" hit-count cache) and just rely on token distribution. This works for everything (assuming context independance) and indeed can speed up processing... but it is something that users should/can configure themselves, knowing what their data and analysis pipeline is, I think. |
|
Also i wonder if the benchmark removed stopwords. They can really skew the results! If you don't want to remove stopwords, you can at least not pass them thru the stemmer, e.g. using KeywordMarkerFilter/StemmerOverrideFilter/ProtectedWordsFilter/etc. its the same idea: the very high frequency words go into a chararrayset and avoid being run thru the stemmer. |
Snowball stemming is expensive due to algorithmic suffix manipulation repeated identically for every occurrence of the same token.
This PR adds an small insert-only cache (CharArrayMap) to avoid the redundant work by exploiting the fact that natural text is Zipfian (same short words account for majority of token occurrences).
The cache is small (1024 entries, ~75KB) and caches only tokens up to 10 chars.
It is enabled by default to avoid mapping changes for existing users and can be disabled through maxCacheSize=0.
Benchmarks
AMD EPYC 7R32 (c5a.2xlarge), JDK 25, 500 unique words, 10K tokens Zipfian corpus. 3 forks, 10 iters × 1s. cacheSize=0 is the uncached baseline.
The default cache size of 1024 entries was picked from the sweep above as it captures virtually all the benefits across English and German.