biheto opened a new pull request, #1893: URL: https://github.com/apache/commons-lang/pull/1893
`replaceEach` searches for every remaining pattern after consuming each match. With many early matches of one pattern and a late match of another, it repeatedly scans the same suffix. Even two single-character patterns can produce quadratic scanning work. Keep each rule's next match position and search again only when the input cursor has passed it. An overlapping rule is searched again if another replacement consumes its saved match. Equal-position matches still use search-array order. This also applies to each pass of `replaceEachRepeatedly`. The production change replaces the existing `boolean[]` with an `int[]`; it does not introduce a reusable matcher or public API. The earliest-match selection still scans the rules. The tradeoff is larger per-call bookkeeping: the five-rule benchmark allocates 40 bytes for this array instead of 24 bytes on this JVM. The one-rule array has the same aligned size. ### Measurement The first commit adds a benchmark of the public API, including search bookkeeping and output allocation. Input construction happens in setup. It covers grouped and interleaved matches, sparse and absent matches, and a single-pattern control, at 64, 4,096, and 65,536 UTF-16 code units. The table reports the controlled 4,096-code-unit comparisons. Windows, Intel i5-12500H, Oracle JDK 17.0.4.1, JMH 1.37, `-Xms256m -Xmx256m`, GC profiler, two forks, five one-second warmups and five one-second measurements. Each comparison used the same fixed logical processor. Values are mean +/- JMH's 99.9% confidence error, in microseconds per operation. | Distribution | Baseline | Candidate | | --- | ---: | ---: | | Grouped | 831.465 +/- 440.537 | 24.865 +/- 3.343 | | Grouped, UTF-16 text | 2,352.200 +/- 627.818 | 48.318 +/- 12.924 | | Interleaved | 90.210 +/- 29.306 | 49.503 +/- 11.426 | | Sparse | 15.876 +/- 14.806 | 12.520 +/- 8.172 | | No match | 5.112 +/- 9.724 | 3.080 +/- 0.446 | | Single pattern, follow-up control | 16.903 +/- 0.537 | 16.805 +/- 0.225 | The single-pattern follow-up used two forks and ten one-second measurement iterations on another fixed processor, after an outlier in the initial comparison. Its intervals overlap. Sparse and no-match timings are noisy; no speedup is claimed for these cases. The large grouped-case gains are synthetic API measurements, not application-level throughput claims. An earlier unrestricted-processor run showed inconsistent control timings and is not used for the claimed gains. Baseline: `d3d0f52a063bf4f13bad3ce704f0228eb7055c81` (benchmark only). Both source trees were compiled with `javac --release 8`, with the identical executable benchmark. The benchmark Javadoc includes Maven/JMH reproduction commands; add `-p length=4096 -wi 5` to select the table's matrix and pin both runs to the same processor on hybrid-core machines. ### Validation - Four focused tests cover distant matches, ignored rules and unchanged-reference returns, consumed overlapping matches, and equal-position ordering. These guard semantics rather than asserting execution time; timing is evaluated by JMH. - A local deterministic comparison of the two actual implementations agreed on 100,000 cases across `replaceEach` and `replaceEachRepeatedly`, including nulls, empty strings, overlaps, UTF-16 code units, exceptions, and unchanged-reference behavior. - JDK 17 / Maven 3.9.16: the default-goal test run reported 107,764 tests, 0 failures, 0 errors, and 29 skipped. An initial run hit transient external-link download errors during Javadoc; retrying Javadoc succeeded. - Validation was completed across runs. After the full test pass, `mvn -B -ntp -DskipTests verify apache-rat:check checkstyle:check japicmp:cmp spotbugs:check pmd:check javadoc:javadoc` finished with `BUILD SUCCESS`, including coverage verification. The verification run reused the passing test reports. Local Maven proxy settings and UTF-8 JVM encoding were used. ### Contribution checklist - [x] Read the contribution guidelines. - [x] Read the ASF Generative Tooling Guidance. - [x] AI used: OpenAI Codex prepared the implementation, tests, benchmark, measurements, and this description. Commits carry `Assisted-By` trailers. - [x] Run the full tests and all required default Maven checks; split-run validation is detailed above. - [x] Add tests for affected behavior; see the performance-only qualification above. - [x] Describe the change and its tradeoffs. - [x] Use meaningful commit subjects and bodies. -- 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]
