Files
hakmem/core/box/hakmem_env_snapshot_box.h
Moe Charm (CI) 88717a8737 Phase 4 E1: ENV Snapshot Consolidation - GO (+3.92% avg, +4.01% median)
Target: Consolidate 3 ENV gate TLS reads → 1 TLS read
- tiny_c7_ultra_enabled_env():    1.28% self
- tiny_front_v3_enabled():        1.01% self
- tiny_metadata_cache_enabled():  0.97% self
- Total overhead: 3.26% self (perf profile analysis)

Implementation:
- core/box/hakmem_env_snapshot_box.h (new): ENV snapshot struct & API
- core/box/hakmem_env_snapshot_box.c (new): TLS snapshot implementation
- core/front/malloc_tiny_fast.h: Migrated 5 call sites to snapshot
- core/box/tiny_legacy_fallback_box.h: Migrated 2 call sites
- core/box/tiny_metadata_cache_hot_box.h: Migrated 1 call site
- core/bench_profile.h: Added hakmem_env_snapshot_refresh_from_env()
- Makefile: Added hakmem_env_snapshot_box.o to build
- ENV gate: HAKMEM_ENV_SNAPSHOT=0/1 (default: 0, research box)

A/B Test Results (Mixed, 10-run, 20M iters):
- Baseline (E1=0): 43,617,549 ops/s (avg), 43,562,895 ops/s (median)
- Optimized (E1=1): 45,327,239 ops/s (avg), 45,309,218 ops/s (median)
- Improvement: avg +3.92%, median +4.01%

Decision: GO (+3.92% >= +2.5% threshold)
- Action: Keep as research box (default OFF) for Phase 4
- Next: Consider promotion to default in MIXED_TINYV3_C7_SAFE preset

Design Rationale:
- Shape optimizations (B3, D3) reached saturation (+0.56% NEUTRAL)
- Shift to memory/TLS overhead optimization (new optimization frontier)
- Pattern: Similar to existing tiny_front_v3_snapshot (proven approach)
- Expected: +1-3% from 3.26% ENV overhead → Achieved: +3.92%

Technical Details:
- Consolidation: 3 TLS reads → 1 TLS read (66% reduction)
- Learner interlock: tiny_metadata_cache_eff pre-computed in snapshot
- Version sync: Refreshes on small_policy_v7_version_changed()
- Fallback safety: Existing ENV gates still available when E1=0

🤖 Generated with Claude Code

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2025-12-14 00:59:12 +09:00

65 lines
2.3 KiB
C

// hakmem_env_snapshot_box.h - Phase 4 E1: ENV Snapshot Consolidation
//
// Purpose: Consolidate 3 hot ENV gate calls into 1 TLS snapshot read
// Target: tiny_c7_ultra_enabled_env (1.28%) + tiny_front_v3_enabled (1.01%) +
// tiny_metadata_cache_enabled (0.97%) = 3.26% combined ENV overhead
//
// Design:
// - ENV: HAKMEM_ENV_SNAPSHOT=0/1 (default 0, research box)
// - Single TLS snapshot struct containing all hot toggles
// - Lazy init with version-based refresh (follows tiny_front_v3_snapshot pattern)
// - Learner interlock: tiny_metadata_cache_eff = cache && !learner
//
// Benefits:
// - 3 TLS reads → 1 TLS read (66% reduction)
// - 3 lazy init checks → 1 lazy init check
// - Expected gain: +1-3% (conservative from 3.26% overhead)
#ifndef HAK_ENV_SNAPSHOT_BOX_H
#define HAK_ENV_SNAPSHOT_BOX_H
#include <stdbool.h>
#include <stdlib.h>
// ENV snapshot struct: consolidates all hot ENV gates
typedef struct HakmemEnvSnapshot {
bool tiny_c7_ultra_enabled; // ENV: HAKMEM_TINY_C7_ULTRA (default 1)
bool tiny_front_v3_enabled; // ENV: HAKMEM_TINY_FRONT_V3_ENABLED (default 1)
bool tiny_metadata_cache; // ENV: HAKMEM_TINY_METADATA_CACHE (default 0)
bool tiny_metadata_cache_eff; // Effective: cache && !learner (for hot path)
} HakmemEnvSnapshot;
// Global snapshot state (implemented in hakmem_env_snapshot_box.c)
extern HakmemEnvSnapshot g_hakmem_env_snapshot;
extern int g_hakmem_env_snapshot_ready;
// Snapshot initializer (implemented in hakmem_env_snapshot_box.c)
void hakmem_env_snapshot_init(void);
// Refresh from ENV (for bench_profile putenv sync)
void hakmem_env_snapshot_refresh_from_env(void);
// Fast snapshot getter: lazy init + 1 TLS read
static inline const HakmemEnvSnapshot* hakmem_env_snapshot(void) {
if (__builtin_expect(!g_hakmem_env_snapshot_ready, 0)) {
hakmem_env_snapshot_init();
}
return &g_hakmem_env_snapshot;
}
// ENV gate: default OFF (research box, set =1 to enable)
static inline bool hakmem_env_snapshot_enabled(void) {
static int g = -1;
if (__builtin_expect(g == -1, 0)) {
const char* e = getenv("HAKMEM_ENV_SNAPSHOT");
if (e && *e) {
g = (*e == '1') ? 1 : 0;
} else {
g = 0; // default: OFF (research box)
}
}
return g != 0;
}
#endif // HAK_ENV_SNAPSHOT_BOX_H