garydgregory opened a new pull request, #1894: URL: https://github.com/apache/commons-lang/pull/1894
Cache each rule's next match to avoid repeated suffix scans. Copy unchanged text ranges in bulk, skipping empty ranges. Preserve overlap handling, search-order ties, and repeated-pass behavior. Add Unicode regression tests and JMH benchmarks for different match distributions. The cached match positions in this PR address the repeated suffix scans. I tested a small additional optimization: copy unchanged text using StringBuilder.append(text, start, end) instead of appending individual characters, and skip empty gaps between matches. This retains PR 1893 matching algorithm and bookkeeping while reducing copying overhead, particularly when replacements are sparse. I compared both implementations using the same benchmark: | Input distribution | PR | With range copying | Speedup | |---|---:|---:|---:| | Grouped | 9.763 µs | 8.880 µs | 1.10× | | Grouped, UTF-16 | 16.242 µs | 14.559 µs | 1.12× | | Interleaved | 21.613 µs | 20.177 µs | 1.07× | | Sparse, match at end | 10.504 µs | 0.934 µs | **11.24×** | | Sparse, match in middle | 9.878 µs | 0.951 µs | **10.39×** | | Sparse, UTF-16 | 6.135 µs | 2.239 µs | **2.74×** | | No match | 0.723 µs | 0.724 µs | ≈1.00× | | Single pattern | 7.359 µs | 6.149 µs | 1.20× | These are mean times per operation from JMH 1.37 on OpenJDK 17.0.21.1, with inputs of 4,096 UTF-16 code units, two forks, three 500 ms warmup iterations and five 500 ms measurement iterations per fork, -Xms256m -Xmx256m, and the GC profiler. The largest gains come from copying long unchanged ranges. No-match timings overlap within measurement error. These are synthetic API measurements; application-level gains will depend on the workload. Validation of the combined change: - The complete default Maven build passed: 111,689 tests, zero failures/errors, 29 skipped, including coverage and static checks. - 404,800 behavior comparisons on each of JDK 8, 17, and 21 matched the PR’s outputs, exception types/messages, and unchanged-input reference identity. - Added regression coverage for unchanged ranges containing supplementary characters and unpaired surrogate code units, including repeated replacement passes. - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project. - [x] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI). - [x] I used AI to create any part of, or all of, this pull request: GPT-6 Astra Extra High. - [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself. - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice. - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. - [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process. -- 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]
