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

   ### Which Issue(s) This PR Fixes
   
   Fixes #10639
   
   ### Brief Description
   
   `CommitLog.checkMessageAndReturnSize(...)` allocated a fresh `byte[]` the 
size of the **entire message** (`new byte[totalSize]`) on every message parsed 
during reput/dispatch. This scratch array is one of the largest byte[] 
allocators on the store path, and it is discarded as soon as the method 
returns, so at high throughput it produces a continuous stream of short-lived, 
whole-message-sized garbage that drives up young-GC frequency.
   
   This PR borrows the scratch array from a per-thread, grow-only buffer 
instead of allocating per call:
   
   - A `ThreadLocal<byte[]>` holds one reusable buffer per dispatch thread 
(per-thread, so no cross-thread sharing / concurrency concern).
   - Grow-only: reuse the cached buffer when it is large enough; allocate (and 
cache) a larger one only when a bigger message arrives. All reads/writes use 
explicit lengths, so a larger-than-needed buffer is safe.
   - Safety cap: for an abnormally large message or a corrupt `totalSize` (`> 
maxMessageSize + 64KB`, or `< 0`), fall back to a one-shot `new 
byte[totalSize]` so an oversized buffer is never pinned in the ThreadLocal.
   
   `bytesContent` is pure transient scratch — bytes are copied in and read back 
out (CRC check, topic/properties `new String(...)`) within the same call, and 
nothing retains a reference after the method returns — so reusing it across 
calls is safe. No public API, method signature, or wire format changes.
   
   ### How Did You Test This Change?
   
   - Existing store tests covering `checkMessageAndReturnSize` and the CRC 
paths pass: `AppendPropCRCTest`, `ConsumeQueueTest` (19 run / 0 failures).
   - Real-server A/B on a fully isolated 4-node cluster (dedicated Producer / 
Broker / NameServer / Consumer machines; `-Xms4g -Xmx4g` G1; OS page cache 
dropped before each arm; 3 interleaved rounds, median):
     - Broker **young GC per million messages −10.5%** (baseline 9.18 → 8.21).
     - **P99 send latency unchanged (~0.85 ms)**, **TPS no regression** (median 
≥ baseline, 0 send failures).
   - Local micro-measurement (`ThreadMXBean.getThreadAllocatedBytes`, JDK 21): 
**−2320 B/op** on this 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to