Repository navigation
Use ParseUtil's precompiled patterns instead of recompiling per call - #3617
Conversation
String.split only takes the JDK fast path for a single non-metacharacter regex, or a two-character backslash escape. Anything longer falls through to Pattern.compile(regex).split(this, limit), which compiles a fresh Pattern on every invocation. Twenty sites split on \s+ or \D+, which ParseUtil already holds as compiled constants, and eleven of them are inside per-line loops. Only those twenty change. The 83 single-character sites are left alone deliberately: a precompiled Pattern allocates a Matcher, so routing them through one would be slower than the fast path they take today. Removes ParseUtil.slash, which has no callers. Its javadoc still named it forwardSlash from an earlier rename, and as a two-character escape it was slower than the fast path a plain split would have taken. It is not @publicapi, which is what japicmp checks. Also guards the first element in SolarisOperatingSystem's static initializer. Splitting on whitespace returns a zero-length array for input that is entirely whitespace, and an exception there would leave the class unusable.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout. (13)
📝 WalkthroughWalkthroughThe change replaces repeated local regular-expression compilation with shared or precompiled patterns across hardware, operating system, and utility parsers. It removes the public ChangesParser cleanup
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This PR makes localized performance-oriented parsing changes without an intended behavior change, and no actionable merge-blocking risk remains beyond normal checks and review. Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Nine more sites compile a regex on every call inside a per-line loop, but split on something ParseUtil does not already hold. Each gets a private compiled constant in its own class rather than a new shared one, since the patterns are specific to the format being parsed. Named in upper snake case, which satisfies checkstyle's ConstantName; the lowercase names in ParseUtil are why that file carries a CHECKSTYLE:OFF block. Two candidates are deliberately left alone. FreeBsdCentralProcessor's [\n\r] split runs once from initProcessorCounts on a single large sysctl blob, so the compile is amortized to nothing, and Xwininfo's splits its input once and then iterates the result.
Hoisting the second split inside parseRelease made Error Prone start reporting StringSplitter on the first one, which is pre-existing and unchanged. Converting the delimiter parameter to a Pattern did not settle it either: the check still fires on a one-argument split through a Pattern parameter, though not through a resolvable constant such as ParseUtil.whitespaces. Adding a limit would be the usual fix but is not semantics-preserving here, since a line ending in the delimiter would start reporting an empty version rather than UNKNOWN. The two hoists that motivated the change are inside the per-line loop and are kept.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3617 +/- ##
============================================
- Coverage 80.02% 79.95% -0.07%
- Complexity 3860 3996 +136
============================================
Files 696 696
Lines 24210 24219 +9
Branches 3996 3997 +1
============================================
- Hits 19373 19365 -8
- Misses 2713 2730 +17
Partials 2124 2124
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A mechanical follow-up to #3615 and #3616. No behavior change and no changelog entry.
Twenty splits stop recompiling their pattern
String.splitonly takes the JDK fast path when the regex is a single character that is not one of.$|()[{^?*+\, or a two-character backslash escape. Anything longer falls through toPattern.compile(regex).split(this, limit)— a fresh compile on every call. Twenty sites split on\s+or\D+, both of whichParseUtilalready holds as compiled constants, and eleven of them sit inside per-line parsing loops.The limit is preserved at each site (nineteen of the twenty carry the
, -1added in #3597), and the receiver moves into the argument unchanged, including the awkward ones such asBsdSysctlUtil.sysctl("hw.disknames", "").trim()andosDescriptors.get(0).The single-character splits are deliberately left alone
This is the part worth not "fixing" later. There are 83 sites splitting on
,:/=and a bare space, and routing those through a precompiledPatternwould make them slower, becausePattern.splitallocates aMatcherwhile the fast path is anindexOfloop. An indicative measurement on JDK 26, splitting a ten-field line two million times after warmup:","viaString.split(fast path)","via precompiledPattern.split"\\s+"viaString.split(compiles each call)"\\s+"via precompiledPattern.splitThat is a warmed-up loop rather than JMH, so treat the magnitudes as indicative; the direction is what matters, and it is the opposite of the intuition that a precompiled pattern is always better.
Removes
ParseUtil.slashIt has no callers anywhere in the project, and its javadoc still called it
forwardSlashfrom a rename. It is also self-defeating:\/is a two-character escape, so a plainsplit("/")would take the fast path and beat it.ParseUtilis not annotated@PublicApi, which is what the japicmp configuration includes, so this is outside the compatibility-checked surface.Guards one static initializer
SolarisOperatingSystemreadsplit[0]without checking the length. Splitting on whitespace returns a zero-length array when the input is entirely whitespace, so auname -rvfirst line of only whitespace would have thrown. It is unreachable in practice —getFirstAnswerreturns an empty string when the command fails, and splitting that yields a one-element array — but this runs in a static initializer, where an exception leaves the class permanently unusable, so it is cheap insurance.Not included
Twelve one-off multi-character regexes have no existing constant (
,,[()],:,[ \t#;],[,\s]+,[\n\r],\s*=\s*,\s, and two literal separators). Hoisting each to its ownprivate static final Patternis a judgment call rather than a mechanical one, and only the two insideLinuxOperatingSystemloops would measurably benefit, so they are left for a separate decision.Verification
Full gate green locally on macOS/JDK 25:
./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadoc, plus./mvnw -Perror-prone install -DskipTestsover the whole reactor. 1561 tests pass, with no test changes — the intended outcome for a semantics-preserving sweep, sinceString.split's slow path isPattern.compile(regex).split(this, limit).Summary by CodeRabbit
Bug Fixes
Refactor