perf(butil): ARM64 (aarch64) performance and modernisation improvements across atomics, CRC32C, spin-wait hints, and fast timers
Maintainers usually reply within 2 days
@jimmystewpot is already working on this.
Since Nov 18, 2026.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 12/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- cpp, linux, macos
- Domain
- performance
Research direction
This is a cross-cutting ARM64 optimisation proposal spanning src/butil/atomicops_internals_arm64_gcc.h, src/butil/atomicops.h, src/butil/crc32c.cc, src/butil/processor.h, src/butil/time.h and src/butil/time.cpp. Read those files and the two already-linked pull requests (#3752 and #3756) before starting, since work is under way. Done would mean baseline -march=armv8-a builds pass, runtime dispatch works on Linux and Apple Silicon, and the stated benchmarks improve.
Written by the indexing model from the issue text.
Description
Is your feature request related to a problem?
While profiling StarRocks (which relies heavily on brpc, bthread, and butil for RPC and concurrency) on modern ARM64 (aarch64) servers, CPU profiles highlighted several legacy ARM64 bottlenecks in butil where brpc was falling back to unoptimised software paths or legacy ARMv8.0 inline assembly:
-
Legacy
ldxr/stxrinline assembly and redundantdmb ishfences insrc/butil/atomicops_internals_arm64_gcc.h:
atomicops_internals_arm64_gcc.his a 2014 Chromium import that hardcodes literalldxr/stxrload-linked/store-conditional loops forNoBarrier_CompareAndSwap,NoBarrier_AtomicExchange, andNoBarrier_AtomicIncrement, and wraps them in fullMemoryBarrier()(dmb ish) fences forAcquire_CompareAndSwap,Release_CompareAndSwap, andBarrier_AtomicIncrement(even emitting a fulldmb ishwhenAcquire_CompareAndSwapfails). Because these are opaque assembly strings, GCC and Clang cannot emit ARMv8.1-A LSE instructions (cas,casa,casl,swp,ldadd,ldaddal) or-moutline-atomicsruntime LSE helpers (__aarch64_cas*,__aarch64_ldadd*,__aarch64_swp*). Additionally,src/butil/atomicops.hroutes Apple Silicon (OS_MACOSX+ARCH_CPU_ARM64) to deprecated<libkern/OSAtomic.h>routines instead ofatomicops_internals_arm64_gcc.h. -
Missing ARM64 hardware CRC32C acceleration in
src/butil/crc32c.cc:
src/butil/crc32c.ccaccelerates CRC32C on x86_64 (__SSE4_2__) and RISC-V (__riscv_zbc/__riscv_zvbc), but has no ARM64 (__aarch64__) hardware path. On ARM64,butil::crc32c::IsFastCrc32Supported()returnsfalseandbutil::crc32c::Extend()falls back toExtendImpl<SlowCRC32Functor>(a ~1.2 GiB/s 4-byte software table lookup), despiteFEAT_CRC32andFEAT_PMULLbeing standard on modern ARM64 server and desktop CPUs. -
Ineffective
yieldspin-wait hint on 64-bit ARM insrc/butil/processor.h:
cpu_relax()onARCH_CPU_ARM_FAMILYemitsasm volatile("yield\n": : :"memory"). On single-threaded-per-core ARM64 CPUs (Neoverse N1/N2/V1/V2, Graviton 2/3/4, Ampere Altra, Apple Silicon),yieldexecutes as a 1-cycle (~0.34 ns)nop, causing spin-wait loops (BT_LOOP_WHENinbthread, mutex/butex spinloops, and work-stealing queues) to hammer the load/store unit and cache-coherency fabric at full frequency. -
Unserialised
cntvct_el0reads and runtime 64-bit integer divisions insrc/butil/time.h/src/butil/time.cpp:
WhenBUTIL_USE_CPU_FREQUENCYis enabled on__aarch64__,butil::detail::clock_cycles()readscntvct_el0without anisbbarrier, allowing out-of-order execution to speculatively hoist counter reads ahead of earlier loads or branches (per ARM DDI 0487, Chapter D12 "The Generic Timer in AArch64 state", Section D12.2.2 "The virtual counter"). Furthermore,cpuwide_time_ns()executes two dependent 64-bit integer divisions (tsc / cpu_freqandremain * 1000000000L / cpu_freq) on every call instead of using a precomputed fixed-point multiplier and shift.
Describe the solution you'd like
Address these four areas in small, self-contained PRs that preserve full backward API/ABI compatibility and support both GCC and Clang on Linux AArch64 and Apple Silicon (__aarch64__):
- Modernise ARM64
butil::subtleatomics (src/butil/atomicops_internals_arm64_gcc.h,src/butil/atomicops.h):- Replace hardcoded
ldxr/stxrinline assembly anddmb ishwrappers forAtomic32andAtomic64with GCC/Clang__atomic_*built-ins (__atomic_compare_exchange_n,__atomic_exchange_n,__atomic_add_fetch,__atomic_store_n,__atomic_load_n, and__atomic_thread_fence), and routeCOMPILER_GCC && ARCH_CPU_ARM64ahead ofOS_MACOSX.
- Replace hardcoded
- Add ARM64 hardware CRC32C (
+crcand+crypto/PMULL) acceleration (src/butil/crc32c.cc):- Add an
#if defined(__aarch64__)hardware path using<arm_acle.h>(__crc32cb,__crc32ch,__crc32cw,__crc32cd) and<arm_neon.h>(vmull_p643-way pipelined lane folding) with function target attributes (target("+crc")/target("+crc+crypto")) and runtime capability detection (getauxval(AT_HWCAP)on Linux,sysctlbynameon macOS) so baseline-march=armv8-abuilds compile cleanly and dispatch at runtime.
- Add an
- Use
isbforcpu_relax()on 64-bit ARM (src/butil/processor.h):- Update
cpu_relax()onARCH_CPU_ARM64to emitasm volatile("isb\n": : :"memory")(~15–30 cycle local pipeline pause without coherency traffic, matching MySQL, PostgreSQL, RocksDB, Folly, and OpenJDK), while keeping 32-bitARCH_CPU_ARM_FAMILYonyield.
- Update
- Serialise
clock_cycles()and optimisecpuwide_time_ns()(src/butil/time.h,src/butil/time.cpp):- Precede
mrs %0, cntvct_el0withisbinbutil::detail::clock_cycles()on__aarch64__, and replace the two runtime 64-bit integer divisions incpuwide_time_ns()with a precomputed 128-bitmul + shiftscale factor initialised alongsidedetail::invariant_cpu_freq.
- Precede
Describe alternatives you've considered
- Requiring global
-march=armv8.2-a+crc+cryptobuild flags: rejected so thatbrpccontinues to build and run safely on baseline-march=armv8-atargets while using runtime detection (AT_HWCAP/-moutline-atomics) to take advantage of modern ARM64 extensions where available.
Additional context/screenshots
Verified on both Linux AArch64 (ARM Neoverse-V2, Ubuntu 24.04, GCC 13.3.0) and Apple Silicon (macOS arm64, Apple Clang 21):
- Atomics (4-thread contention on Neoverse-V2):
Barrier_AtomicIncrementimproves from46.34 ns/opto15.39 ns/op(3.01x faster with baseline-march=armv8-aoutline LSE) and7.48 ns/op(6.20x faster with-march=armv8-a+lse); contended CAS spinlock improves from68.01 ns/opto29.74 ns/op(2.29x faster). - CRC32C (64 KiB buffers, compiled with baseline
-march=armv8-a): Throughput increases from1.20 GiB/sto20.73 GiB/s(17.3x faster) on Neoverse-V2 and from1.12 GiB/sto28.20 GiB/s(25.2x faster) on Apple Silicon. - Spin-wait (
cpu_relax()on Neoverse-V2): Replaces a0.34 ns(1-cyclenop)yieldwith a9.04 ns(~26-cycle) localisbpipeline pause. - Fast timer (
cpuwide_time_ns()scaling math): Replaces two dependent 64-bitudivinstructions withmul/umulh+lsr, reducing scaling latency from7.91 nsto3.04 ns(2.6x faster) on Neoverse-V2 (18 nsto8 nsper call on Apple Silicon).
- Dominant language
- C++
- Stars
- 17.6k
- Forks
- 4.2k
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 72
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/brpc
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
endpoint2hostname() prints a byte-swapped port for IPv6 endpointsPossibly taken @0z5a claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 2 days
-
file:// naming service takes a comment after the address as the tagPossibly taken @0z5a claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
Maintainers usually reply within 2 days
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 2 days
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 20/100
apache/brpc#3707 · 3 comments ·
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
protocolbuffers/protobuf#31768 ·
Maintainers usually reply within 1 day
-
Needs: Triage :mag: Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
react/react-native#60167 ·
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
-
tests: ofx_render_test doesn't compile against FFmpeg 4.x (const AVCodec* to av_find_best_stream)Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/openfx-io#221 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
subsurface/subsurface#5145 ·
Maintainers usually reply within 2 days