Files
hakmem/CLAUDE.md

680 lines
24 KiB
Markdown
Raw Normal View History

# HAKMEM Memory Allocator - Claude 作業ログ
このファイルは Claude との開発セッションで重要な情報を記録します。
## プロジェクト概要
**HAKMEM** は高性能メモリアロケータで、以下を目標としています:
- 平均性能で mimalloc 前後
- 賢い学習層でメモリ効率も狙う
- Mid-Large (8-32KB) で特に強い性能
---
## 📊 包括的ベンチマーク結果 (2025-11-02)
### 測定完了
- **Comprehensive Benchmark**: 21パターン (LIFO, FIFO, Random, Interleaved, Long/Short-lived, Mixed) × 4サイズ (16B, 32B, 64B, 128B)
- **Fragmentation Stress**: 50 rounds, 2000 live slots, mixed sizes
### 結果サマリー
```
Tiny (≤128B): HAKMEM 52.59 M/s vs System 135.94 M/s → -61.3% 💀
Fragment Stress: HAKMEM 4.68 M/s vs System 18.43 M/s → -75.0% 💥
Mid-Large (8-32KB): HAKMEM 167.75 M/s vs System 61.81 M/s → +171% 🏆
```
### 詳細レポート
- [`benchmarks/results/BENCHMARK_SUMMARY_2025_11_02.md`](benchmarks/results/BENCHMARK_SUMMARY_2025_11_02.md) - 総合まとめ
- [`benchmarks/results/comprehensive_comparison.md`](benchmarks/results/comprehensive_comparison.md) - 詳細比較表
### ベンチマーク実行方法
```bash
# ビルド
make bench_comprehensive_hakmem bench_comprehensive_system
make bench_fragment_stress_hakmem bench_fragment_stress_system
# 実行
./bench_comprehensive_hakmem # 包括的テスト (~5分)
./bench_fragment_stress_hakmem 50 2000 # フラグメンテーションストレス
```
### 重要な発見
1. **Tiny は構造的に System に劣る** (-60~-70%)
- すべてのパターン (LIFO/FIFO/Random/Interleaved) で劣る
- Magazine 層のオーバーヘッド、Refill コスト、フラグメンテーション耐性の弱さ
2. **Mid-Large は圧倒的に強い** (+108~+171%)
- SuperSlab の効率、L25 中間層、System の mmap overhead 回避
- HAKX 専用最適化で更に高速化可能
3. **System malloc fallback は不可**
- HAKMEM の存在意義がなくなる
- Tiny の根本的再設計が必要
### 次のアクション
- [ ] Tiny の根本原因分析 (なぜ System tcache に劣るのか?)
- [ ] Magazine 層の効率化検討
- [ ] Mid-Large (HAKX) の mainline 統合検討
---
## 開発履歴
### Phase 6-1.7: Box Theory Refactoring (2025-11-05) ✅
**目標:** Ultra-Simple Fast Path (3-4命令) による Larson ベンチマーク改善
**結果:** +64% 性能向上 🎉
#### 実装内容
- **Box 1 (Foundation)**: `core/tiny_atomic.h` - アトミック操作抽象化
- **Box 5 (Alloc Fast Path)**: `core/tiny_alloc_fast.inc.h` - TLS freelist 直接 pop (3-4命令)
- **Box 6 (Free Fast Path)**: `core/tiny_free_fast.inc.h` - TOCTOU-safe ownership check + TLS push
#### ビルド方法
**基本Box-refactor のみ):**
```bash
make box-refactor # Box 5/6 Fast Path 有効
./larson_hakmem 2 8 128 1024 1 12345 4
```
**Larson 最適化Box-refactor + 環境変数):**
```bash
make box-refactor
# デバッグモード(+64%
HAKMEM_TINY_REFILL_OPT_DEBUG=1 HAKMEM_TINY_TRACE_RING=0 HAKMEM_SAFE_FREE=0 \
HAKMEM_TINY_TLS_SLL=1 HAKMEM_TINY_TLS_LIST=0 HAKMEM_TINY_HOTMAG=0 \
HAKMEM_WRAP_TINY=1 HAKMEM_TINY_SS_ADOPT=1 \
./larson_hakmem 2 8 128 1024 1 12345 4
# 本番モード(+150%
HAKMEM_TINY_REFILL_COUNT_HOT=64 HAKMEM_TINY_FAST_CAP=16 \
HAKMEM_TINY_TRACE_RING=0 HAKMEM_SAFE_FREE=0 \
HAKMEM_TINY_TLS_SLL=1 HAKMEM_TINY_TLS_LIST=0 HAKMEM_TINY_HOTMAG=0 \
HAKMEM_WRAP_TINY=1 HAKMEM_TINY_SS_ADOPT=1 \
./larson_hakmem 2 8 128 1024 1 12345 4
```
**通常版(元のコード):**
```bash
make larson_hakmem # Box-refactor なし
```
#### 性能結果
| 設定 | Throughput | 改善 |
|------|-----------|------|
| 元のコード(デバッグモード) | 1,676,8xx ops/s | ベースライン |
| **Box-refactorデバッグモード** | **2,748,759 ops/s** | **+64% 🚀** |
| Box-refactor最適化モード | 4,192,128 ops/s | +150% 🏆 |
#### ChatGPT の評価
> **「グッドジョブ」**
>
> - 境界の一箇所化で安全性↑所有権→drain→bind を SlabHandle に集約)
> - ホットパス短縮(中間層を迂回)でレイテンシ↓・分岐↓
> - A213/A202 エラー3日間の詰まりを解決
> - 環境ブでA/B可能`g_sll_multiplier`, `g_sll_cap_override[]`
#### Batch Refill との統合
**Box-refactor は ChatGPT の Batch Refill 最適化と完全統合:**
```
Box 5: tiny_alloc_fast()
↓ TLS freelist pop (3-4命令)
↓ Miss
↓ tiny_alloc_fast_refill()
↓ sll_refill_small_from_ss()
↓ (自動マッピング)
↓ sll_refill_batch_from_ss() ← ChatGPT の最適化
↓ - trc_linear_carve() (batch 64個)
↓ - trc_splice_to_sll() (一度で splice)
g_tls_sll_head に補充完了
↓ Retry pop → Success!
```
**統合の効果:**
- Fast path: 3-4命令Box 5
- Refill path: Batch carving で64個を一気に補充ChatGPT 最適化)
- メモリ書き込み: 128回 → 2回-98%
- 結果: +64% 性能向上
#### 主要ファイル
- `core/tiny_atomic.h` - Box 1: アトミック操作
- `core/tiny_alloc_fast.inc.h` - Box 5: Ultra-fast alloc
- `core/tiny_free_fast.inc.h` - Box 6: Fast free with ownership validation
- `core/tiny_refill_opt.h` - Batch Refill helpers (ChatGPT)
- `core/hakmem_tiny_refill_p0.inc.h` - P0 Batch Refill 最適化 (ChatGPT)
- `Makefile` - `box-refactor` ターゲット追加
#### Feature Flag
- `HAKMEM_TINY_PHASE6_BOX_REFACTOR=1`: Box Theory Fast Path を有効化
- デフォルトflag なし): 元のコードが動作(後方互換性維持)
---
### Phase 6-2.1: ChatGPT Pro P0 Optimization (2025-11-05) ✅
**目標:** superslab_refill の O(n) 線形走査を O(1) ctz 化
**結果:** 内部効率改善、性能維持 (4.19M ops/s)
#### 実装内容
**1. P0 最適化 (ChatGPT Pro):**
- **O(n) → O(1) 変換**: 32スラブの線形スキャンを `__builtin_ctz()` で1命令化
- **nonempty_mask**: `uint32_t` ビットマスクbit i = slabs[i].freelist != NULL
- **効果**: `superslab_refill` CPU 29.47% → 25.89% (-12%)
**コード:**
```c
// Before (O(n)): 32 loads + 32 branches
for (int i = 0; i < 32; i++) {
if (slabs[i].freelist) { /* try acquire */ }
}
// After (O(1)): bitmap build + ctz
uint32_t mask = 0;
for (int i = 0; i < 32; i++) {
if (slabs[i].freelist) mask |= (1u << i);
}
while (mask) {
int i = __builtin_ctz(mask); // 1 instruction!
mask &= ~(1u << i);
/* try acquire slab i */
}
```
**2. Active Counter Bug Fix (ChatGPT Pro Ultrathink):**
- **問題**: P0 batch refill が `meta->used` を更新するが `ss->total_active_blocks` を更新しない
- **影響**: カウンタ不整合 → メモリリーク/不正回収
- **修正**: `ss_active_add(tls->ss, batch)` を freelist/linear carve の両方に追加
**3. Debug Overhead 削除 (Claude Task Agent Ultrathink):**
- **問題**: `refill_opt_dbg()` が debug=off でも atomic CAS を実行 → -26% 性能低下
- **修正**: `trc_pop_from_freelist()``trc_linear_carve()` から debug 呼び出しを削除
- **効果**: 3.10M → 4.19M ops/s (+35% 復帰)
#### 性能結果
| Version | Score | Change | Notes |
|---------|-------|--------|-------|
| BOX_REFACTOR baseline | 4.19M ops/s | - | 元のコード |
| P0 (buggy) | 4.19M ops/s | 0% | カウンタバグあり |
| P0 + active_add (debug on) | 3.10M ops/s | -26% | Debug overhead |
| **P0 + active_add + no debug** | **4.19M ops/s** | **0%** | 最終版 ✅ |
**内部改善 (perf):**
- `superslab_refill` CPU: 29.47% → 25.89% (-12%)
- 全体スループット: Baseline 維持 (debug overhead 削除で復帰)
#### 主要ファイル
- `core/hakmem_tiny_superslab.h` - nonempty_mask フィールド追加
- `core/hakmem_tiny_superslab.c` - nonempty_mask 初期化
- `core/hakmem_tiny_free.inc` - superslab_refill の ctz 最適化
- `core/hakmem_tiny_refill_p0.inc.h` - ss_active_add() 呼び出し追加
- `core/tiny_refill_opt.h` - debug overhead 削除
- `Makefile` - ULTRA_SIMPLE テスト結果を記録 (-15%, 無効化)
#### 重要な発見
---
### Phase 6-2.3: P0 batch refill active-counter fix (2025-11-07)
- 症状: 4T 起動直後に `free(): invalid pointer`。P0 batch refill 経路で freelist → TLS 移送時の active カウンタ加算漏れにより、後段で二重デクリメント→アンダーフロー→OOM→クラッシュ。
- 修正: `core/hakmem_tiny_refill_p0.inc.h` の freelist 移送分岐に `ss_active_add(tls->ss, from_freelist);` を追加。線形 carve 側も `ss_active_add(tls->ss, batch);` を明示。
- 結果: 4T デフォルト設定で安定(~0.84M ops/s。再現試行2回で同一スコア。
- 残課題: `HAKMEM_TINY_REFILL_COUNT_HOT=64` 設定で再発報告あり。class03 大量 refill と `FAST_CAP` の相互作用を調査予定。
- **ULTRA_SIMPLE テスト**: 3.56M ops/s (-15% vs BOX_REFACTOR)
- **両方とも同じボトルネック**: `superslab_refill` 29% CPU
- **P0 で部分改善**: 内部 -12% だが全体効果は限定的
- **Debug overhead の教訓**: Hot path に atomic 操作は禁物
---
Fix: SuperSlab guess loop & header magic SEGV (random_mixed/mid_large_mt) ## Problem bench_random_mixed_hakmem and bench_mid_large_mt_hakmem crashed with SEGV: - random_mixed: Exit 139 (SEGV) ❌ - mid_large_mt: Exit 139 (SEGV) ❌ - Larson: 838K ops/s ✅ (worked fine) Error: Unmapped memory dereference in free path ## Root Causes (2 bugs found by Ultrathink Task) ### Bug 1: Guess Loop (core/box/hak_free_api.inc.h:92-95) ```c for (int lg=21; lg>=20; lg--) { SuperSlab* guess=(SuperSlab*)((uintptr_t)ptr & ~mask); if (guess && guess->magic==SUPERSLAB_MAGIC) { // ← SEGV // Dereferences unmapped memory } } ``` ### Bug 2: Header Magic Check (core/box/hak_free_api.inc.h:115) ```c void* raw = (char*)ptr - HEADER_SIZE; AllocHeader* hdr = (AllocHeader*)raw; if (hdr->magic != HAKMEM_MAGIC) { // ← SEGV // Dereferences unmapped memory if ptr has no header } ``` **Why SEGV:** - Registry lookup fails (allocation not from SuperSlab) - Guess loop calculates 1MB/2MB aligned address - No memory mapping validation - Dereferences unmapped memory → SEGV **Why Larson worked but random_mixed failed:** - Larson: All from SuperSlab → registry hit → never reaches guess loop - random_mixed: Diverse sizes (8-4096B) → registry miss → enters buggy paths **Why LD_PRELOAD worked:** - hak_core_init.inc.h:119-121 disables SuperSlab by default - → SS-first path skipped → buggy code never executed ## Fix (2-part) ### Part 1: Remove Guess Loop File: core/box/hak_free_api.inc.h:92-95 - Deleted unsafe guess loop (4 lines) - If registry lookup fails, allocation is not from SuperSlab ### Part 2: Add Memory Safety Check File: core/hakmem_internal.h:277-294 ```c static inline int hak_is_memory_readable(void* addr) { unsigned char vec; return mincore(addr, 1, &vec) == 0; // Check if mapped } ``` File: core/box/hak_free_api.inc.h:115-131 ```c if (!hak_is_memory_readable(raw)) { // Not accessible → route to appropriate handler // Prevents SEGV on unmapped memory goto done; } // Safe to dereference now AllocHeader* hdr = (AllocHeader*)raw; ``` ## Verification | Test | Before | After | Result | |------|--------|-------|--------| | random_mixed (2KB) | ❌ SEGV | ✅ 2.22M ops/s | 🎉 Fixed | | random_mixed (4KB) | ❌ SEGV | ✅ 2.58M ops/s | 🎉 Fixed | | Larson 4T | ✅ 838K | ✅ 838K ops/s | ✅ No regression | **Performance Impact:** 0% (mincore only on fallback path) ## Investigation - Complete analysis: SEGV_ROOT_CAUSE_COMPLETE.md - Fix report: SEGV_FIX_REPORT.md - Previous investigation: SEGFAULT_INVESTIGATION_REPORT.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 17:34:24 +09:00
### Phase 6-2.3: Header Magic SEGV Fix (2025-11-07) ✅
**目標:** bench_random_mixed での SEGV を完全解消
**結果:** 100% 成功、全テスト通過、性能影響なし
#### 問題発見
- **症状**: `bench_random_mixed_hakmem` が SEGV (Exit 139)
- **Larson**: 動作 (838K ops/s)
- **原因**: `hdr->magic` デリファレンス時に未マップメモリアクセス
#### 根本原因 (Ultrathink 調査)
**未マップメモリのデリファレンス**
```c
// core/box/hak_free_api.inc.h:113-115 (修正前)
void* raw = (char*)ptr - HEADER_SIZE;
AllocHeader* hdr = (AllocHeader*)raw;
if (hdr->magic != HAKMEM_MAGIC) { // ← SEGV HERE
```
**問題のシナリオ:**
1. 混合サイズ割り当て (8-4096B)
2. 一部が SuperSlab registry lookup に失敗
3. Mid/L25 registry lookup も失敗
4. Raw header dispatch に到達
5. `ptr - HEADER_SIZE` が未マップメモリを指す
6. `hdr->magic` デリファレンス → **SEGV**
#### 実装内容
**1. メモリ安全性ヘルパー追加 (core/hakmem_internal.h:277-294):**
```c
static inline int hak_is_memory_readable(void* addr) {
#ifdef __linux__
unsigned char vec;
// mincore returns 0 if page is mapped, -1 (ENOMEM) if not
return mincore(addr, 1, &vec) == 0;
#else
return 1; // Conservative fallback
#endif
}
```
**2. Free パス修正 (core/box/hak_free_api.inc.h:113-131):**
```c
void* raw = (char*)ptr - HEADER_SIZE;
// CRITICAL FIX: Check if memory is accessible before dereferencing
if (!hak_is_memory_readable(raw)) {
// Memory not accessible, route to appropriate handler
if (!g_ldpreload_mode && g_invalid_free_mode) {
hak_tiny_free(ptr);
goto done;
}
extern void __libc_free(void*);
__libc_free(ptr);
goto done;
}
// Safe to dereference header now
AllocHeader* hdr = (AllocHeader*)raw;
```
#### 結果
| Test | Before | After | Change |
|------|--------|-------|--------|
| `larson_hakmem` | 838K ops/s | 838K ops/s | 0% ✅ |
| `bench_random_mixed` (2048B) | **SEGV** | 2.34M ops/s | **Fixed** 🎉 |
| `bench_random_mixed` (4096B) | **SEGV** | 2.58M ops/s | **Fixed** 🎉 |
| Stress test (10 runs) | **N/A** | All pass | **Stable** ✅ |
#### なぜ機能するか
1. **未マップメモリデリファレンスを防止**: mincore() でメモリアクセス可能性を事前確認
2. **既存ロジック保持**: エラーハンドリングはそのまま、安全性チェックのみ追加
3. **全エッジケース対応**:
- Tiny alloc (ヘッダーなし) → `tiny_free()` へルーティング
- Libc alloc (LD_PRELOAD) → `__libc_free()` へルーティング
- 有効なヘッダー → 通常処理
4. **最小コード変更**: 15行追加のみ
#### 性能影響
**mincore() オーバーヘッド**: ~50-100 cycles (システムコール)
**トリガー条件**:
- 全ての lookup (SS, Mid, L25) が失敗した場合のみ
- Larson: 0% (全て SS-first でキャッチ)
- Random Mixed: 1-3% (稀なフォールバック)
**測定結果**: **性能影響なし (0% regression)**
#### 主要ファイル
- `core/hakmem_internal.h:277-294` - `hak_is_memory_readable()` ヘルパー追加
- `core/box/hak_free_api.inc.h:113-131` - メモリアクセス可能性チェック追加
- `SEGV_FIX_REPORT.md` - 包括的修正レポート
- `FALSE_POSITIVE_SEGV_FIX.md` - 修正戦略ドキュメント
#### 今後の作業 (Optional)
**Root Cause 調査 (Phase 2):**
- なぜ一部の割り当てが registry lookup をエスケープするのか?
- SuperSlab registry の完全性確認
- レジストリルックアップ成功率の測定
**調査コマンド**:
```bash
# Registry trace 有効化
HAKMEM_SUPER_REG_REQTRACE=1 ./bench_random_mixed_hakmem 1000 2048 1234567
# Free route trace 有効化
HAKMEM_FREE_ROUTE_TRACE=1 ./bench_random_mixed_hakmem 1000 2048 1234567
```
**優先度**: Low (現在の修正は完全かつ高性能)
---
### Phase 6-2.2: Sanitizer Compatibility Fix (2025-11-07) ✅
**目標:** ASan/TSan ビルドの早期 SEGV を解消
**結果:** ASan 完全動作、TSan は Larson ベンチマーク自体の問題を発見
#### 問題発見
- **症状**: ASan/TSan 有効時に初期化前段階で SEGVconstructor すら動く前に落下)
- **通常ビルド**: 安定4.19M ops/s
- **Sanitizer ビルド**: 即座にクラッシュ(バックトレースすら出ない)
#### 根本原因Task Agent Ultrathink 調査)
**ASan 初期化中の `dlsym()``malloc()` → TLS 未初期化 SEGV**
```
1. Dynamic linker が ASan を初期化
2. ASan が dlsym("__isoc99_printf") を呼び出す
3. glibc dlsym() 内部で malloc() が発生
4. HAKMEM の malloc() wrapper が実行
5. g_hakmem_lock_depth (TLS) にアクセス
→ 💥 SEGV (TLS 未初期化)
```
**TLS 変数の完全インベントリ**: 50+ 個(レポート参照)
#### 実装内容
**Phase 1: 即座の修正1行変更✅**
1. **Makefile (line 810-828)**`-DHAKMEM_FORCE_LIBC_ALLOC_BUILD=1` を追加:
```diff
SAN_ASAN_ALLOC_CFLAGS = -O1 -g -fno-omit-frame-pointer -fno-lto \
-fsanitize=address,undefined -fno-sanitize-recover=all -fstack-protector-strong \
+ -DHAKMEM_FORCE_LIBC_ALLOC_BUILD=1
```
2. **core/tiny_fastcache.c (line 231-305)** - 統計出力を `FORCE_LIBC` でガード:
```diff
void tiny_fast_print_profile(void) {
+#ifndef HAKMEM_FORCE_LIBC_ALLOC_BUILD
// ... wrapper TLS 変数を参照する統計コード
+#endif
}
```
**理由**: `FORCE_LIBC_ALLOC_BUILD=1` 時は wrapper が無効化され、TLS 統計変数が定義されないためリンクエラー回避
#### 結果
| Target | Build | Runtime | Notes |
|--------|-------|---------|-------|
| `larson_hakmem_asan_alloc` | ✅ | ✅ | **4.29M ops/s** |
| `larson_hakmem_tsan_alloc` | ✅ | ❌ SEGV | Larson benchmark issue |
| `larson_hakmem_tsan` (libc) | ✅ | ❌ SEGV | **HAKMEM とは無関係** |
| `libhakmem_asan.so` | ✅ | 未テスト | LD_PRELOAD版 |
| `libhakmem_tsan.so` | ✅ | 未テスト | LD_PRELOAD版 |
**重要な発見**:
- **ASan**: 完全動作TLS 初期化順序問題を完全回避)
- **TSan**: Larson ベンチマーク自体と TSan の非互換性HAKMEM とは無関係)
- `larson_hakmem_tsan`allocator 無効版)も同じく SEGV
- Larson は C++ コードmimalloc-benchで thread 初期化に問題あり
#### 主要ファイル
- `Makefile:810-828` - Sanitizer ビルドフラグ修正
- `core/tiny_fastcache.c:231-305` - 統計出力ガード
- `SANITIZER_INVESTIGATION_REPORT.md` - 包括的調査レポート50+ TLS 変数リスト、詳細分析)
- `SANITIZER_PHASE1_RESULTS.md` - Phase 1 結果まとめ
#### 次のステップ(推奨順)
**Phase 2: Constructor Priority2-3日**
- `__attribute__((constructor(101)))` で TLS 早期初期化
- HAKMEM allocator を Sanitizer でテスト可能にする
- 完全な Sanitizer サポートを実現
**Phase 1.5: TSan 調査Optional**
- Larson ベンチマークの TSan 互換性を調査
- 代替ベンチマーク(`bench_random_mixed_hakmem` など)で TSan テスト
**使い方**:
```bash
# ASan ビルド(動作確認済み)
make asan-larson-alloc
./larson_hakmem_asan_alloc 1 1 128 1024 1 12345 1
# → Throughput = 4294477 ops/s ✅
# LD_PRELOAD 版
make asan-shared-alloc
LD_PRELOAD=./libhakmem_asan.so <your_app>
```
---
### Phase 6-2.3: Active Counter Bug Fix (2025-11-07) ✅
**目標:** 4T クラッシュ(`free(): invalid pointer`)の根本原因修正
**結果:** デフォルト設定で 4T 安定動作達成838K ops/s
#### 問題発見
- **症状**: HAKMEM 直リンク 4T で起動直後にクラッシュ
- **再現**: `./larson_hakmem 10 8 128 1024 1 12345 4` → Exit 134
- **エラー**: `free(): invalid pointer`, `superslab_refill returned NULL (OOM)`
- **性能**: 1T も System の 1/4838K vs 3.3M ops/s
#### 根本原因Ultrathink Task Agent 調査)
**Active Counter Double-Decrement in P0 Batch Refill**
`core/hakmem_tiny_refill_p0.inc.h:103` で freelist から TLS cache にブロックを移動する際、active counter をインクリメントし忘れていた:
```
1. Free → カウンタ減算 ✅
2. Remote drain → freelist に追加(カウンタ変更なし) ✅
3. P0 batch refill → TLS に移動(カウンタ増加忘れ)❌ ← バグ!
4. 次の Free → カウンタ減算 ❌ ← ダブルデクリメント!
```
**結果**: カウンタアンダーフロー → SuperSlab が「満杯」→ OOM → クラッシュ
#### 修正内容1行追加
**File:** `core/hakmem_tiny_refill_p0.inc.h:103`
```diff
trc_splice_to_sll(class_idx, &chain, &g_tls_sll_head[class_idx], &g_tls_sll_count[class_idx]);
-// NOTE: from_freelist は既に used/active 計上済みのブロックの再循環。
+// FIX: Blocks from freelist were decremented when freed, must increment when allocated
+ss_active_add(tls->ss, from_freelist);
```
**理由**: Freelist からの再割り当ては「free 状態 → allocated 状態」への遷移なので、active counter を増やす必要がある。
#### 検証結果
| 設定 | 修正前 | 修正後 | 改善 |
|------|--------|--------|------|
| 4T デフォルト | ❌ クラッシュ | ✅ 838,445 ops/s | 🎉 安定化 |
| 安定性2回 | - | ✅ 同一スコア | 再現性確認 |
#### 発見の経緯
- **Heisenbug**: Debug hooks ON で消失(タイミング依存の race condition
- **Load-dependent**: 256 chunks/thread = OK, 1024 = crash
- **Ready/Mailbox independent**: 設定に関係なくクラッシュ
#### 残課題
**`HAKMEM_TINY_REFILL_COUNT_HOT=64` でクラッシュ再発**
```bash
HAKMEM_TINY_REFILL_COUNT_HOT=64 ./larson_hakmem 10 8 128 1024 1 12345 4
# → Exit 134: class=4 で OOM
```
**暫定診断**:
- Class 0-3 が `want=64` で大量 refill → TLS cache 過剰蓄積
- Class 4 がメモリ不足 → OOM
- 原因候補: TLS cache サイズ制限不足、メモリリーク
**次のアクション**:
1. `HAKMEM_TINY_FAST_CAP` との相互作用調査
2. Valgrind でメモリリーク検出
3. デフォルト refill count 確認
#### 主要ファイル
- `core/hakmem_tiny_refill_p0.inc.h:103` - Active counter 修正
---
### Phase 5-B-Simple: Dual Free Lists + Magazine Unification (2025-11-02) ❌
- 目標: +15-23% → 実際: -71% ST, -35% MT
- Magazine unification 自体は良アイデアだが、capacity tuning と Dual Free Lists の組み合わせが失敗
- 詳細: [`HISTORY.md`](HISTORY.md)
### Phase 5-A: Direct Page Cache (2025-11-01) ❌
- Global cache による contention で -3~-7.7%
### Phase 2+1: Magazine + Registry optimizations (2025-10-29) ✅
- 成功: 性能改善達成
---
## 重要なドキュメント
- [`LARSON_GUIDE.md`](LARSON_GUIDE.md) - Larson ベンチマーク統合ガイド(ビルド・実行・プロファイル)
- [`HISTORY.md`](HISTORY.md) - 失敗した最適化の詳細記録
- [`CURRENT_TASK.md`](CURRENT_TASK.md) - 現在のタスク
- [`benchmarks/results/`](benchmarks/results/) - ベンチマーク結果
## 🔍 Tiny 性能分析 (2025-11-02)
### 根本原因発見
詳細レポート: [`benchmarks/results/TINY_PERFORMANCE_ANALYSIS.md`](benchmarks/results/TINY_PERFORMANCE_ANALYSIS.md)
**Fast Path が複雑すぎる:**
- System tcache: 3-4 命令
- HAKMEM: 何十もの分岐 + 複数の関数呼び出し
- Branch misprediction cost: 50-200 cycles (vs System の 15-40 cycles)
**改善案:**
1. **Option A: Ultra-Simple Fast Path (tcache風)** ⭐⭐⭐⭐⭐
- System tcache と同等の設計
- 3-4 命令の fast path
- 成功確率: 80%, 期間: 1-2週間
2. **Option C: Hybrid アプローチ** ⭐⭐⭐⭐
- Tiny: tcache風に再設計
- Mid-Large: 現行維持 (+171% の強みを活かす)
- 成功確率: 75%, 期間: 2-3週間
**推奨:** Option A → 成功したら Option C に発展
---
## 🚀 Phase 6: Learning-Based Tiny Allocator (2025-11-02~)
### 戦略決定
ユーザーの洞察: **「Mid-Large の真似をすればいい」**
**コンセプト: "Simple Front + Smart Back"**
- Front: Ultra-Simple Fast Path (System tcache 風、3-4 命令)
- Back: 学習層 (動的容量調整、hotness tracking)
### 実装プラン
**Phase 1 (1週間): Ultra-Simple Fast Path**
```c
// TLS Free List ベース (3-4 命令のみ!)
void* hak_tiny_alloc(size_t size) {
int cls = size_to_class_inline(size);
void** head = &g_tls_cache[cls];
void* ptr = *head;
if (ptr) {
*head = *(void**)ptr; // Pop
return ptr;
}
return hak_tiny_alloc_slow(size, cls);
}
```
目標: System の 70-80% (95-108 M ops/sec)
**Phase 2 (1週間): 学習層**
- Class hotness tracking
- 動的キャッシュ容量調整 (16-256 slots)
- Adaptive refill count (16-128 blocks)
目標: System の 80-90% (108-122 M ops/sec)
**Phase 3 (1週間): メモリ効率最適化**
- Cold classes のキャッシュ削減
- 目標: System 同等速度 + メモリで勝つ 🏆
### Mid-Large HAKX の成功パターンを適用
| 要素 | HAKX (Mid-Large) | Tiny への適用 |
|------|------------------|---------------|
| Fast Path | Direct SuperSlab pop | TLS Free List pop (3-4命令) ✅ |
| 学習層 | Size pattern 学習 | Class hotness 学習 ✅ |
| 専用最適化 | 8-32KB 専用 | Hot classes 優遇 ✅ |
| Batch 処理 | Batch allocation | Adaptive refill ✅ |
### 進捗
- [x] TODO リスト作成
- [x] CURRENT_TASK.md 更新
- [x] CLAUDE.md 更新
- [ ] Phase 1 実装開始
---
## 🛠️ ビルドシステムの改善 (2025-11-02)
### 問題発見: `.inc` ファイル更新時の再ビルド漏れ
**症状:**
- `.inc` / `.inc.h` ファイルを更新しても `libhakmem.so` が再ビルドされない
- ChatGPT が何度も最適化を実装したが、スコアが全く変わらなかった
- 原因: Makefile の依存関係に `.inc` ファイルが含まれていなかった
**影響:**
- タイムスタンプ確認で発覚: `libhakmem.so` が36分前のまま
- 古いバイナリで実行され続けていた
- エラーも出ないため気づきにくい(超危険!)
### 解決策: 自動依存関係生成 ✅
**実装内容:**
1. **自動依存関係生成: 導入済み** 〈採用〉
- gcc の `-MMD -MP` フラグで `.inc` ファイルも自動検出
- `.d` ファイル(依存関係情報)を生成
- メンテナンス不要、業界標準の方法
2. **build.sh毎回clean:** 必要なら追加可能
- 確実だが遅い
3. **smart_build.shタイムスタンプ検知で必要時のみclean:** 追加可能
- `.inc``.so` より新しければ自動 clean
4. **verify_build.shビルド後検証:** 追加可能
- ビルド後にバイナリが最新か確認
### ビルド時の注意点
**`.inc` ファイル更新時:**
- 自動依存関係生成により、通常は自動再ビルド
- 不安なら `make clean && make` を実行
**確認方法:**
```bash
# タイムスタンプ確認
ls -la --time-style=full-iso libhakmem.so core/*.inc core/*.inc.h
# 強制リビルド
make clean && make
```
### 効果確認 (2025-11-02)
**修正前:**
- どんな最適化を実装してもスコアが変わらない(~2.3-4.2M ops/s 固定)
**修正後 (`make clean && make` 実行):**
| モード | スコア (ops/s) | 変化 |
|--------|----------------|------|
| Normal | 2,229,692 | ベースライン |
| **TINY_ONLY** | **2,623,397** | **+18% 🎉** |
| LARSON_MODE | 1,459,156 | -35% (allocation 失敗) |
| ONDEMAND | 1,439,179 | -35% (allocation 失敗) |
→ 最適化が実際に反映され、スコアが変化するようになった!