Files
hakmem/docs/analysis/PHASE60_ALLOC_PASSDOWN_SSOT_DESIGN_AND_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

3.0 KiB
Raw Blame History

Phase 60: Alloc Pass-Down SSOT重複スナップショット/ルート計算の排除)

目的:

  • 現状の層FastLane/Box群と学習層OFFを維持したまま、alloc 側の op-per-loop 冗長を削って +510% の積み上げを狙う。
  • 方針は Phase 19-6Cfree 側 pass-downと同型: 入口で 1回だけ計算し、下流へ引き回す(境界 1 箇所、単純化)。

スコープ:

  • 対象: alloc hot pathmalloc_tiny_fast.h / front_fastlane_try_alloc() 相当)
  • 非対象: アルゴリズム刷新segment/page 再設計、研究箱の物理削除layout tax リスク)

成功基準A/B:

  • Mixed 10-run meanFAST build+1.0% 以上 = GO
  • ±1.0% = NEUTRALfreeze、code cleanliness 目的で保持)
  • -1.0% 以下 = NO-GOrevert

計測の正:

  • BENCH_BIN=./bench_random_mixed_hakmem_minimal scripts/run_mixed_10_cleanenv.sh
  • 比較は 同一バイナリ + ENV トグル(別バイナリ比較は layout 混入)

Step 0: Runtime 実行確認(必須)

Phase 40/41 の教訓: ASM にあっても実行されない最適化は触らない。

  • perf report --no-children で top 50 に alloc 側の対象関数が入っていることを確認する。
  • 対象候補(例):
    • malloc_tiny_fast*
    • front_fastlane_try_alloc*
    • tiny_c7_ultra_alloc
    • unified_cache_pop_or_refill

Step 1: L0 ENV box戻せる

新規:

  • core/box/alloc_passdown_ssot_env_box.{h,c}or .h only

ENV:

  • HAKMEM_ALLOC_PASSDOWN_SSOT=0/1default: 0

Step 2: L1 “入口で 1回だけ” helperSSOT

設計:

  • alloc 入口で必要な情報(例: route_kind, use_tiny_heap, class_idx, policy_snapshot_ptr)を 1 回だけ確定する。
  • 下流の cold/slow/refill には 引数で渡す(再計算禁止)。

実装指針:

  • static inline helper でまとめる関数分割を増やさないlayout tax 回避)
  • 例:
    • alloc_compute_route_and_heap(class_idx, ...) -> {route_kind, use_tiny_heap}
    • alloc_select_handler(route_kind, ...)

Step 3: 下流の重複を除去pass-down

典型的に削れる冗長:

  • policy snapshot の二重取得
  • tiny_route_for_class() の複数回呼び出し
  • route kind → heap kind 判定の重複

注意:

  • 入口の branch を増やしすぎないPhase 43 の教訓: branch は store より高い)
  • “monolithic + early-exit” を優先し、noinline helper 乱立は避ける

Step 4: A/BMixed 10-run

OFF:

  • HAKMEM_ALLOC_PASSDOWN_SSOT=0

ON:

  • HAKMEM_ALLOC_PASSDOWN_SSOT=1

判定:

  • GO: +1.0% 以上
  • NEUTRAL: ±1.0%
  • NO-GO: -1.0% 以下(即 revert

Rollback

  • HAKMEM_ALLOC_PASSDOWN_SSOT=0(同一バイナリで切戻し)

Phase 60B/C 候補)

  • 60B: TLS “同じ値を 1回だけ読む” を徹底alloc/free 入口で snapshot/passdown
  • 60C: unified_cache_* の telemetryrelease atomics/relaxed counters見直しcompile-out