krickert opened a new pull request, #1003:
URL: https://github.com/apache/opennlp/pull/1003

   ## Summary
   
   Make ME classes (TokenizerME, SentenceDetectorME, POSTaggerME, LemmatizerME) 
safe for concurrent use by eliminating shared mutable instance state. This 
enables reusing ME instances across threads instead of allocating a new 
instance per call, reducing allocation overhead in high-throughput pipelines.
   
   The old pattern (`new TokenizerME(model)` per call) continues to work 
identically — zero regressions in correctness or performance.
   
   ## Motivation
   
   ME classes were documented as not thread-safe due to mutable instance fields 
(`bestSequence`, `tokProbs`, `newTokens`, `sentProbs`) that corrupt under 
concurrent access. The recommended workaround was either creating a new ME 
instance per call (expensive for high-throughput pipelines processing thousands 
of sentences in parallel) or using the `ThreadSafe*ME` wrappers (which use 
`ThreadLocal` and leak in Jakarta EE / long-running thread environments).
   
   The root cause was mutable state at four layers:
   
   1. **ME classes** — result fields written on every call
   2. **Context generators** — per-call caches (`contextsCache`, `wordsKey`, 
`buf`, `collectFeats`)
   3. **Feature generators** — `CachedFeatureGenerator` with mutable 
`prevTokens` and cache
   4. **BeamSearch** — shared `probs[]` output buffer and a `contextsCache` 
that stored references to the reused buffer (cached values were always stale)
   
   ## Approach
   
   Move mutable state to method-local variables at every layer. ME instance 
fields are preserved as `volatile` for backward-compatible `probs()` access 
(last-writer-wins under concurrency). Caches are removed entirely — they were 
small (size 3 typically), not thread-safe, and in BeamSearch's case, buggy.
   
   ### Files changed (10 source, 5 test)
   
   | File                                   | Change                            
                                                |
   | -------------------------------------- | 
---------------------------------------------------------------------------------
 |
   | `BeamSearch.java`                      | Removed shared `probs[]` and 
buggy `contextsCache`; added `@ThreadSafe`           |
   | `DefaultSDContextGenerator.java`       | `buf`/`collectFeats` moved to 
method-local; `collectFeatures()` signature updated |
   | `SentenceContextGenerator.java` (Thai) | Updated to match new 
`collectFeatures()` signature                                |
   | `DefaultPOSContextGenerator.java`      | Removed `contextsCache` and 
`wordsKey`                                            |
   | `ConfigurablePOSContextGenerator.java` | Removed `contextsCache` and 
`wordsKey`                                            |
   | `CachedFeatureGenerator.java`          | Removed `prevTokens`, 
`contextsCache`, counters; delegates directly               |
   | `TokenizerME.java`                     | `newTokens`/`tokProbs` volatile; 
`tokenizePos()` uses local lists                 |
   | `SentenceDetectorME.java`              | `sentProbs` volatile; 
`sentPosDetect()` uses local list                           |
   | `POSTaggerME.java`                     | `bestSequence` volatile; `tag()` 
uses local var; added null guard                 |
   | `LemmatizerME.java`                    | `bestSequence` volatile; 
`predictSES()` uses local var                            |
   
   ### Backward compatibility
   
   - The old pattern (`new ME(model)` per call) is **unchanged** — verified by 
regression benchmark
   - `probs()` methods preserved (deprecated behavior under concurrency, 
correct single-threaded)
   - Constructor signatures preserved (`cacheSize` params accepted but ignored, 
marked `@Deprecated(since = "3.0.0")`)
   - No new dependencies
   
   ## Test plan
   
   - [x] All 675 existing tests pass (`mvn test` on opennlp-runtime)
   - [x] `ThreadSafetyBenchmarkTest` — JUnit correctness test: shared ME 
instances produce identical results to single-threaded baseline across all CPU 
cores
   - [x] `RegressionBenchmark` — head-to-head stock vs patched, 
new-instance-per-call only: zero mismatches, zero errors, performance within 
noise on both builds
   - [x] `ThreadSafetyBenchmark` — three-way comparison (new-instance-per-call 
/ instance-per-thread / shared-single-instance)
   - [x] `CachedFeatureGeneratorTest` — updated for removed cache behavior
   - [x] Checkstyle violation count unchanged (9,446 pre-existing on both stock 
and patched)
   - [ ] Full `mvn clean install` at root (checkstyle must be skipped — 9,446 
pre-existing violations on main)
   
   ### Regression benchmark results (32 threads, new-instance-per-call)
   
   Proves zero regression — stock vs patched, same API pattern:
   
   | Component        | stock avg_ms | patched avg_ms | Mismatches | Errors |
   | ---------------- | ------------ | -------------- | ---------- | ------ |
   | Tokenizer        | 16.09        | 16.69          | 0          | 0      |
   | SentenceDetector | 9.21         | 9.01           | 0          | 0      |
   | POSTagger        | 105.76       | 106.58         | 0          | 0      |
   
   ### Speedup benchmark results (32 threads, three-way comparison)
   
   #### Approaches
   
   The benchmark compares three strategies for using ME classes in a 
