Skip to content

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

@wang-jiahua

Description

@wang-jiahua

Before Creating the Enhancement Request

  • 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<String, Long> for queue offsets, boxing every long update into a Long object and requiring map lookups.
  2. BrokerStatsManagerbuildStatsKey/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<String, Long> with ConcurrentMap<String, AtomicLong> — 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 AtomicLongAtomicLong 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions