diff --git a/CURRENT_TASK.md b/CURRENT_TASK.md index c0db2af5..662e425a 100644 --- a/CURRENT_TASK.md +++ b/CURRENT_TASK.md @@ -1,113 +1,118 @@ -# CURRENT TASK – Performance Optimization Status +# CURRENT TASK - Larson Master Rebuild -**Last Updated**: 2025-11-25 -**Scope**: Random Mixed 16-1024B / Arena Allocator / Architecture Limit Analysis +**Last Updated**: 2025-11-26 +**Branch**: `larson-master-rebuild` +**Scope**: Larson バグ修正 + 安定化 + 性能回復 --- ## 🎯 現状サマリ -### ✅ Arena Allocator 実装完了 - mmap 95% 削減達成 +### ベースライン性能(larson-master-rebuild) +| Benchmark | Performance | Status | +|-----------|-------------|--------| +| Larson 1T | **51.35M ops/s** | ✅ 安定動作 | +| Random Mixed 10M | **62.18M ops/s** | ✅ 安定動作 | -| Metric | Before | After | Improvement | -|--------|--------|-------|-------------| -| mmap syscalls | 401 | 32 | -92% | -| munmap syscalls | 378 | 3 | -99% | -| Performance (10M) | ~60M ops/s | **68-70M ops/s** | +15% | - -### 現在の性能比較 (10M iterations) -``` -System malloc: 93M ops/s (baseline) -HAKMEM: 68-70M ops/s (73-76% of system malloc) -Gap: ~25% (構造的オーバーヘッド) -``` +### 旧 master の問題 +- Larson: **クラッシュ** (Step 2.5 バグ) +- Random Mixed: ~80M ops/s だったが Larson が壊れた --- -## 🔬 Phase 27 調査結果: アーキテクチャ限界の確認 +## 📋 作業計画 -### 試した最適化(すべて失敗) -| 最適化案 | 結果 | 効果 | -|---------|------|------| -| C5 TLS容量 2倍 (1024→2048) | 68-69M | 変化なし | -| Registry lookup削除 | 68-70M | 変化なし | -| Ultra SLIM 4-layer | ~69M | 変化なし | -| **Phase 27-A: Ultra-Inline (全size)** | **56-61M** | **-15% 悪化** ❌ | -| **Phase 27-B: Ultra-Inline (9-512B)** | **61-62M** | **-10% 悪化** ❌ | +### Phase 0: 安定ベースライン確立 ✅ DONE +- [x] `larson-fix` ブランチから `larson-master-rebuild` 作成 +- [x] Larson 動作確認 (51M ops/s) +- [x] Random Mixed 動作確認 (62M ops/s) -### Phase 27 失敗の原因 -- Workload の ~52% が headerless classes (cls 0: 1-8B, cls 7: 513-1024B) -- Headerless クラスをフィルタする条件分岐自体が overhead -- Classes 1-6 からの利益 < 条件分岐の overhead +### Phase 1: クリーンアップ & リファクタリング 🔄 IN PROGRESS +**目標**: 安定状態でコードベースを整理 -### 残り 25% ギャップの原因(構造的オーバーヘッド) -1. **Header byte オーバーヘッド** - 毎 alloc/free で 1 バイト書き込み/読み込み -2. **TLS SLL カウンタ** - 毎回 count++ / count-- (vs tcache: pointer のみ) -3. **多層分岐** - 4-5層 dispatch (vs tcache: 2-3層) +#### 1.1 Cherry-pick 済み(7コミット) +- [x] `9793f17d6` レガシーコード削除 (-1,159 LOC) +- [x] `cc0104c4e` テストファイル削除 (-1,750 LOC) +- [x] `416930eb6` バックアップファイル削除 (-1,072 KB) +- [x] `225b6fcc7` 死コード削除: UltraHot, RingCache等 (-1,844 LOC) +- [x] `2c99afa49` 学習システムバグドキュメント +- [x] `328a6b722` Larsonバグ分析更新 +- [x] `0143e0fed` CONFIGURATION.md 追加 -### 結論 -**68-70M ops/s が現アーキテクチャの実質的な限界**。System malloc の 93M ops/s に到達するには: -- Header-free design への全面的な見直し -- tcache 模倣(カウンタ削除、分岐削減) +#### 1.2 追加クリーンアップ(TODO) +- [ ] P0/P1/P2 ENV整理コミットの独立部分を手動ポート +- [ ] 不要なデバッグログ削除 +- [ ] ビルドシステム整理 -が必要だが、現時点では投資対効果が低い。 +### Phase 2: 性能最適化ポート 📊 PENDING +**目標**: 62M → 80M+ ops/s 回復 + +#### 2.1 簡単なチューニング(独立・低リスク) +- [ ] `e81fe783d` tiny_get_max_size inline化 (+2M) +- [ ] `04a60c316` Superslab/SharedPool チューニング (+1M) +- [ ] `392d29018` Unified Cache容量チューニング (+1M) +- [ ] `dcd89ee88` Stage 1 lock-free (+0.3M) + +#### 2.2 本丸(UNIFIED-HEADER) +- [ ] `472b6a60b` Phase UNIFIED-HEADER (+17%, C7ヘッダ統一) +- [ ] `d26519f67` UNIFIED-HEADERバグ修正 (+15-41%) +- [ ] `165c33bc2` Larsonフォールバック修正(必要なら) + +#### 2.3 スキップ対象 +- ❌ `03d321f6b` Phase 27 Ultra-Inline → **-10~15%回帰** +- ❌ Step 2.5関連コミット → **Larsonクラッシュの原因** + +### Phase 3: 検証 & マージ 🔀 PENDING +- [ ] Larson 10回平均ベンチマーク +- [ ] Random Mixed 10回平均ベンチマーク +- [ ] master ブランチ更新 --- -## 📁 主要な修正ファイル(Arena Allocator 実装) +## 🔍 根本原因分析 -- `core/box/ss_cache_box.inc:138-229` - SSArena allocator 追加 -- `core/box/tls_sll_box.h:509-561` - Release mode で recycle check オプショナル化 -- `core/tiny_free_fast_v2.inc.h:113-148` - Release mode で cross-check 削除 -- `core/hakmem_tiny_sll_cap_box.inc:8-25` - C5 容量を full capacity に変更 -- `core/hakmem_policy.c:24-30` - min_keep tuning -- `core/tiny_alloc_fast_sfc.inc.h:18-26` - SFC defaults tuning +### Larson クラッシュの原因 +**First Bad Commit**: `19c1abfe7` "Fix Unified Cache TLS SLL bypass" + +Step 2.5 が TLS_SLL_PUSH_DUP を「修正」するために追加されたが: +1. TLS_SLL_PUSH_DUP は実際には発生しない(ベースで10M回テスト済み) +2. Step 2.5 がマルチスレッド環境で cross-thread ownership 問題を引き起こす +3. 結論:**不要な「修正」が Larson を壊した** + +### 80M 達成の主要因 +| コミット | 内容 | 改善幅 | +|---------|------|--------| +| `472b6a60b` | UNIFIED-HEADER (C7統一) | **+17%** | +| `d26519f67` | UH バグ修正 | +15-41% | +| その他チューニング | inline, policy等 | +4-5M | --- -## 🗃 過去の問題と解決(参考) +## 📁 関連ファイル -### Arena Allocator 以前の状態 -- **Random Mixed (5M ops)**: ~56-60M ops/s, **mmap 418回** (mimalloc の 26倍) -- **根本原因**: SuperSlab = allocation単位 = cache単位 という設計ミスマッチ -- **問題**: ws=256 では Slab が 5-15% 使用率で停滞 → 完全 EMPTY にならない → LRU キャッシュ不発 → 毎回 mmap/munmap +### 修正対象 +- `core/front/tiny_unified_cache.c` - Step 2.5 なしのまま維持 +- `core/tiny_free_fast_v2.inc.h` - LARSON_FIX 関連 +- `core/box/ptr_conversion_box.h` - UNIFIED-HEADER で変更予定 -### Arena Allocator による解決 -- SuperSlab を OS 単位として扱う Arena allocator 実装 -- mmap 418回 → 32回 (-92%)、munmap 378回 → 3回 (-99%) -- 性能 60M → 68-70M ops/s (+15%) +### ドキュメント +- `LEARNING_SYSTEM_BUGS_P0.md` - 学習システムバグ記録 +- `CONFIGURATION.md` - ENV変数リファレンス +- `PROFILES.md` - 性能プロファイル --- -## 📊 他アロケータとのアーキテクチャ対応(参考) +## ✅ 完了マイルストーン -| HAKMEM | mimalloc | tcmalloc | jemalloc | -|--------|----------|----------|----------| -| SuperSlab (2MB) | Segment (~2MiB) | PageHeap | Extent | -| Slab (64KB) | Page (~64KiB) | Span | Run/slab | -| per-class freelist | pages_queue | Central freelist | bin/slab lists | -| Arena allocator | segment cache | PageHeap | extent_avail | +1. **Larson 安定化** - 51M ops/s で動作 ✅ +2. **Cherry-pick Phase 1** - 7コミット完了 ✅ +3. **ベースライン確立** - 62M/51M で安定 ✅ --- -## 🚀 将来の可能性(長期) +## 🎯 次のアクション -### Slab-level EMPTY Recycling(未実装) -- **Goal**: Slab を cross-class で再利用可能にする -- **設計**: EMPTY slab を lock-free stack で管理、alloc 時に class_idx を動的割り当て -- **期待効果**: メモリ効率向上(ただし性能向上は限定的) - -### Abandoned SuperSlab(MT 用、未実装) -- **Goal**: スレッド終了後のメモリを他スレッドから reclaim -- **設計**: mimalloc の abandoned segments 相当 -- **実装タイミング**: MT ワークロードで必要になってから - ---- - -## ✅ 完成したマイルストーン - -1. **Arena Allocator 実装** - mmap 95% 削減達成 ✅ -2. **Phase 27 調査** - アーキテクチャ限界の確認 ✅ -3. **性能 68-70M ops/s** - System malloc の 73-76% に到達 ✅ - -**現在の推奨**: 68-70M ops/s を baseline として受け入れ、他のワークロード(Mid-Large, Larson 等)の最適化に注力する。 +1. **Phase 1.2**: 追加クリーンアップ作業 +2. **Phase 2.1**: 簡単なチューニングコミットをポート +3. **Phase 2.2**: UNIFIED-HEADER を慎重にポート +4. **Phase 3**: 検証 & master 更新 diff --git a/core/box/front_gate_classifier.d b/core/box/front_gate_classifier.d index 2db2743d..c694ea8b 100644 --- a/core/box/front_gate_classifier.d +++ b/core/box/front_gate_classifier.d @@ -15,7 +15,8 @@ core/box/front_gate_classifier.o: core/box/front_gate_classifier.c \ core/box/../hakmem_build_flags.h core/box/../hakmem_internal.h \ core/box/../hakmem.h core/box/../hakmem_config.h \ core/box/../hakmem_features.h core/box/../hakmem_sys.h \ - core/box/../hakmem_whale.h core/box/../hakmem_tiny_config.h + core/box/../hakmem_whale.h core/box/../hakmem_tiny_config.h \ + core/box/../pool_tls_registry.h core/box/front_gate_classifier.h: core/box/../tiny_region_id.h: core/box/../hakmem_build_flags.h: @@ -42,3 +43,4 @@ core/box/../hakmem_features.h: core/box/../hakmem_sys.h: core/box/../hakmem_whale.h: core/box/../hakmem_tiny_config.h: +core/box/../pool_tls_registry.h: diff --git a/core/hakmem_libc.c b/core/hakmem_libc.c deleted file mode 100644 index 66007f3c..00000000 --- a/core/hakmem_libc.c +++ /dev/null @@ -1,18 +0,0 @@ -// HAKMEM libc wrapper for fallback allocations -#include "hakmem_internal.h" - -void* hkm_libc_malloc(size_t size) { - return malloc(size); -} - -void* hkm_libc_calloc(size_t count, size_t size) { - return calloc(count, size); -} - -void* hkm_libc_realloc(void* ptr, size_t size) { - return realloc(ptr, size); -} - -void hkm_libc_free(void* ptr) { - free(ptr); -} diff --git a/hakmem.d b/hakmem.d index 9fa01f74..c18c4f5e 100644 --- a/hakmem.d +++ b/hakmem.d @@ -22,11 +22,11 @@ hakmem.o: core/hakmem.c core/hakmem.h core/hakmem_build_flags.h \ core/box/hak_kpi_util.inc.h core/box/hak_core_init.inc.h \ core/hakmem_phase7_config.h core/box/ss_hot_prewarm_box.h \ core/box/hak_alloc_api.inc.h core/box/../hakmem_tiny.h \ - core/box/../hakmem_smallmid.h core/box/hak_free_api.inc.h \ - core/hakmem_tiny_superslab.h core/box/../tiny_free_fast_v2.inc.h \ - core/box/../tiny_region_id.h core/box/../hakmem_build_flags.h \ - core/box/../hakmem_tiny_config.h core/box/../box/tls_sll_box.h \ - core/box/../box/../hakmem_tiny_config.h \ + core/box/../hakmem_smallmid.h core/box/../pool_tls.h \ + core/box/hak_free_api.inc.h core/hakmem_tiny_superslab.h \ + core/box/../tiny_free_fast_v2.inc.h core/box/../tiny_region_id.h \ + core/box/../hakmem_build_flags.h core/box/../hakmem_tiny_config.h \ + core/box/../box/tls_sll_box.h core/box/../box/../hakmem_tiny_config.h \ core/box/../box/../hakmem_build_flags.h core/box/../box/../tiny_remote.h \ core/box/../box/../tiny_region_id.h \ core/box/../box/../hakmem_tiny_integrity.h \ @@ -104,6 +104,7 @@ core/box/ss_hot_prewarm_box.h: core/box/hak_alloc_api.inc.h: core/box/../hakmem_tiny.h: core/box/../hakmem_smallmid.h: +core/box/../pool_tls.h: core/box/hak_free_api.inc.h: core/hakmem_tiny_superslab.h: core/box/../tiny_free_fast_v2.inc.h: diff --git a/pool_tls_remote.d b/pool_tls_remote.d index beac3102..62a2cbfb 100644 --- a/pool_tls_remote.d +++ b/pool_tls_remote.d @@ -5,8 +5,8 @@ pool_tls_remote.o: core/pool_tls_remote.c core/pool_tls_remote.h \ core/hakmem_tiny_config.h core/ptr_track.h core/hakmem_super_registry.h \ core/hakmem_tiny_superslab.h core/superslab/superslab_types.h \ core/hakmem_tiny_superslab_constants.h core/superslab/superslab_inline.h \ - core/superslab/superslab_types.h core/tiny_debug_ring.h \ - core/tiny_remote.h + core/superslab/superslab_types.h core/superslab/../tiny_box_geometry.h \ + core/tiny_debug_ring.h core/tiny_remote.h core/pool_tls_remote.h: core/box/tiny_next_ptr_box.h: core/hakmem_tiny_config.h: @@ -23,5 +23,6 @@ core/superslab/superslab_types.h: core/hakmem_tiny_superslab_constants.h: core/superslab/superslab_inline.h: core/superslab/superslab_types.h: +core/superslab/../tiny_box_geometry.h: core/tiny_debug_ring.h: core/tiny_remote.h: