Remove debug overhead from release builds (19 hotspots)

Problem:
- Release builds (-DHAKMEM_BUILD_RELEASE=1) still execute debug code
- fprintf, getenv(), atomic counters in hot paths
- Performance: 9M ops/s vs System malloc 43M ops/s (4.8x slower)

Fixed hotspots:
1. hak_alloc_api.inc.h - atomic_fetch_add + fprintf every alloc
2. hak_free_api.inc.h - Free wrapper trace + route trace
3. hak_wrappers.inc.h - Malloc wrapper logs
4. tiny_free_fast.inc.h - getenv() every free (CRITICAL!)
5. hakmem_tiny_refill.inc.h - Expensive validation
6. hakmem_tiny_sfc.c - SFC initialization logs
7. tiny_alloc_fast_sfc.inc.h - getenv() caching

Changes:
- Guard all fprintf/printf with #if !HAKMEM_BUILD_RELEASE
- Cache getenv() results in TLS variables (debug builds only)
- Remove atomic counters from hot paths in release builds
- Add no-op stubs for release builds

Impact:
- All debug code completely eliminated in release builds
- Expected improvement: Limited (deeper profiling needed)
- Root cause: Performance bottleneck exists beyond debug overhead

Note: Benchmark results show debug removal alone insufficient for
performance goals. Further investigation required with perf profiling.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-11-13 13:32:58 +09:00
parent c28314fb96
commit 6570f52f7b
8 changed files with 661 additions and 3 deletions

View File

@ -102,14 +102,20 @@ static inline int tiny_free_fast_ss(SuperSlab* ss, int slab_idx, void* base, uin
TinySlabMeta* meta = &ss->slabs[slab_idx];
// Debug: Track tiny_free_fast_ss calls
#if !HAKMEM_BUILD_RELEASE
static __thread int free_ss_debug_enabled = -1;
static __thread int free_ss_debug_count = 0;
if (getenv("HAKMEM_SFC_DEBUG") && free_ss_debug_count < 20) {
if (__builtin_expect(free_ss_debug_enabled == -1, 0)) {
free_ss_debug_enabled = getenv("HAKMEM_SFC_DEBUG") ? 1 : 0;
}
if (free_ss_debug_enabled && free_ss_debug_count < 20) {
free_ss_debug_count++;
int is_same = tiny_free_is_same_thread_ss(ss, slab_idx, my_tid);
extern int g_sfc_enabled;
fprintf(stderr, "[FREE_SS] base=%p, cls=%d, same_thread=%d, sfc_enabled=%d\n",
base, ss->size_class, is_same, g_sfc_enabled);
}
#endif
// Box 6 Boundary: Ownership check (TOCTOU-safe)
if (__builtin_expect(!tiny_free_is_same_thread_ss(ss, slab_idx, my_tid), 0)) {