From da3f3507b8f80ddadbb95ae320395a7ebf9d23a8 Mon Sep 17 00:00:00 2001 From: "Moe Charm (CI)" Date: Fri, 28 Nov 2025 18:04:32 +0900 Subject: [PATCH] Perf optimization: Add __builtin_expect hints to hot paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Branch mispredictions in allocation hot paths. Perf analysis suggested adding likely/unlikely hints. Solution: Added __builtin_expect hints to critical allocation paths: 1. smallmid_is_enabled() - unlikely (disabled by default) 2. sm_ptr/tiny_ptr/pool_ptr/mid_ptr null checks - likely (success expected) Optimized locations (core/box/hak_alloc_api.inc.h): - Line 44: smallmid check (unlikely) - Line 53: smallmid success check (likely) - Line 81: tiny success check (likely) - Line 112: pool success check (likely) - Line 126: mid success check (likely) Benchmark results (10M iterations × 5 runs, ws=256): - Before (Opt2): 71.30M ops/s (avg) - After (Opt3): 72.92M ops/s (avg) - Improvement: +2.3% (+1.62M ops/s) Matches Task agent's prediction of +2-3% throughput gain. Perf analysis: commit 53bc92842 --- core/box/hak_alloc_api.inc.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core/box/hak_alloc_api.inc.h b/core/box/hak_alloc_api.inc.h index 652887fe..35e9d8d5 100644 --- a/core/box/hak_alloc_api.inc.h +++ b/core/box/hak_alloc_api.inc.h @@ -40,7 +40,8 @@ inline void* hak_alloc_at(size_t size, hak_callsite_t site) { // ENV: HAKMEM_SMALLMID_ENABLE=1 to enable (default: OFF) // CRITICAL: Must come BEFORE Tiny to avoid routing conflict // When enabled, auto-adjusts Tiny to C0-C5 (0-255B only) - if (smallmid_is_enabled() && smallmid_is_in_range(size)) { + // PERF_OPT: unlikely hint - smallmid disabled by default + if (__builtin_expect(smallmid_is_enabled() && smallmid_is_in_range(size), 0)) { #if HAKMEM_DEBUG_TIMING HKM_TIME_START(t_smallmid); #endif @@ -48,7 +49,8 @@ inline void* hak_alloc_at(size_t size, hak_callsite_t site) { #if HAKMEM_DEBUG_TIMING HKM_TIME_END(HKM_CAT_TINY_ALLOC, t_smallmid); #endif - if (sm_ptr) { + // PERF_OPT: likely hint - smallmid usually succeeds when enabled + if (__builtin_expect(sm_ptr != NULL, 1)) { hkm_ace_track_alloc(); return sm_ptr; } @@ -75,7 +77,8 @@ inline void* hak_alloc_at(size_t size, hak_callsite_t site) { #if HAKMEM_DEBUG_TIMING HKM_TIME_END(HKM_CAT_TINY_ALLOC, t_tiny); #endif - if (tiny_ptr) { hkm_ace_track_alloc(); return tiny_ptr; } + // PERF_OPT: likely hint - tiny allocations usually succeed (hot path) + if (__builtin_expect(tiny_ptr != NULL, 1)) { hkm_ace_track_alloc(); return tiny_ptr; } // PHASE 7 CRITICAL FIX: No malloc fallback for Tiny failures // If Tiny fails for size <= tiny_get_max_size(), let it flow to Mid/ACE layers @@ -105,7 +108,8 @@ inline void* hak_alloc_at(size_t size, hak_callsite_t site) { // Phase 1: Ultra-fast Pool TLS for 8KB-52KB range if (size >= 8192 && size <= 53248) { void* pool_ptr = pool_alloc(size); - if (pool_ptr) return pool_ptr; + // PERF_OPT: likely hint - pool allocations usually succeed + if (__builtin_expect(pool_ptr != NULL, 1)) return pool_ptr; // Fall through to existing Mid allocator as fallback } #endif @@ -118,7 +122,8 @@ inline void* hak_alloc_at(size_t size, hak_callsite_t site) { #if HAKMEM_DEBUG_TIMING HKM_TIME_END(HKM_CAT_POOL_GET, t_mid); #endif - if (mid_ptr) return mid_ptr; + // PERF_OPT: likely hint - mid allocations usually succeed + if (__builtin_expect(mid_ptr != NULL, 1)) return mid_ptr; } #if HAKMEM_FEATURE_EVOLUTION