[Enhancement] Optimize message properties encode/decode to reduce allocation
Before Creating the Enhancement Request
- [x] I have confirmed that this should be classified as an enhancement rather than a bug/feature.
Summary
Replace per-message HashMap allocation in the properties encode/decode hot path with a compact FlatPropertiesMap backed by a flat Object[] array. Add ThreadLocal reuse for the map, StringBuilder, and char[] buffers. Intern high-frequency property keys and values to eliminate redundant String allocation.
Scope: 8 files in common/message
Motivation
Heap dump analysis reveals:
HashMap$Node[]occupies 21.0% of live heap — every message creates multiple HashMaps for properties parsing.Stringoccupies 8.1% with only 21,873 distinct values out of 100,000 sampled —"true"duplicated 7,142 times,"UNIQ_KEY"4,059 times, etc.byte[]occupies 29.3% — partly from backing arrays of duplicated small Strings.
The string2messageProperties / messageProperties2String path is the single largest allocation hotspot on the broker send path.
Describe the Solution You'd Like
FlatPropertiesMap(new class): A compactMap<String, String>implementation using a flatObject[](key-value pairs). Supportsreset()for ThreadLocal reuse,computeEncodedLength(), andencodeTo(ByteBuffer)for zero-copy serialization.MessageDecoder: Newbytes2messageProperties(ByteBuffer)method that parses properties byte-by-byte withoutnew String()+split(). UsesREUSABLE_PROPS_MAPThreadLocal for map reuse andREUSABLE_SBThreadLocal for StringBuilder reuse.MessageConst: AddSTRING_INTERN_BY_LEN— length-bucketed arrays for key interning. Known property keys (e.g.,KEYS,TAGS,WAIT,UNIQ_KEY) are resolved to static constants by length + first-char matching.MessageDecoder: AddVALUE_INTERN_BY_LENfor common value interning ("true","false","DefaultRegion", etc.).MessageClientIDSetter: ThreadLocalchar[]buffer forcreateUniqIDto avoid per-call allocation.MessageVersion: Replacevalues()iteration with directif-elselookup.
Describe Alternatives You've Considered
String.intern()(JVM native): Risks polluting the JVM String table and has unpredictable GC interaction; length-bucketed array interning is deterministic and bounded.- Keep
HashMapbut pool it: Still pays the overhead ofHashMap.put()internal Node allocation;FlatPropertiesMapavoids this entirely for the typical 8–12 property case. - Protocol-level binary properties (like Pulsar): Would require wire format changes; out of scope for a backward-compatible enhancement.
Additional Context
FlatPropertiesMapis only used on the broker-internal decode→encode path within a single thread; it does not escape to user-facing APIs.- The send-path
string2messagePropertiesinMessageDecoderreturnsHashMap(notFlatPropertiesMap) to maintain compatibility with downstream code that expectsHashMap. - JFR-measured bytes/msg reduction: -42.7% from this change alone (from 4,594 to 2,631 bytes/msg).
Issue Evaluation
Category: type/enhancement | Status: Evaluated
Feasibility: feasible Scope: common/message module (8 files) Compatibility: backward compatible (internal optimization, no API changes)
Assessment: This is a well-researched performance optimization targeting the message properties encode/decode hot path. The heap dump analysis provides concrete evidence:
- HashMap$Node[] occupies 21.0% of live heap
- String duplication is significant (only 21,873 distinct values out of 100,000 sampled)
- The proposed FlatPropertiesMap with ThreadLocal reuse and string interning is a sound approach
The solution addresses the root cause (per-message HashMap allocation) with a compact flat array structure. The zero-copy serialization via ByteBuffer is particularly elegant.
Recommendation: This optimization could significantly reduce GC pressure on high-throughput broker deployments. The approach is technically sound and the scope is well-contained.
Automated evaluation by github-manager-bot
Issue Evaluation
Category: type/enhancement | Status: Evaluated
Feasibility: Feasible
Scope: remoting module — message properties serialization
Compatibility: Wire format must remain unchanged — only internal allocation patterns change.
Reducing allocation in encode/decode hot paths is valuable for throughput. The properties map is accessed on every message send/receive, so even small per-message savings compound at scale.
Note: Ensure the optimized encode/decode produces byte-identical output to maintain wire compatibility with older clients/brokers.
Automated evaluation by RockteMQ-AI