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:
@ -108,8 +108,12 @@ static inline int sfc_free_push(int cls, void* ptr) {
|
||||
#if !HAKMEM_BUILD_RELEASE && defined(HAKMEM_SFC_DEBUG_LOG)
|
||||
// Debug logging (compile-time gated; zero cost in release)
|
||||
do {
|
||||
static __thread int free_debug_enabled = -1;
|
||||
static __thread int free_debug_count = 0;
|
||||
if (getenv("HAKMEM_SFC_DEBUG") && free_debug_count < 20) {
|
||||
if (__builtin_expect(free_debug_enabled == -1, 0)) {
|
||||
free_debug_enabled = getenv("HAKMEM_SFC_DEBUG") ? 1 : 0;
|
||||
}
|
||||
if (free_debug_enabled && free_debug_count < 20) {
|
||||
free_debug_count++;
|
||||
extern int g_sfc_enabled;
|
||||
fprintf(stderr, "[SFC_FREE_PUSH] cls=%d, ptr=%p, cnt=%u, cap=%u, will_succeed=%d, enabled=%d\n",
|
||||
|
||||
Reference in New Issue
Block a user