nishantmehta opened a new pull request, #757: URL: https://github.com/apache/commons-text/pull/757
## Summary Two independent allocation reductions in the `similarity` package: **1. `JaccardSimilarity.apply`** built three `HashSet`s per call: the distinct elements of each input, plus a *union* set (a copy of the left set with all right elements added) used only to obtain the union size. By the inclusion–exclusion identity `|A∪B| = |A| + |B| − |A∩B|`, the union set is unnecessary — count the intersection by probing the smaller distinct-element set against the larger and derive the union size arithmetically. The two remaining sets are inherent (the distinct elements of each input) and are now pre-sized to the input length. **2. `JaroWinklerSimilarity` (`matches`)** materialized two `Object[]` arrays (the matched characters of each input) solely to count transpositions position-by-position, and tracked matched positions of the shorter input in an `int[]` where a `boolean` per position suffices. Count transpositions by walking the matched positions of both inputs in lockstep, comparing characters directly, and use a `boolean[]` for the match marks. Behavior is unchanged in both. ## Measurement ThreadMXBean allocation driver, 200k warmed ops, two 19-character inputs: | method | before | after | |--------|--------|-------| | `JaccardSimilarity.apply` | 2408 B/op | 1448 B/op (−40%) | | `JaroWinklerSimilarity` (matches) | 376 B/op | 144 B/op (−62%) | ## Testing `JaccardSimilarityTest`, `JaccardDistanceTest`, `JaroWinklerSimilarityTest` and `JaroWinklerDistanceTest` pass unchanged (36 tests). -- 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]