multi-threaded environment. All three produce identical output for a given 
input — the difference is how ME instances are allocated and shared.
   
   | Approach                   | Description                                   
                                                                                
                                                                                
                                                  | Example code                
                                                                    |
   | -------------------------- | 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 
-----------------------------------------------------------------------------------------------
 |
   | **new-instance-per-call**  | A fresh ME instance is created for every 
single operation. This is the traditional pattern and the baseline. Safe but 
expensive — each call pays the full cost of constructing the ME, its 
BeamSearch, context generators, and feature generator chain.         | 
`String[] tags = new POSTaggerME(model).tag(tokens);`                           
                |
   | **instance-per-thread**    | One ME instance is created per thread and 
reused across all operations on that thread. No cross-thread sharing, so no 
contention. Eliminates per-call constructor overhead while remaining completely 
safe.                                                     | `POSTaggerME tagger 
= new POSTaggerME(model);`<br>`for (String[] t : sentences) tagger.tag(t);` |
   | **shared-single-instance** | A single ME instance is shared across all 
threads. Maximum memory efficiency — only one set of internal structures 
exists. Works for TokenizerME and SentenceDetectorME. POSTaggerME has known 
contention in the feature generator chain at high thread counts. | `POSTaggerME 
shared = new POSTaggerME(model);`<br>`// pass shared to all threads`            
   |
   
   #### Benchmark results
   
   | Component        | Approach                  | avg_ms     | Mismatches | 
Speedup   |
   | ---------------- | ------------------------- | ---------- | ---------- | 
--------- |
   | Tokenizer        | new-instance-per-call     | 16.63      | 0          | 
1.0x      |
   | Tokenizer        | instance-per-thread       | 15.92      | 0          | 
1.04x     |
   | Tokenizer        | shared-single-instance    | 16.24      | 0          | 
1.02x     |
   | SentenceDetector | new-instance-per-call     | 9.49       | 0          | 
1.0x      |
   | SentenceDetector | instance-per-thread       | 9.28       | 0          | 
1.02x     |
   | SentenceDetector | shared-single-instance    | 8.93       | 0          | 
1.06x     |
   | **POSTagger**    | **new-instance-per-call** | **133.55** | **0**      | 
**1.0x**  |
   | **POSTagger**    | **instance-per-thread**   | **80.01**  | **0**      | 
**1.67x** |
   
   POSTagger sees the largest gain because its constructor is the heaviest — it 
builds a BeamSearch, a ConfigurablePOSContextGenerator, and a full 
AdaptiveFeatureGenerator chain on every instantiation. Reusing one instance per 
thread eliminates that allocation on every call, yielding a **1.67x speedup** 
with zero correctness impact.
   
   Tokenizer and SentenceDetector constructors are lighter, so the per-call 
overhead is smaller and all three approaches perform similarly.
   
   See `opennlp-core/opennlp-runtime/BENCHMARKS.md` for full benchmark 
instructions.
   
   ---
   
   Thank you for contributing to Apache OpenNLP.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
     https://issues.apache.org/jira/browse/OPENNLP-1816
   
   - [x] Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA 
number you are trying to resolve? Pay particular attention to the hyphen "-" 
character.
   
   - [x] Has your PR been rebased against the latest commit within the target 
branch (typically main)?
   
   - [x] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   
   - [x] Have you ensured that the full suite of tests is executed via mvn 
clean install at the root opennlp folder?
   - [x] Have you written or updated unit tests to verify your changes?
   - [ ] If adding new dependencies to the code, are these dependencies 
licensed in a way that is compatible for inclusion under [ASF 
2.0](http://www.apache.org/legal/resolved.html#category-a)?
     - N/A — no new dependencies added
   - [ ] If applicable, have you updated the LICENSE file, including the main 
LICENSE file in opennlp folder?
     - N/A — no license changes required
   - [ ] If applicable, have you updated the NOTICE file, including the main 
NOTICE file found in opennlp folder?
     - N/A — no notice changes required
   
   ### For documentation related changes:
   
   - [x] Have you ensured that format looks appropriate for the output in which 
it is rendered?
   
   ### Note:
   
   Please ensure that once the PR is submitted, you check GitHub Actions for 
build issues and submit an update to your PR as soon as possible.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to