# Phase 4 E3-4: ENV Constructor Init 設計メモ ## 目的 E1 で統合した ENV snapshot の lazy init check(3.22% self%)を排除。 **期待**: **+0.5-1.5%** 改善 --- ## 結果(A/B テスト) **判定**: ✅ **GO** (+4.75%) | Metric | Baseline (CTOR=0) | Optimized (CTOR=1) | Delta | |--------|-------------------|-------------------|-------| | Mean | 44.27M ops/s | 46.38M ops/s | **+4.75%** | | Median | 44.60M ops/s | 46.53M ops/s | **+4.35%** | **観察**: - 期待値 +0.5-1.5% を大幅に上回る +4.75% 達成 - 全 10 run で Optimized が Baseline を上回る(一貫した改善) - Median でも +4.35% 確認(外れ値ではない) **分析**: - lazy init check(`if (g == -1)`)の削除効果が予想以上 - 分岐予測ミス削減 + TLS アクセスパターン改善が複合的に効いた可能性 - E1 (+3.92%) と E3-4 (+4.75%) の累積効果: **~+9%** --- ## 現状分析 ### E1 完了後の状態 - `hakmem_env_snapshot_enabled()`: 3.22% self%(perf profile) - 原因: 毎回の lazy init check(`static int g = -1` + `getenv()`) ```c // 現在の実装(core/box/hakmem_env_snapshot_box.h:51-62) static inline bool hakmem_env_snapshot_enabled(void) { static int g = -1; if (__builtin_expect(g == -1, 0)) { // ← この分岐が 3.22% const char* e = getenv("HAKMEM_ENV_SNAPSHOT"); if (e && *e) { g = (*e == '1') ? 1 : 0; } else { g = 0; } } return g != 0; } ``` ### 問題 1. **分岐コスト**: `if (g == -1)` が hot path で毎回評価 2. **予測ミス**: first call で branch misprediction 3. **関数呼び出しオーバーヘッド**: inline でも分岐は残る --- ## 設計 ### アプローチ: Constructor Init + Direct Read ```c // 新しい実装 static int g_hakmem_env_snapshot_gate = -1; __attribute__((constructor(101))) // priority 101: after libc init static void hakmem_env_snapshot_gate_init(void) { const char* e = getenv("HAKMEM_ENV_SNAPSHOT"); g_hakmem_env_snapshot_gate = (e && *e == '1') ? 1 : 0; } static inline bool hakmem_env_snapshot_enabled(void) { return g_hakmem_env_snapshot_gate != 0; // No branch (just load + compare) } ``` ### 利点 1. **分岐削減**: `if (g == -1)` 完全排除 2. **一度だけ**: `getenv()` は main() 前に 1 回のみ 3. **キャッシュ効率**: global read は TLS より高速(L1 hit 率高い) ### リスク | リスク | 対策 | |--------|------| | putenv() 後の変更が反映されない | bench_profile の `hakmem_env_snapshot_refresh_from_env()` で gate/snapshot を同期 | | constructor order | priority 101 で libc init 後を保証 | | fork() 安全性 | hakmem は fork-safe 設計済み | --- ## Box Theory(実装計画) ### L0: Env(戻せる) ``` HAKMEM_ENV_SNAPSHOT_CTOR=0/1 # default: 0(OFF) ``` - **ON (=1)**: Constructor init を使用(lazy check なし) - **OFF (=0)**: 従来の lazy init を使用(rollback 可能) ### L1: ENV Constructor Box(境界: 1 箇所) #### 変更対象 - `core/box/hakmem_env_snapshot_box.h` (変更) - `hakmem_env_snapshot_enabled()` を 2 つのモードで実装 - `core/box/hakmem_env_snapshot_box.c` (変更) - Constructor 関数を追加 --- ## 実装指示 ### Patch 1: Constructor Init Gate **ファイル**: `core/box/hakmem_env_snapshot_box.c` ```c // Global gate (not static local - avoids lazy init) int g_hakmem_env_snapshot_gate = -1; int g_hakmem_env_snapshot_ctor_mode = -1; // Constructor: run before main() __attribute__((constructor(101))) static void hakmem_env_snapshot_gate_ctor(void) { // Read HAKMEM_ENV_SNAPSHOT_CTOR (default OFF) const char* ctor_env = getenv("HAKMEM_ENV_SNAPSHOT_CTOR"); g_hakmem_env_snapshot_ctor_mode = (ctor_env && *ctor_env == '1') ? 1 : 0; if (g_hakmem_env_snapshot_ctor_mode) { // Constructor mode: init gate now const char* e = getenv("HAKMEM_ENV_SNAPSHOT"); g_hakmem_env_snapshot_gate = (e && *e == '1') ? 1 : 0; } } ``` ### Patch 2: Dual-Mode Enabled Check **ファイル**: `core/box/hakmem_env_snapshot_box.h` ```c // Global gate state (defined in .c) extern int g_hakmem_env_snapshot_gate; extern int g_hakmem_env_snapshot_ctor_mode; static inline bool hakmem_env_snapshot_enabled(void) { // Fast path: constructor mode (no branch except final compare) // Default is OFF, so ctor_mode==1 is UNLIKELY. if (__builtin_expect(g_hakmem_env_snapshot_ctor_mode == 1, 0)) { return g_hakmem_env_snapshot_gate != 0; } // Slow path: legacy lazy init (fallback) if (__builtin_expect(g_hakmem_env_snapshot_gate == -1, 0)) { const char* e = getenv("HAKMEM_ENV_SNAPSHOT"); g_hakmem_env_snapshot_gate = (e && *e == '1') ? 1 : 0; } return g_hakmem_env_snapshot_gate != 0; } ``` --- ## A/B テスト計画 ### Test Matrix | Profile | Iterations | Runs | Command | |---------|-----------|------|---------| | Mixed | 20M | 10 | `./bench_random_mixed_hakmem 20000000 400 1` | ### Baseline ```bash HAKMEM_PROFILE=MIXED_TINYV3_C7_SAFE HAKMEM_ENV_SNAPSHOT=1 HAKMEM_ENV_SNAPSHOT_CTOR=0 \ ./bench_random_mixed_hakmem 20000000 400 1 ``` ### Optimized ```bash HAKMEM_PROFILE=MIXED_TINYV3_C7_SAFE HAKMEM_ENV_SNAPSHOT=1 HAKMEM_ENV_SNAPSHOT_CTOR=1 \ ./bench_random_mixed_hakmem 20000000 400 1 ``` ### 判定基準 - **GO**: +0.5% 以上 - **NEUTRAL**: ±0.5%(研究箱維持) - **NO-GO**: -0.5% 以下 --- ## 期待値の根拠 **なぜ +0.5-1.5% か?** 1. **現在のオーバーヘッド**: 3.22% self% 2. **削減分**: lazy init check の分岐コスト(~10-15 cycles per call) 3. **削減率**: ~15-30% of 3.22% → 0.5-1.0% 4. **追加効果**: better branch prediction(warm path に分岐なし) --- ## 非目標 - snapshot refresh API の変更(putenv sync は既存 API で対応) - E1 の構造変更(consolidation は維持) - 他の ENV gate の constructor 化(E3-4 は hakmem_env_snapshot_enabled のみ)