You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "wang-jiahua (via GitHub)" <gi...@apache.org> on 2026/06/15 03:19:41 UTC

[PR] [ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType [rocketmq]

wang-jiahua opened a new pull request, #10513:
URL: https://github.com/apache/rocketmq/pull/10513

   ## Summary
   
   Replace `Enum.values()` + O(n) loop with static `BY_CODE[]` array for O(1) lookup in `LanguageCode.valueOf(byte)` and `SerializeType.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:
   
   ```java
   private static final LanguageCode[] BY_CODE;
   static {
       int max = 0;
       for (LanguageCode lc : values()) max = Math.max(max, lc.code & 0xFF);
       BY_CODE = new LanguageCode[max + 1];
       for (LanguageCode lc : values()) BY_CODE[lc.code & 0xFF] = lc;
   }
   public static LanguageCode valueOf(byte code) {
       int idx = code & 0xFF;
       return idx < BY_CODE.length ? BY_CODE[idx] : null;
   }
   ```
   
   Same pattern for `SerializeType` (2 values: JSON=0, ROCKETMQ=1).
   
   ### Files Changed
   
   | File | Change |
   |---|---|
   | `remoting/.../protocol/LanguageCode.java` | +15/-6: `BY_CODE[]` static array + O(1) `valueOf(byte)` |
   | `remoting/.../protocol/SerializeType.java` | +4/-6: `BY_CODE[]` static array + O(1) `valueOf(byte)` |


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType [rocketmq]

Posted by "wang-jiahua (via GitHub)" <gi...@apache.org>.
wang-jiahua commented on code in PR #10513:
URL: https://github.com/apache/rocketmq/pull/10513#discussion_r3410867825


##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};

Review 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.



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};
+
     private byte code;
 
     SerializeType(byte code) {
         this.code = code;
     }
 
     public static SerializeType valueOf(byte code) {
-        for (SerializeType serializeType : SerializeType.values()) {
-            if (serializeType.getCode() == code) {
-                return serializeType;
-            }
-        }
-        return null;
+        int idx = code & 0xFF;
+        return idx < BY_CODE.length ? BY_CODE[idx] : null;

Review 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).



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/LanguageCode.java:
##########
@@ -38,19 +38,28 @@ public enum LanguageCode {
     RUST((byte) 12),
     NODE_JS((byte) 13);
 
+    private static final LanguageCode[] BY_CODE;
+    static {
+        LanguageCode[] all = values();
+        int max = 0;
+        for (LanguageCode lc : all) {
+            max = Math.max(max, lc.code & 0xFF);
+        }
+        BY_CODE = new LanguageCode[max + 1];
+        for (LanguageCode lc : all) {
+            BY_CODE[lc.code & 0xFF] = lc;
+        }

Review 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.



-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType [rocketmq]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #10513:
URL: https://github.com/apache/rocketmq/pull/10513#issuecomment-4704327630

   ## [Codecov](https://app.codecov.io/gh/apache/rocketmq/pull/10513?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `83.33333%` with `2 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 48.05%. Comparing base ([`82a6a78`](https://app.codecov.io/gh/apache/rocketmq/commit/82a6a784ee35b36d94b3ab644bd5394834e2c540?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`847a08c`](https://app.codecov.io/gh/apache/rocketmq/commit/847a08c9aeb62f0eabab9b0ccf6743ad0bac861c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 22 commits behind head on develop.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/rocketmq/pull/10513?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...pache/rocketmq/remoting/protocol/LanguageCode.java](https://app.codecov.io/gh/apache/rocketmq/pull/10513?src=pr&el=tree&filepath=remoting%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Frocketmq%2Fremoting%2Fprotocol%2FLanguageCode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cmVtb3Rpbmcvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3JvY2tldG1xL3JlbW90aW5nL3Byb3RvY29sL0xhbmd1YWdlQ29kZS5qYXZh) | 88.88% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/rocketmq/pull/10513?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...ache/rocketmq/remoting/protocol/SerializeType.java](https://app.codecov.io/gh/apache/rocketmq/pull/10513?src=pr&el=tree&filepath=remoting%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Frocketmq%2Fremoting%2Fprotocol%2FSerializeType.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cmVtb3Rpbmcvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3JvY2tldG1xL3JlbW90aW5nL3Byb3RvY29sL1NlcmlhbGl6ZVR5cGUuamF2YQ==) | 66.66% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/rocketmq/pull/10513?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@              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     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/rocketmq/pull/10513?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType [rocketmq]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #10513:
URL: https://github.com/apache/rocketmq/pull/10513#discussion_r3410842445


##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};
+
     private byte code;
 
     SerializeType(byte code) {
         this.code = code;
     }
 
     public static SerializeType valueOf(byte code) {
-        for (SerializeType serializeType : SerializeType.values()) {
-            if (serializeType.getCode() == code) {
-                return serializeType;
-            }
-        }
-        return null;
+        int idx = code & 0xFF;
+        return idx < BY_CODE.length ? BY_CODE[idx] : null;

Review Comment:
   `BY_CODE` is manually ordered and implicitly assumes codes are contiguous and start at 0 (and that JSON/ROCKETMQ codes won’t change). This is fragile: if a new serialize type gets a non-contiguous code or an existing code changes, the lookup can silently return the wrong enum or `null`. Prefer building `BY_CODE` from `values()` (like `LanguageCode` does) and indexing by `getCode() & 0xFF`, optionally sizing by max code.



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};

Review Comment:
   `BY_CODE` is manually ordered and implicitly assumes codes are contiguous and start at 0 (and that JSON/ROCKETMQ codes won’t change). This is fragile: if a new serialize type gets a non-contiguous code or an existing code changes, the lookup can silently return the wrong enum or `null`. Prefer building `BY_CODE` from `values()` (like `LanguageCode` does) and indexing by `getCode() & 0xFF`, optionally sizing by max code.



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/LanguageCode.java:
##########
@@ -38,19 +38,28 @@ public enum LanguageCode {
     RUST((byte) 12),
     NODE_JS((byte) 13);
 
+    private static final LanguageCode[] BY_CODE;
+    static {
+        LanguageCode[] all = values();
+        int max = 0;
+        for (LanguageCode lc : all) {
+            max = Math.max(max, lc.code & 0xFF);
+        }
+        BY_CODE = new LanguageCode[max + 1];
+        for (LanguageCode lc : all) {
+            BY_CODE[lc.code & 0xFF] = lc;
+        }

Review Comment:
   This will silently overwrite entries if two enum constants are ever assigned the same `code` value, changing behavior vs. the old implementation (which returned the first match during iteration). Add a duplicate check during initialization (e.g., if `BY_CODE[idx] != null` then fail fast with an exception) to prevent ambiguous protocol decoding.



-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType [rocketmq]

Posted by "RongtongJin (via GitHub)" <gi...@apache.org>.
RongtongJin merged PR #10513:
URL: https://github.com/apache/rocketmq/pull/10513


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org