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/10 14:07:54 UTC

[I] Reduce per-message allocation in createUniqID and valueOfMagicCode [rocketmq]

wang-jiahua opened a new issue, #10467:
URL: https://github.com/apache/rocketmq/issues/10467

   ### Motivation
   
   JFR profiling on the producer send path and broker dispatch path reveals two unnecessary per-message allocations:
   
   1. **`MessageClientIDSetter.createUniqID()`** — allocates a `new char[LEN * 2]` on every send. Since the char array is only used to build a msgId string and is immediately discarded, it can be reused via `ThreadLocal`. JFR shows ~405 events/60s from this site.
   
   2. **`MessageVersion.valueOfMagicCode(int)`** — calls `MessageVersion.values()` which copies the enum values array on every invocation (JDK spec), then performs an O(n) linear scan. Since there are only two known magic codes, a direct if-else is both faster and avoids the array copy.
   
   ### Proposed Changes
   
   - `createUniqID()`: replace `new char[LEN * 2]` with `ThreadLocal<char[]>` initialized once per thread.
   - `valueOfMagicCode()`: replace `values()` loop with direct if-else matching the two known magic code constants.
   
   ### Impact
   
   - Eliminates ~405 char[] allocations per 60s on the producer side.
   - Eliminates per-call `Enum.values()` array copy on the broker dispatch path.


-- 
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.apache.org

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


Re: [I] Reduce per-message allocation in createUniqID and valueOfMagicCode [rocketmq]

Posted by "oss-sentinel-ai (via GitHub)" <gi...@apache.org>.
oss-sentinel-ai commented on issue #10467:
URL: https://github.com/apache/rocketmq/issues/10467#issuecomment-4671614742

   ## 🔍 Issue Evaluation
   
   **Type**: Enhancement
   **Category**: Performance Optimization
   **Priority**: Medium
   
   ### Analysis
   
   This is a well-documented performance enhancement based on JFR profiling data. The issue identifies two per-message allocation hotspots:
   
   1. **MessageClientIDSetter.createUniqID()**: Allocates new char[LEN * 2] on every send (~405 events/60s)
   2. **MessageVersion.valueOfMagicCode(int)**: Calls Enum.values() which copies the array per JDK spec
   
   ### Proposed Solutions
   
   The suggested optimizations are sound:
   - **ThreadLocal<char[]>** for createUniqID() - eliminates per-call allocation
   - **Direct if-else** for valueOfMagicCode() - avoids array copy and O(n) scan
   
   ### Assessment
   
   ✅ **Accepted** - These are legitimate performance improvements with clear profiling evidence. The solutions are straightforward and maintain backward compatibility.
   
   **Next Steps**: Implementation PR should include:
   - Benchmark results showing allocation reduction
   - Verification that ThreadLocal doesn't introduce memory leaks
   - Test coverage for edge cases
   
   ---
   *🤖 Automated evaluation by oss-sentinel-ai*


-- 
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: [I] [Enhancement] Reduce per-message allocation in createUniqID and valueOfMagicCode [rocketmq]

Posted by "RongtongJin (via GitHub)" <gi...@apache.org>.
RongtongJin closed issue #10467: [Enhancement] Reduce per-message allocation in createUniqID and valueOfMagicCode
URL: https://github.com/apache/rocketmq/issues/10467


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