apache / apache/rocketmq

[Enhancement] Reduce auxiliary component allocation via AtomicLong, string caches, StringBuilder reuse, and dirty flag

Open
#10,527 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
22.6k
Forks
12k
Avg merge
3d 1h
Merged PRs (30d)
27

Description

### Before Creating the Enhancement Request

- [x] I have confirmed that this should be classified as an enhancement rather than a bug/feature.

### Summary

Reduce allocation in auxiliary store components by replacing boxed maps with primitive atomics, caching string keys, reusing StringBuilders, and optimizing timer wheel flush.

### Motivation

JFR profiling reveals several allocation hotspots in auxiliary store components that run on every message:

1. **`QueueOffsetOperator`** — uses `ConcurrentMap` for queue offsets, boxing every `long` update into a `Long` object and requiring map lookups.
2. **`BrokerStatsManager`** — `buildStatsKey`/`topicQueueKey`/`consumerOffset` methods create new strings on every call. `incQueue*` methods use `Integer` parameters causing autoboxing.
3. **`IndexService`** — builds index keys with `StringBuilder` allocated per-call.
4. **`TimerWheel`** — flushes all slots unconditionally, even when no changes occurred.

### Describe the Solution You'd Like

1. `QueueOffsetOperator`: Replace `ConcurrentMap` with `ConcurrentMap` — eliminates boxing on every update.
2. `BrokerStatsManager`: Cache `buildStatsKey`/`topicQueueKey`/`consumerOffset` string results. Change `Integer` parameters to `int` to eliminate autoboxing.
3. `IndexService`: Reuse `StringBuilder` via ThreadLocal.
4. `TimerWheel`: Add volatile `dirty` flag — skip flush when no changes since last flush.
5. `AppendMessageResult`: Add constructor with pre-computed fields to avoid redundant allocation.

### Describe Alternatives You've Considered

- Use `LongAdder` instead of `AtomicLong` — `AtomicLong` is sufficient for moderate contention and provides `get()` for reads.
- Use `String.format` cache — `String.concat` is faster for small fixed key patterns.
- Use object pool for `StringBuilder` — ThreadLocal is simpler and thread-safe by design.

### Additional Context

Part of a larger JFR-driven optimization effort. Related PRs: #10443, #10444, #10514, #10524, #10526.

Contributor guide

Open the contributing guide

Research direction

Read QueueOffsetOperator, BrokerStatsManager, IndexService, TimerWheel, and AppendMessageResult together with the related optimization PRs (#10443, #10444, #10514, #10524, #10526). Use the JFR allocation hotspots described in the issue as the baseline, then run the existing relevant tests and verify that all five components retain their behavior while reducing the targeted allocations and unnecessary timer-wheel flushes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.