[ISSUE #10400] Cache Version.values() in MQVersion to avoid repeated array allocation#10401
[ISSUE #10400] Cache Version.values() in MQVersion to avoid repeated array allocation#10401qianye1001 wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #10401 +/- ##
=============================================
- Coverage 49.09% 48.96% -0.14%
+ Complexity 13507 13472 -35
=============================================
Files 1376 1376
Lines 100537 100540 +3
Branches 12983 12983
=============================================
- Hits 49362 49229 -133
- Misses 45180 45301 +121
- Partials 5995 6010 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
qianye1001
left a comment
There was a problem hiding this comment.
Review by rocketmq-reviewer-bot
Summary
Cache Version.values() result in a private static final field to eliminate repeated defensive-copy array allocations (~7.5KB per call) in getVersionDesc() and value2Version() hot-path methods.
Findings
-
[Info]
MQVersion.java:23—private static final Version[] VERSION_VALUES = Version.values();is initialized once at class loading time. The enum array is immutable after JVM startup (no new enum constants can be added at runtime), so caching is safe. ✅ -
[Info] Both methods — Using a local variable
Version[] versions = VERSION_VALUES;before accessing.lengthand indexing is a good practice. It avoids repeated field reads and is JIT-friendly. ✅ -
[Low]
MQVersion.java:23— The cached array is mutable (Java arrays are always mutable). SinceVERSION_VALUESisprivateand only used read-only within this class, the risk is negligible. However, if any future code were to modify the array (e.g.,VERSION_VALUES[0] = null), it would corrupt all lookups. This is a very low risk given the current usage, but worth noting for future maintainers. -
[Suggestion] Consider adding a brief comment on the cached field explaining why
Version.values()is cached (defensive copy avoidance) — this helps future developers understand the optimization intent without needing to trace the git history. -
[Info] The PR description mentions 943 enum elements and ~7.5KB per allocation. This is a meaningful optimization for high-throughput broker scenarios (heartbeats, client connections). The fix is minimal, well-isolated, and follows a standard Java enum caching pattern.
Cross-repo Note
No cross-repo impact. MQVersion is a common utility class; the change is purely internal performance optimization with no API or behavioral changes.
Verdict
Approve — Clean, minimal performance optimization. The "cache values() in static final + use local variable" pattern is a well-established Java idiom for enum-heavy hot paths. No API changes, no compatibility risk, no cross-repo impact. The only suggestion is to add a brief comment on the field for future maintainability.
Automated review by rocketmq-reviewer-bot
Summary
Cache
Version.values()in a static final field to eliminate repeated defensive-copy array allocations in hot-path methodsgetVersionDesc()andvalue2Version().Problem
Version.values()returns a defensive copy of a 943-element enum array on every call. These two methods call it 6 times total, allocating ~7.5KB of heap per invocation. Under high throughput (broker heartbeats, client connections), this creates unnecessary GC pressure.Fix
private static final Version[] VERSION_VALUES = Version.values();(initialized once at class load)Changes
common/.../MQVersion.javaRelated Issue
Closes #10400
This PR was automatically generated.