Commit 08a1dd9
Read transition arrays directly on the match path, bypassing SafeIndexable dispatch
The per-character match loop reached every transition, value, and prefix
through AhoCorasickStore -> a SafeIndexable field. SafeIndexable is abstract
with two implementations (DoubleArrayIntList during build, UnboxedIntList
after), so each access was a bimorphic virtual call plus a backing-array
field load plus a bounds check. That indirection sat in the hottest loop in
the library.
After build() the store is always backed by UnboxedIntList, so the match path
can read the raw int[] directly: monomorphic, and the JIT can hoist the array
reference and eliminate per-access bounds checks. The build-time and keyValue
paths still go through the SafeIndexable accessors, so pre-build mutation
(add/replace, which run before buildRuntimeStructures) is unchanged.
Mechanical changes:
- Expose UnboxedIntList.backingArray.
- AhoCorasickStore.buildRuntimeStructures captures the five backing arrays
(runtimeBaseOffsets/Parents/Values/FailureIndices/PrefixIndices) once the
lists have been converted to UnboxedIntList.
- calculateNextState reads baseOffsets/parents/failureIndices directly; the
parentCount bounds check reproduces safeGetParent (out-of-range child has no
parent, and RESERVED_VALUE can never equal a non-negative node index).
- AhoCorasickSpliterator captures the value/prefix arrays once and indexes
them per result.
Behavior unchanged (full library test suite green). Allocation per op
unchanged, as expected — this removes dispatch, not allocation.
ParseBenchmark, 100k dictionary, JDK 25, f2/wi5/i10 (ops/s, higher is better):
config before after delta
ASCII SPARSE 38.5±0.7 43.8±0.8 +13.6%
ASCII DENSE 12.0±0.2 13.7±0.4 +14.6%
UNICODE SPARSE 71.4±1.7 81.2±1.5 +13.8%
UNICODE DENSE 12.0±0.1 13.2±0.2 +9.6%
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 09b2b5f commit 08a1dd9
3 files changed
Lines changed: 63 additions & 9 deletions
File tree
- library/src/main/kotlin/com/pkware/ahocorasick
Lines changed: 22 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
416 | 424 | | |
417 | 425 | | |
418 | | - | |
419 | | - | |
| 426 | + | |
| 427 | + | |
420 | 428 | | |
421 | 429 | | |
422 | 430 | | |
423 | | - | |
424 | | - | |
425 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
426 | 434 | | |
427 | 435 | | |
428 | 436 | | |
| |||
873 | 881 | | |
874 | 882 | | |
875 | 883 | | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
876 | 889 | | |
877 | 890 | | |
878 | 891 | | |
879 | 892 | | |
880 | 893 | | |
881 | 894 | | |
882 | 895 | | |
883 | | - | |
884 | | - | |
| 896 | + | |
| 897 | + | |
885 | 898 | | |
886 | 899 | | |
887 | 900 | | |
888 | 901 | | |
889 | 902 | | |
890 | 903 | | |
891 | | - | |
892 | | - | |
| 904 | + | |
| 905 | + | |
893 | 906 | | |
894 | 907 | | |
895 | 908 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
47 | 70 | | |
48 | 71 | | |
49 | 72 | | |
| |||
229 | 252 | | |
230 | 253 | | |
231 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
232 | 263 | | |
233 | 264 | | |
234 | 265 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
41 | 51 | | |
42 | 52 | | |
43 | 53 | | |
| |||
0 commit comments