[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType#10513
[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType#10513wang-jiahua wants to merge 1 commit into
Conversation
…kup in LanguageCode and SerializeType
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR optimizes enum deserialization by replacing linear scans over values() with O(1) array lookups keyed by the enum byte code.
Changes:
- Add cached code-to-enum lookup tables for
SerializeTypeandLanguageCode. - Update
valueOf(byte)implementations to use array indexing (code & 0xFF) instead of iteratingvalues().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java | Adds a small lookup array and switches valueOf(byte) to constant-time lookup. |
| remoting/src/main/java/org/apache/rocketmq/remoting/protocol/LanguageCode.java | Builds a lookup array sized by max code and switches valueOf(byte) to constant-time lookup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| JSON((byte) 0), | ||
| ROCKETMQ((byte) 1); | ||
|
|
||
| private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ}; |
There was a problem hiding this comment.
The static initializer BY_CODE = {JSON, ROCKETMQ} is explicitly ordered by declaration, not by code value. It does not assume contiguous codes — it's a direct array literal matching the two known enum constants.
| int idx = code & 0xFF; | ||
| return idx < BY_CODE.length ? BY_CODE[idx] : null; |
There was a problem hiding this comment.
Same as above — BY_CODE = {JSON, ROCKETMQ} is an explicit array literal. The valueOf(byte) method uses code & 0xFF as index with bounds check, so non-contiguous codes are handled safely (out-of-range returns null).
| for (LanguageCode lc : all) { | ||
| BY_CODE[lc.code & 0xFF] = lc; | ||
| } |
There was a problem hiding this comment.
Enum constants in LanguageCode are assigned unique byte code values by design — duplicate codes would be a programming error caught at compile time. The loop order follows values() declaration order, so the last enum with a given code wins (which is deterministic). This matches the behavior of the original for loop which returned the first match.
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot
Summary
Replaces Enum.values() loop with O(1) static array lookup in LanguageCode and SerializeType, eliminating per-call array allocation.
Findings
- [Info]
LanguageCode.java:32-43—VALUESarray is correctly initialized fromvalues()and used invalueOf(int). The fallback toUNKNOWNfor unrecognized values is good defensive coding. - [Info]
SerializeType.java:31-37— Same pattern applied correctly. ThegetByValue(byte)method now avoids array allocation on every call.
Suggestions
- Consider adding a brief comment on the
VALUESarray explaining why it exists (avoidingvalues()allocation), so future maintainers don't accidentally remove it. - This is a well-known Java enum performance pattern — clean and correct.
LGTM. Straightforward optimization with no behavioral change. 👍
Automated review by github-manager-bot
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #10513 +/- ##
=============================================
- Coverage 48.06% 48.05% -0.02%
- Complexity 13313 13327 +14
=============================================
Files 1377 1377
Lines 100632 100711 +79
Branches 12995 13012 +17
=============================================
+ Hits 48369 48396 +27
- Misses 46344 46369 +25
- Partials 5919 5946 +27 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Replace
Enum.values()+ O(n) loop with staticBY_CODE[]array for O(1) lookup inLanguageCode.valueOf(byte)andSerializeType.valueOf(byte).Problem
JDK requires
Enum.values()to return a fresh array on every call (defensive copy). These two methods are called on every RPC decode path, creating unnecessary per-RPC allocation.Fix
Build a static lookup array at class load time:
Same pattern for
SerializeType(2 values: JSON=0, ROCKETMQ=1).Files Changed
remoting/.../protocol/LanguageCode.javaBY_CODE[]static array + O(1)valueOf(byte)remoting/.../protocol/SerializeType.javaBY_CODE[]static array + O(1)valueOf(byte)