Files
hakmem/docs/analysis/PHASE3_CACHE_LOCALITY_NEXT_INSTRUCTIONS.md
Moe Charm (CI) 4c4796a1f8 Phase 2 B4: Documentation & Instruction Creation (Phase 2→3 Transition)
Documentation Created:
- docs/analysis/PHASE2_STRUCTURAL_CHANGES_NEXT_INSTRUCTIONS.md: Phase 2 完了レポート (B3+B4累積 +4.4%)
- docs/analysis/PHASE3_CACHE_LOCALITY_NEXT_INSTRUCTIONS.md: Phase 3 開始指示(C3 Static Routing優先)

Verification Completed:
-  HAKMEM_WRAP_SHAPE=1 プリセット昇格(core/bench_profile.h:67)
-  wrapper_env_refresh_from_env() 実装済み(core/box/wrapper_env_box.c:49-64)
-  malloc_cold() lock_depth 対称性確認(全 return 経路で g_hakmem_lock_depth--)
-  A/B テスト結果: Mixed +1.47% (≥+1.0% GO threshold)

Summary:
  B3 routing shape:  +2.89%
  B4 wrapper shape:  +1.47%
  ─────────────────
  Estimated total:   ~+4.4%

Next Phase: Phase 3 (Cache Locality, +12-22%)
- Priority: C3 (Static Routing) - bypass policy_snapshot, +5-8% expected
- Profile: perf top で malloc/policy_snapshot hot spot を特定推奨

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

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2025-12-13 17:32:34 +09:00

98 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Phase 3: Cache Locality 最適化(開始指示)
## 目標
**現状**: Mixed ~35.2M ops/s (B3+B4 後)
**目標**: 57-68M ops/s (+12-22%)
## Phase 3 構成
### C3優先度: 🔴 最高): Static Routing
**背景**:
- Mixed の perf top では malloc/policy_snapshot が hot
- 現在: 毎回 malloc 時に policy snapshot + learner evaluation → 大きな overhead
- 案: malloc_tiny_fast() 呼び出し前に "static route" を init 時決定
**実装ステップ**:
1. **Profiling現状把握**
```bash
perf record -F 99 ./bench_random_mixed_hakmem
perf top --call-graph=flame -p [pid]
# → malloc/policy_snapshot/learner がどの程度か確認
```
2. **Static Route Detection (init 時)**
- malloc_tiny_fast() が呼ばれる前に route を "決定"
- 対象: C0-C7 の class 別に「LEGACY が dominant か」を判定
- ENV gate: HAKMEM_STATIC_ROUTE=1/0 (default 0)
3. **Route Bypass**
```c
// 現在(毎回評価):
route = g_policy_learner->get_route(class_idx); // 高コスト
// C3 Staticinit 時決定):
if (static_route_enabled()) {
route = g_static_route[class_idx]; // cached, no learner
} else {
route = learner_route(...); // 従来通り
}
```
4. **期待**: +5-8%
5. **A/B Test**: Mixed 10-run + C6-heavy 5-run
### C1優先度: 🟡 中): TLS Cache Prefetch
**狙い**: policy_snapshot 実行前に g_small_policy_v7 をプリフェッチ
**実装**:
```c
__builtin_prefetch(g_small_policy_v7, 0, 3); // L1 cache, write intent
```
**期待**: +2-4%
### C2優先度: 🟡 中): Slab Metadata Cache Optimization
**狙い**: hot metadatapolicy, slab descriptorをより近い場所に配置
**期待**: +5-10%
## 実装フロー
```
1. C3Static Routing実装 & A/B test
├─ GO: default 化
└─ NO-GO: freeze
2. C1TLS Prefetch追加実装
└─ Cumulative test
3. C2Metadata Optimization
└─ Final A/B
```
## 安全性確認
- **LD mode**: static route は LD 環境では disablepolicy learner は LD で active
- **Lock depth**: malloc 側なので不要
- **Rollback**: ENV gate で即時 OFF 可能
## 次のアクション
**今すぐ**:
1. `perf record -F 99 ./bench_random_mixed_hakmem` を実行して hot spot 特定
2. policy_snapshot / learner evaluation の overhead 定量化
3. C3 static route detection の実装開始
**以降**:
- A/B テストMixed 10-runで +5-8% 確認
- C1/C2 の段階的導入
---
**背中が見える段階から、さらに奥深く。**