Files
hakmem/docs/analysis/PHASE42_RUNTIME_FIRST_METHOD_INSTRUCTIONS.md
Moe Charm (CI) 7adbcdfcb6 Phase 54-60: Memory-Lean mode, Balanced mode stabilization, M1 (50%) achievement
## Summary

Completed Phase 54-60 optimization work:

**Phase 54-56: Memory-Lean mode (LEAN+OFF prewarm suppression)**
- Implemented ss_mem_lean_env_box.h with ENV gates
- Balanced mode (LEAN+OFF) promoted as production default
- Result: +1.2% throughput, better stability, zero syscall overhead
- Added to bench_profile.h: MIXED_TINYV3_C7_BALANCED preset

**Phase 57: 60-min soak finalization**
- Balanced mode: 60-min soak, RSS drift 0%, CV 5.38%
- Speed-first mode: 60-min soak, RSS drift 0%, CV 1.58%
- Syscall budget: 1.25e-7/op (800× under target)
- Status: PRODUCTION-READY

**Phase 59: 50% recovery baseline rebase**
- hakmem FAST (Balanced): 59.184M ops/s, CV 1.31%
- mimalloc: 120.466M ops/s, CV 3.50%
- Ratio: 49.13% (M1 ACHIEVED within statistical noise)
- Superior stability: 2.68× better CV than mimalloc

**Phase 60: Alloc pass-down SSOT (NO-GO)**
- Implemented alloc_passdown_ssot_env_box.h
- Modified malloc_tiny_fast.h for SSOT pattern
- Result: -0.46% (NO-GO)
- Key lesson: SSOT not applicable where early-exit already optimized

## Key Metrics

- Performance: 49.13% of mimalloc (M1 effectively achieved)
- Stability: CV 1.31% (superior to mimalloc 3.50%)
- Syscall budget: 1.25e-7/op (excellent)
- RSS: 33MB stable, 0% drift over 60 minutes

## Files Added/Modified

New boxes:
- core/box/ss_mem_lean_env_box.h
- core/box/ss_release_policy_box.{h,c}
- core/box/alloc_passdown_ssot_env_box.h

Scripts:
- scripts/soak_mixed_single_process.sh
- scripts/analyze_epoch_tail_csv.py
- scripts/soak_mixed_rss.sh
- scripts/calculate_percentiles.py
- scripts/analyze_soak.py

Documentation: Phase 40-60 analysis documents

## Design Decisions

1. Profile separation (core/bench_profile.h):
   - MIXED_TINYV3_C7_SAFE: Speed-first (no LEAN)
   - MIXED_TINYV3_C7_BALANCED: Balanced mode (LEAN+OFF)

2. Box Theory compliance:
   - All ENV gates reversible (HAKMEM_SS_MEM_LEAN, HAKMEM_ALLOC_PASSDOWN_SSOT)
   - Single conversion points maintained
   - No physical deletions (compile-out only)

3. Lessons learned:
   - SSOT effective only where redundancy exists (Phase 60 showed limits)
   - Branch prediction extremely effective (~0 cycles for well-predicted branches)
   - Early-exit pattern valuable even when seemingly redundant

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-17 06:24:01 +09:00

98 lines
2.9 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 42 — Runtime-firstperf → asm最適化手順
Phase 40/41 の教訓:
- **asm に存在する ≠ 実行される**dead code でも callsite は残り得る)
- “gate 定数化/削除” は **layout tax** で符号反転する
Phase 42 は手順を固定して、**実行されている hot spot だけ**を触る。
---
## 目的
FAST build`make perf_fast`)の Mixed で、実行中の固定税gate/branch/indirectionを削って **+0.5% 以上**を狙う。
判定build-level / FAST:
- **GO**: +0.5% 以上Mixed 10-run mean
- **NEUTRAL**: ±0.5%
- **NO-GO**: -0.5% 以下revert
**layout リスクが高い変更**(関数の大きな再配置、広範囲の `#if` 追加、dead code 除去など)は閾値を **+1.0%** に引き上げる。
---
## Step 0: ベースライン固定(必須)
1) `make perf_fast` を 1回回して baselineFASTを記録。
2) baseline を `docs/analysis/PHASE42_RUNTIME_FIRST_METHOD_RESULTS.md` に貼る(まずは baseline だけ)。
---
## Step 1: Runtime profiling実行中の Top を確定)
目的: 「実行されていない最適化」を踏まない。
1) perf recordFAST binary
```sh
perf record -F 99 -g -- ./bench_random_mixed_hakmem_minimal 20000000 400 1
```
2) perf report実行されている上位だけを見る
```sh
perf report --no-children | head -120
```
ルール:
- **Top 50 に入っていないものは触らない**
- gate の候補は「関数名」で perf 上位に出たものだけ(例: `*_enabled`, `*_mode`, `*_snapshot`
---
## Step 2: asm inspectionTop 50 の候補だけ)
目的: “既に最適化されている/呼び出しが消えている” を避ける。
```sh
objdump -d ./bench_random_mixed_hakmem_minimal | rg -n "<SYMBOL>|call.*<SYMBOL>" -n
```
判定:
- **asm で branch/call が見える** → Step 3 へ
- **asm に出ない**inlined/消滅)→ skip触らない
---
## Step 3: 最小パッチで “呼び出し回数” を減らす(優先)
いきなり gate を定数化しない。まず “呼び出さない形” にする。
典型:
- 悪い: `if (gate() && cheap_pred) ...`(常に gate が呼ばれる)
- 良い: `if (cheap_pred && gate()) ...`cheap で弾けるなら gate を呼ばない)
これは layout 変化が小さく、勝ちやすい。
---
## Step 4: 最後の手段として定数化BENCH_MINIMAL 限定)
条件:
- Step 1 で “実行されている”
- Step 2 で asm に branch/call が残っている
- Step 3 で削れないcheap_pred が無い)
実装:
- `#if HAKMEM_BENCH_MINIMAL` の中だけで return constantStandard/OBSERVE は無傷)
---
## Step 5: A/BFAST 10-run
必ずこれを正として使う:
- `make perf_fast`
結果mean/median`docs/analysis/PHASE42_RUNTIME_FIRST_METHOD_RESULTS.md` に追記して確定判定。