-
Notifications
You must be signed in to change notification settings - Fork 501
OPENNLP-1883: Make stemmers thread-safe, add StemmerFactory and per-thread stem caching #1163
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
Open
krickert
wants to merge
12
commits into
apache:main
Choose a base branch
from
ai-pipestream:stemmer-factory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8c8d27d
OPENNLP-1883: Make SnowballStemmer thread-safe; add StemmerFactory an…
krickert 7ab7a01
OPENNLP-1883: Add multi-threaded stemmer eval and expand concurrency …
krickert cb9df98
OPENNLP-1883: Add SnowballStemmer JMH benchmark: thread-safe vs pre-p…
krickert 6de3ee7
OPENNLP-1883: Add CachingStemmer: per-thread LRU memoization of word-…
krickert bc0b474
OPENNLP-1883: Route TermAnalyzer factory-based stemming through Cachi…
krickert b76eef4
OPENNLP-1883: Address review: wrapper forwarding, state release, zero…
krickert bfeb0bd
OPENNLP-1883: Address review: concise contracts, shared thread routin…
krickert 97328a1
OPENNLP-1883: Quality pass: unbounded interner growth and capacity ov…
krickert 2302621
OPENNLP-1883: Extract a DelegatingStemmer base to share the per-threa…
krickert 81a0c63
OPENNLP-1883: Tighten javadoc to contracts, document overrides, valid…
krickert e43fa46
OPENNLP-1883: Use IllegalArgumentException for null arguments and tri…
krickert 7fadc1b
OPENNLP-1883: Drop the patch-history framing from the benchmark notes
krickert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
opennlp-api/src/main/java/opennlp/tools/stemmer/StemmerFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package opennlp.tools.stemmer; | ||
|
|
||
| /** | ||
| * A factory for {@link Stemmer} instances: it captures a stemmer configuration (algorithm, | ||
| * repeat count, dictionary path, ...) once and mints configured stemmers on demand. | ||
| * | ||
| * <p>Despite the name, this is not one of the {@code BaseToolFactory}-based tool factories (such | ||
| * as {@code LemmatizerFactory}) that are instantiated by name from a model manifest. It is a | ||
| * plain supplier of configured {@link Stemmer} instances and takes no part in the model-loading | ||
| * mechanism.</p> | ||
| */ | ||
| public interface StemmerFactory { | ||
|
|
||
| /** | ||
| * {@return a new {@link Stemmer}} | ||
| */ | ||
| Stemmer newStemmer(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
opennlp-core/opennlp-runtime/src/jmh/java/opennlp/tools/stemmer/CachingStemmerBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package opennlp.tools.stemmer; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
| import org.openjdk.jmh.runner.Runner; | ||
| import org.openjdk.jmh.runner.options.Options; | ||
| import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
|
||
| import opennlp.tools.stemmer.snowball.SnowballStemmer; | ||
| import opennlp.tools.stemmer.snowball.SnowballStemmerFactory; | ||
|
|
||
| /** | ||
| * JMH benchmark for {@link CachingStemmer} against an uncached shared {@link SnowballStemmer}. | ||
| * | ||
| * <p>Two workloads drive both strategies:</p> | ||
| * <ul> | ||
| * <li>{@code zipf}: a 64k-token stream sampled with 1/rank weights from a 512-word | ||
| * vocabulary. This models real text, where a small vocabulary dominates; the default | ||
| * 1024-entry cache holds the whole vocabulary.</li> | ||
| * <li>{@code diverse}: a 64k-token stream sampled uniformly from an 8192-word vocabulary, | ||
| * 8x the cache capacity. This is the cache-hostile case: mostly misses plus constant | ||
| * eviction, so it bounds the overhead the cache can add.</li> | ||
| * </ul> | ||
| * | ||
| * <p>One op stems 16 consecutive tokens from the stream; each benchmark thread walks the stream | ||
| * from its own cursor.</p> | ||
| */ | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @Warmup(iterations = 5, time = 2) | ||
| @Measurement(iterations = 10, time = 2) | ||
| @Fork(2) | ||
| public class CachingStemmerBenchmark { | ||
|
|
||
| private static final int STREAM_LENGTH = 65536; | ||
| private static final int WORDS_PER_OP = 16; | ||
|
|
||
| private static final String[] ROOTS = { | ||
| "run", "walk", "talk", "develop", "nation", "connect", "form", "create", | ||
| "act", "direct", "govern", "manage", "operate", "organize", "present", "relate", | ||
| "report", "state", "structure", "test", "train", "transform", "translate", "value", | ||
| "view", "wonder", "yield", "zone", "note", "mark", "place", "point" | ||
| }; | ||
| private static final String[] PREFIXES = { | ||
| "", "re", "un", "over", "under", "out", "pre", "post", | ||
| "non", "anti", "de", "dis", "mis", "sub", "super", "inter" | ||
| }; | ||
| private static final String[] SUFFIXES = { | ||
| "", "s", "ed", "ing", "er", "ers", "ation", "ations", | ||
| "ly", "ness", "ment", "ments", "ize", "ized", "izing", "al" | ||
| }; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class WorkloadState { | ||
|
|
||
| @Param({"zipf", "diverse"}) | ||
| String workload; | ||
|
|
||
| String[] stream; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void build() { | ||
| Random random = new Random(42); | ||
| List<String> vocabulary = new ArrayList<>(); | ||
| if ("zipf".equals(workload)) { | ||
| // 32 roots x 16 suffixes = 512 unique words, sampled with 1/rank weights. | ||
| for (String root : ROOTS) { | ||
| for (String suffix : SUFFIXES) { | ||
| vocabulary.add(root + suffix); | ||
| } | ||
| } | ||
| double[] cumulative = new double[vocabulary.size()]; | ||
| double sum = 0; | ||
| for (int rank = 0; rank < vocabulary.size(); rank++) { | ||
| sum += 1.0 / (rank + 1); | ||
| cumulative[rank] = sum; | ||
| } | ||
| stream = new String[STREAM_LENGTH]; | ||
| for (int i = 0; i < STREAM_LENGTH; i++) { | ||
| double r = random.nextDouble() * sum; | ||
| int idx = 0; | ||
| while (cumulative[idx] < r) { | ||
| idx++; | ||
| } | ||
| stream[i] = vocabulary.get(idx); | ||
| } | ||
| } else { | ||
| // 16 prefixes x 32 roots x 16 suffixes = 8192 unique words, sampled uniformly. | ||
| for (String prefix : PREFIXES) { | ||
| for (String root : ROOTS) { | ||
| for (String suffix : SUFFIXES) { | ||
| vocabulary.add(prefix + root + suffix); | ||
| } | ||
| } | ||
| } | ||
| stream = new String[STREAM_LENGTH]; | ||
| for (int i = 0; i < STREAM_LENGTH; i++) { | ||
| stream[i] = vocabulary.get(random.nextInt(vocabulary.size())); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class UncachedState { | ||
| Stemmer stemmer; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void create() { | ||
| stemmer = new SnowballStemmer(SnowballStemmer.ALGORITHM.ENGLISH); | ||
| } | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class CachedState { | ||
| Stemmer stemmer; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void create() { | ||
| stemmer = new CachingStemmer( | ||
| new SnowballStemmerFactory(SnowballStemmer.ALGORITHM.ENGLISH)); | ||
| } | ||
| } | ||
|
|
||
| @State(Scope.Thread) | ||
| public static class Cursor { | ||
| int position; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void randomize() { | ||
| position = new Random().nextInt(STREAM_LENGTH); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Threads(Threads.MAX) | ||
| public void uncachedShared(WorkloadState w, UncachedState st, Cursor cursor, Blackhole bh) { | ||
| for (int i = 0; i < WORDS_PER_OP; i++) { | ||
| bh.consume(st.stemmer.stem(w.stream[cursor.position++ & (STREAM_LENGTH - 1)])); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Threads(Threads.MAX) | ||
| public void cachedShared(WorkloadState w, CachedState st, Cursor cursor, Blackhole bh) { | ||
| for (int i = 0; i < WORDS_PER_OP; i++) { | ||
| bh.consume(st.stemmer.stem(w.stream[cursor.position++ & (STREAM_LENGTH - 1)])); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Quick local iteration only: {@code forks(0)} disables JVM fork isolation | ||
| * (unlike {@code mvn} with the {@code jmh} profile). | ||
| * Use the Maven-invoked configuration for publishable numbers. | ||
| */ | ||
| public static void main(String[] args) throws Exception { | ||
| Options opt = new OptionsBuilder() | ||
| .include(CachingStemmerBenchmark.class.getSimpleName()) | ||
| .forks(0) | ||
| .warmupIterations(3) | ||
| .measurementIterations(5) | ||
| .build(); | ||
| new Runner(opt).run(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.