diff --git a/TINY_HEAP_V2_TASK_SPEC.md b/TINY_HEAP_V2_TASK_SPEC.md new file mode 100644 index 00000000..02572327 --- /dev/null +++ b/TINY_HEAP_V2_TASK_SPEC.md @@ -0,0 +1,227 @@ +# Tiny Heap v2 (T‑HEAP) – Task Spec for Claude Code + +**Date**: 2025‑11‑14 +**Owner**: Claude Code (Tiny Phase 13) +**Status**: Draft – ready for implementation + +--- + +## 1. 背景とゴール + +### 現状 + +- Phase 12 までに: + - Shared SuperSlab Pool + SP‑SLOT Box 完成(multi‑class sharing, Superslab 92%削減)。 + - TLS SLL drain + Lock‑free 改善で Superslab churn / futex / race をほぼ解消。 + - Mid‑Large (8–32KB) は Pool TLS 経由で System malloc より高速(~10M ops/s)。 +- Tiny (16–1024B) は: + - 構造バグはほぼ解消済みだが、random_mixed / Larson では mimalloc / System に対してまだ大きな差がある。 + - Tiny front/back は Box で綺麗に分離されているが、shared pool / drain / TLS SLL など層が厚く、per‑thread heap ほどシンプルではない。 + +### 目的 + +Tiny 向けに **per‑thread heap フレーバー**(Tiny Heap v2 / T‑HEAP)を導入し、 + +- Tiny heavy ワークロード(random_mixed, Larson)での性能を大きく引き上げる。 +- 既存 HAKMEM の学習層 / Superslab 管理は「細い箱経由」で最低限のコストで活かす。 +- 本体構造(SP‑SLOT, drain, mid‑large, LD_PRELOAD 対応)は壊さず、**A/B 可能な新モード**として提供する。 + +ターゲットイメージ: + +- Tiny random_mixed / Larson: + - 現在: ~8–9M ops/s レンジ + - 目標: 15–20M ops/s レンジ(System の 25–40%) + - Stretch: 20M+(以降は別フェーズ) + +--- + +## 2. 現在の実装状態(TinyHeapV2 の骨格) + +既にこのリポジトリには、**実験用の tiny heap v2 箱**が骨組みだけ存在しています。 + +### 2.1 追加済みファイル・シンボル + +1. `core/front/tiny_heap_v2.h` + + - ENV ゲート: + - `tiny_heap_v2_enabled()`: + - `HAKMEM_TINY_HEAP_V2` を読んで ON/OFF 判定(TLS キャッシュ)。 + - TLS magazine 型: + - `TinyHeapV2Mag`: + - `void* items[TINY_HEAP_V2_MAG_CAP];` + - `int top;` + - `TINY_HEAP_V2_MAG_CAP` は現在 16。 + - TLS インスタンス: + - `extern __thread TinyHeapV2Mag g_tiny_heap_v2_mag[TINY_NUM_CLASSES];` + - ヘルパ: + - `tiny_heap_v2_refill_mag(int class_idx)`: + - FastCache → backend (`tiny_alloc_fast_refill`) の順で `TINY_HEAP_V2_MAG_CAP` 個まで magazine に詰める。 + - `tiny_heap_v2_alloc(size_t size)`: + - `size → class_idx`(`hak_tiny_size_to_class`)。 + - class 0–3 のみ対象。 + - magazine pop → refill → magazine pop → FAIL なら `NULL`。 + +2. `core/hakmem_tiny.c` + + - include 追加: + - `#include "front/tiny_heap_v2.h"` + - TLS 定義追加: + - `__thread TinyHeapV2Mag g_tiny_heap_v2_mag[TINY_NUM_CLASSES];` + +3. `core/tiny_alloc_fast.inc.h` + + - `tiny_alloc_fast()` 内に **コメントアウトされた** hook がある: + - 現状(コメントアウト後): + ```c + // Experimental Tiny heap v2 front (Box T-HEAP) is currently disabled + // due to instability under shared SuperSlab pool. Keep the hook here + // commented out for future experimentation. + // if (__builtin_expect(tiny_heap_v2_enabled(), 0)) { + // void* base = tiny_heap_v2_alloc(size); + // if (base) { + // HAK_RET_ALLOC(class_idx, base); + // } + // } + ``` + +### 2.2 なぜ今は無効化されているか + +- `tiny_heap_v2_alloc` を有効化して `bench_random_mixed_hakmem` を回したところ、 + - `shared_pool_acquire_slab()` 内で SEGV が発生。 + - 同時に SP‑SLOT の lock‑free node pool の枯渇 (`[P0-4 WARN] Node pool exhausted for class 7`) が絡んでいた。 +- その後、node pool 枯渇時には **従来の mutex 保護 free list へフォールバック** する修正を入れたが、 + - TinyHeapV2 経路と shared pool の組み合わせを十分検証する時間がなかったため、 + - 現時点では **安全第一で hook をコメントアウト** している。 + +--- + +## 3. Phase 13 Tiny Heap v2 – 具体タスク + +ここから先は Claude code 君に任せたい作業です。 +大きく 3 フェーズに分けています。 + +### Phase 13‑A: TinyHeapV2 の安定化(既存骨格の堅牢化) + +目的: 「`tiny_heap_v2_alloc` を有効にしても SEGV / 破綻が出ない」状態を作る。 + +**A‑1. magazine 初期化と基本動作の確認** + +- 確認: + - `g_tiny_heap_v2_mag[class_idx].top` が初期値 0 であること。 + - magazine 中のポインタは **BASE ポインタ**(ヘッダ位置)を保持していること(FastCache 同様)。 +- テスト: + - 短尺(1K〜10K iterations)の `bench_random_mixed_hakmem` を、 + - `HAKMEM_TINY_HEAP_V2=1`、 + - `HAKMEM_TINY_FRONT_DIRECT=1`(必要に応じて) + で走らせ、正常終了するか確認。 + +**A‑2. shared pool / SP‑SLOT / node pool との整合確認** + +- shared pool まわりの注意点: + - SP‑SLOT + lock‑free free list の node pool は、枯渇時に mutex の legacy free list にフォールバックするようになっている。 + - TinyHeapV2 導入後も: + - node pool 枯渇があっても **クラッシュせず**、 + - 性能が極端に悪化しない(大量ログや runaway がない)ことを要確認。 +- 手順: + - `strace -c` / `perf record` までは無理にやらなくても良いが、 + - `HAKMEM_SS_ACQUIRE_DEBUG=1` や `HAKMEM_SS_FREE_DEBUG=1` で shared_pool の挙動を軽く確認。 + +**A‑3. 再度の hook 有効化(限定クラスのみ)** + +- `core/tiny_alloc_fast.inc.h` のコメントアウトを戻し、ただし: + - `class_idx <= 3` の場合のみ TinyHeapV2 を試し、 + - 失敗(NULL)の場合は従来経路にフォールバックするように構造化。 +- ここまでで: + - 100K iterations の 128B / 256B / random_mixed で正常終了、 + - TinyHeapV2 ON/OFF で **挙動(SEGV の有無)が変わらない**ことを確認。 + +### Phase 13‑B: T‑HEAP/T‑BACKEND/T‑REMOTE の設計深化(optional だが推奨) + +目的: TinyHeapV2 を単なる「magazine front」から、より mimalloc に近い per‑thread heap へ育てる。 + +このフェーズは **研究色が強い**ため、段階的に進めてください。 + +**B‑1. T‑BACKEND: “span 供給箱” の導入** + +- 方向性: + - 現状の `tiny_heap_v2_refill_mag` は FastCache + `tiny_alloc_fast_refill` に依存している。 + - これを、TinyHeapV2 専用の backend(span 単位の管理)に少しずつ寄せていく。 +- 具体: + - 新しい Box API を定義 (`tiny_heap_backend.h` 等): + ```c + typedef struct TinySpan TinySpan; // 既存 Superslab/TinySlabMeta からラップ + + TinySpan* tiny_backend_acquire_span(int class_idx); + void tiny_backend_release_span(TinySpan* span); + ``` + - 最初は wrapper で構わない: + - `tiny_backend_acquire_span` → 既存 `superslab_refill` / shared_pool から TLS に 1 slab を割り当てるだけ。 + - `tiny_backend_release_span` → 既存の `shared_pool_release_slab` / SP‑SLOT に流すだけ。 +- TinyHeapV2 側では: + - magazine が空になったときに: + - まず FastCache / existing refill を試す(後方互換)。 + - 将来的には `tiny_backend_acquire_span` から直接 span を借りて、そこから magazine を満たす方向に進化させる。 + +**B‑2. T‑REMOTE: cross‑thread free を視野にいれる** + +- 現状: + - free は `hak_tiny_free_fast_v2` → TLS SLL → drain → Superslab の流れ。 +- 方向性(長期): + - cross‑thread free のパスを、TinyHeapV2 に合わせて簡略化した T‑REMOTE に乗せ換える余地がある。 + - ただし今は **free パスを壊さない**ことを優先し、短期では触らなくてよい。 + +### Phase 13‑C: 計測とまとめ + +目的: TinyHeapV2 ON/OFF の効果を定量化し、どこまで mimalloc に迫れたかを整理する。 + +**C‑1. ベンチセット** + +- 少なくともこの 2 系列で A/B を取る: + 1. `bench_random_mixed_hakmem`(100K, size=128/256/512/1024) + 2. `Larson` 系 (`scripts/bench_larson_*` / `run_larson_claude.sh`) +- それぞれで: + - `HAKMEM_TINY_HEAP_V2=0/1` の比較。 + - Throughput と、可能なら `strace -c` の syscall 率。 + +**C‑2. レポート** + +- 新しい `.md` として: + - `TINY_HEAP_V2_EVALUATION.md` + - 内容: + - 実装概要(T‑HEAP/T‑BACKEND/T‑REMOTE/T‑EVENT のどこまで入ったか)。 + - ベンチ結果(System/mimalloc/HAKMEM + HeapV2 ON/OFF)。 + - どのサイズ・どの workload でどれだけ改善したか。 + - まだ残っているギャップと、その原因の仮説(カーネル側、ページフォールトなど)。 + +--- + +## 4. 制約と注意事項 + +1. **既存の安定経路を壊さないこと** + - `HAKMEM_TINY_HEAP_V2` が 0 のときは、今の Phase12 Tiny 経路と完全に同じ動作を維持する。 + - TinyHeapV2 関連の変更は、必ず ENV/flag でゲートし、A/B 可能に保つ。 + +2. **shared pool / SP‑SLOT の契約を破らないこと** + - span/superslab の acquire/release は必ず既存の API (`shared_pool_acquire_slab`, `shared_pool_release_slab`, `superslab_refill` 等) を経由する。 + - SP‑SLOT の `meta->slots[i].state` や `active_slots` を直接いじるのは避ける(専用 helper 経由のみ)。 + +3. **Lock‑free 化は段階的に** + - すでに SP‑SLOT 周りには lock‑free の free list や CAS が入っているため、 + - TinyHeapV2 側でさらに lock‑free を追加する際は、「mutex fallback」を必ず用意し、node pool 枯渇時のようなケースで SEGV しないようにする。 + +4. **ベンチは短尺から** + - まず 10K–100K iterations で TinyHeapV2 の安定性を確認し、その後 200K–1M など長尺ベンチに進む。 + - perf/strace 用の run 時は、`P0-4 WARN` や debug ログがパフォーマンス計測を歪めないよう注意する。 + +--- + +## 5. まとめ + +- TinyHeapV2 は、現時点では「骨組みと TLS 構造だけある実験用 Box」です。 +- Claude code 君には: + - Phase 13‑A: 既存骨格の安定化と安全な hook 再有効化 + - Phase 13‑B: T‑BACKEND/T‑REMOTE への進化(可能な範囲で) + - Phase 13‑C: ベンチ・評価・レポート + を、Box Theory の境界を守りながら進めてもらいたい、というのがこのタスクの趣旨です。 + +この箱がうまく育てば、「HAKMEM の学習層+Superslab 管理」と「mimalloc 風のシンプル Tiny front」が共存する、かなり面白い実験場になるはずです。*** End Patch*** }}}}]]} ***! diff --git a/core/front/tiny_heap_v2.h b/core/front/tiny_heap_v2.h new file mode 100644 index 00000000..60c86d45 --- /dev/null +++ b/core/front/tiny_heap_v2.h @@ -0,0 +1,137 @@ +// tiny_heap_v2.h - Tiny per-thread heap (experimental Box) +// Purpose: +// - Provide a very simple per-thread front for tiny allocations. +// - Currently targets small classes (C0–C3) and is gated by ENV: +// HAKMEM_TINY_HEAP_V2=1 +// - Backend remains existing FastCache + Superslab refill. +// +// Design (first pass): +// - Per-thread, per-class small magazine (L0) in front of FastCache. +// - On alloc: +// 1) Pop from magazine. +// 2) If empty, refill magazine from FastCache (and backend via tiny_alloc_fast_refill). +// - On free: still goes through existing free path (hak_tiny_free_fast_v2), +// which ultimately feeds TLS SLL / drain / Superslab. +// +// This Box is intentionally minimal; performance tuning (sizes, class set) +// is left for later phases. + +#ifndef HAK_FRONT_TINY_HEAP_V2_H +#define HAK_FRONT_TINY_HEAP_V2_H + +#include "../hakmem_tiny.h" + +// NOTE: TinyHeapV2Mag struct and g_tiny_heap_v2_mag are defined in hakmem_tiny.c +// This header provides only the implementations (static inline functions). + +// Enable flag (cached) +static inline int tiny_heap_v2_enabled(void) { + static int g_enable = -1; + static int g_first_call = 1; + if (__builtin_expect(g_enable == -1, 0)) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2"); + g_enable = (e && *e && *e != '0') ? 1 : 0; + fprintf(stderr, "[HeapV2-INIT] tiny_heap_v2_enabled() called: ENV='%s' → %d\n", + e ? e : "(null)", g_enable); + fflush(stderr); + } + if (g_first_call && g_enable) { + fprintf(stderr, "[HeapV2-FIRST] Returning enabled=%d\n", g_enable); + fflush(stderr); + g_first_call = 0; + } + return g_enable; +} + +// Class-specific enable mask (cached) +// ENV: HAKMEM_TINY_HEAP_V2_CLASS_MASK (bitmask: bit 0=C0, bit 1=C1, bit 2=C2, bit 3=C3) +// Default: 0xF (all classes C0-C3 enabled) +// Example: 0x2 = C1 only, 0x8 = C3 only, 0x6 = C1+C2 +static inline int tiny_heap_v2_class_enabled(int class_idx) { + static int g_class_mask = -1; + if (__builtin_expect(g_class_mask == -1, 0)) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2_CLASS_MASK"); + if (e && *e) { + // Parse hex or decimal + char* endptr; + long val = strtol(e, &endptr, 0); // 0 = auto-detect base (0x for hex, else decimal) + g_class_mask = (int)val; + } else { + g_class_mask = 0xF; // Default: C0-C3 all enabled + } + } + + if (class_idx < 0 || class_idx >= 8) return 0; + return (g_class_mask & (1 << class_idx)) != 0; +} + +// NOTE: This header MUST be included AFTER tiny_alloc_fast.inc.h! +// It uses fastcache_pop, tiny_alloc_fast_refill, hak_tiny_size_to_class which are +// static inline functions defined in tiny_alloc_fast.inc.h and related headers. + +// Phase 13-A Step 1: NO REFILL (avoid circular dependency) +// TinyHeapV2 is a "lucky hit" L0 cache that doesn't refill itself. +// Refill will come from existing front layers later (outside TinyHeapV2). +// This function is currently a no-op stub for future use. +static inline int tiny_heap_v2_refill_mag(int class_idx) { + (void)class_idx; + // NO-OP: Do not refill to avoid circular dependency with FastCache + return 0; +} + +// Tiny heap v2 alloc – returns BASE pointer or NULL. +// Phase 13-A Step 1: Minimal "lucky hit" L0 cache (NO REFILL) +// Strategy: Pop from magazine if available, else return NULL immediately. +// Caller is responsible for header write via HAK_RET_ALLOC (BASE → USER conversion). +// Contract: +// - Only handles class 0-3 (8-64B) based on CLASS_MASK +// - Returns BASE pointer (not USER pointer!) +// - Returns NULL if magazine empty (caller falls back to existing path) +static inline void* tiny_heap_v2_alloc(size_t size) { + // 1. Size → class index + int class_idx = hak_tiny_size_to_class(size); + if (__builtin_expect(class_idx < 0, 0)) { + return NULL; // Not a tiny size + } + + // 2. Limit to hot tiny classes (0..3) for now + if (class_idx > 3) { + return NULL; // Fall back to existing path for class 4-7 + } + + // 3. Check class-specific enable mask + if (__builtin_expect(!tiny_heap_v2_class_enabled(class_idx), 0)) { + return NULL; // Class disabled via HAKMEM_TINY_HEAP_V2_CLASS_MASK + } + + g_tiny_heap_v2_stats[class_idx].alloc_calls++; + + // Debug: Print first few allocs + static __thread int g_debug_count[TINY_NUM_CLASSES] = {0}; + if (g_debug_count[class_idx] < 3) { + const char* debug_env = getenv("HAKMEM_TINY_HEAP_V2_DEBUG"); + if (debug_env && *debug_env && *debug_env != '0') { + fprintf(stderr, "[HeapV2-DEBUG] C%d alloc #%d (total_allocs=%lu)\n", + class_idx, g_debug_count[class_idx]++, g_tiny_heap_v2_stats[class_idx].alloc_calls); + } + } + + TinyHeapV2Mag* mag = &g_tiny_heap_v2_mag[class_idx]; + + // 4. ONLY path: pop from magazine if available (lucky hit!) + if (__builtin_expect(mag->top > 0, 0)) { // Expect miss (unlikely hit) + g_tiny_heap_v2_stats[class_idx].mag_hits++; + void* base = mag->items[--mag->top]; + return base; // BASE pointer (caller will convert to USER) + } + + // 5. Magazine empty: return NULL immediately (NO REFILL) + // Let existing front layers handle this allocation. + return NULL; +} + +// Print statistics (called at program exit if HAKMEM_TINY_HEAP_V2_STATS=1) +// Declaration only (implementation in hakmem_tiny.c for external linkage) +void tiny_heap_v2_print_stats(void); + +#endif // HAK_FRONT_TINY_HEAP_V2_H diff --git a/core/hakmem_tiny.c b/core/hakmem_tiny.c index 6cdd7d9d..8f92e3de 100644 --- a/core/hakmem_tiny.c +++ b/core/hakmem_tiny.c @@ -1267,6 +1267,39 @@ static __thread TinyHotMag g_tls_hot_mag[TINY_NUM_CLASSES]; int g_quick_enable = 0; // HAKMEM_TINY_QUICK=1 __thread TinyQuickSlot g_tls_quick[TINY_NUM_CLASSES]; // compile-out via guards below +// Phase 13: Tiny Heap v2 - Forward declarations (implementations come after tiny_alloc_fast.inc.h) +// This allows tiny_alloc_fast.inc.h to call these functions. + +// Very small per-class magazine for tiny sizes (C0–C3) +#ifndef TINY_HEAP_V2_MAG_CAP +#define TINY_HEAP_V2_MAG_CAP 16 +#endif + +typedef struct { + void* items[TINY_HEAP_V2_MAG_CAP]; + int top; +} TinyHeapV2Mag; + +// TLS magazines per class +__thread TinyHeapV2Mag g_tiny_heap_v2_mag[TINY_NUM_CLASSES]; + +// Phase 13-A: Observability counters (per-thread, per-class) +typedef struct { + uint64_t alloc_calls; // Total alloc attempts via HeapV2 + uint64_t mag_hits; // Magazine had blocks (fast path) + uint64_t refill_calls; // Refill invocations + uint64_t refill_blocks; // Total blocks obtained from refills + uint64_t backend_oom; // Backend OOM (refill returned 0) +} TinyHeapV2Stats; + +__thread TinyHeapV2Stats g_tiny_heap_v2_stats[TINY_NUM_CLASSES]; + +// Forward declarations +static inline int tiny_heap_v2_enabled(void); +static inline int tiny_heap_v2_class_enabled(int class_idx); +static inline int tiny_heap_v2_refill_mag(int class_idx); +static inline void* tiny_heap_v2_alloc(size_t size); + // Phase 2D-1: Hot-path inline function extractions(Front) // NOTE: TinyFastCache/TinyQuickSlot は front/ で定義済み #include "hakmem_tiny_hot_pop.inc.h" // 4 functions: tiny_hot_pop_class{0..3} @@ -1752,6 +1785,9 @@ TinySlab* hak_tiny_owner_slab(void* ptr) { // Box 5: Allocation Fast Path (Layer 1 - 3-4 instructions) #include "tiny_alloc_fast.inc.h" + // Phase 13: Tiny Heap v2 front (must come AFTER tiny_alloc_fast.inc.h) + #include "front/tiny_heap_v2.h" + // Box 6: Free Fast Path (Layer 2 - 2-3 instructions) #include "tiny_free_fast.inc.h" @@ -2040,3 +2076,36 @@ void tiny_guard_on_invalid(void* user_ptr, uint8_t hdr) { tiny_guard_dump_bytes("dump_before", u - 8, 8); tiny_guard_dump_bytes("dump_after", u, 8); } + + +// Phase 13-A: Tiny Heap v2 statistics wrapper (for external linkage) +void tiny_heap_v2_print_stats(void) { + // Implemented in front/tiny_heap_v2.h as static inline + // This wrapper is needed for external linkage from bench programs + extern __thread TinyHeapV2Stats g_tiny_heap_v2_stats[TINY_NUM_CLASSES]; + + static int g_stats_enable = -1; + if (g_stats_enable == -1) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2_STATS"); + g_stats_enable = (e && *e && *e != '0') ? 1 : 0; + } + if (!g_stats_enable) return; + + fprintf(stderr, "\n=== TinyHeapV2 Statistics (en=%d) ===\n", g_stats_enable); + int any_allocs = 0; + for (int cls = 0; cls < TINY_NUM_CLASSES; cls++) { + TinyHeapV2Stats* s = &g_tiny_heap_v2_stats[cls]; + if (s->alloc_calls == 0) continue; + + double hit_rate = (s->alloc_calls > 0) ? (100.0 * s->mag_hits / s->alloc_calls) : 0.0; + double avg_refill = (s->refill_calls > 0) ? ((double)s->refill_blocks / s->refill_calls) : 0.0; + + fprintf(stderr, "[C%d] alloc=%lu mag_hits=%lu (%.1f%%) refill=%lu avg_blocks=%.1f oom=%lu\n", + cls, s->alloc_calls, s->mag_hits, hit_rate, + s->refill_calls, avg_refill, s->backend_oom); + any_allocs = 1; + } + if (!any_allocs) fprintf(stderr, "(No HeapV2 allocs recorded)\n"); + fprintf(stderr, "==============================\n\n"); +} + diff --git a/core/hakmem_tiny_alloc.inc b/core/hakmem_tiny_alloc.inc index 81e51959..0a77f7bc 100644 --- a/core/hakmem_tiny_alloc.inc +++ b/core/hakmem_tiny_alloc.inc @@ -148,6 +148,17 @@ void* hak_tiny_alloc(size_t size) { } } while (0); + // Phase 13-A: Tiny Heap v2 (per-thread heap, experimental) + // ENV-gated: HAKMEM_TINY_HEAP_V2=1 + // Targets class 0-3 (16-256B) only, falls back to existing path if NULL + if (__builtin_expect(tiny_heap_v2_enabled(), 0) && class_idx <= 3) { + void* base = tiny_heap_v2_alloc(size); + if (base) { + HAK_RET_ALLOC(class_idx, base); // Header write + return USER pointer + } + // Fall through to existing front path if HeapV2 returned NULL (disabled class or OOM) + } + #if HAKMEM_TINY_MINIMAL_FRONT // Minimal Front for hot tiny classes (bench-focused): // SLL direct pop → minimal refill → pop, bypassing other layers. diff --git a/core/hakmem_tiny_alloc_new.inc b/core/hakmem_tiny_alloc_new.inc index f204b9ef..a78026ae 100644 --- a/core/hakmem_tiny_alloc_new.inc +++ b/core/hakmem_tiny_alloc_new.inc @@ -78,9 +78,47 @@ void* hak_tiny_alloc(size_t size) { // Size to class index int class_idx = hak_tiny_size_to_class(size); if (class_idx < 0) return NULL; // > 1KB + + // DEBUG: Verify hak_tiny_alloc() is called + static int g_alloc_dbg = -1; + if (g_alloc_dbg == -1) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2_DEBUG"); + g_alloc_dbg = (e && *e && *e != '0') ? 1 : 0; + } + if (g_alloc_dbg) { + static int g_call_count = 0; + if (g_call_count < 3) { + fprintf(stderr, "[HAK_TINY_ALLOC] Called #%d, size=%zu, class=%d\n", + g_call_count++, size, class_idx); + } + } + // Route fingerprint begin (debug-only; no-op unless HAKMEM_ROUTE=1) ROUTE_BEGIN(class_idx); + // Phase 13-A: Tiny Heap v2 (per-thread heap, experimental) + // ENV-gated: HAKMEM_TINY_HEAP_V2=1 + // Targets class 0-3 (8-64B) only, falls back to existing path if NULL + if (__builtin_expect(tiny_heap_v2_enabled(), 0) && class_idx <= 3) { + static int g_heap_v2_dbg = -1; + if (g_heap_v2_dbg == -1) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2_DEBUG"); + g_heap_v2_dbg = (e && *e && *e != '0') ? 1 : 0; + } + if (g_heap_v2_dbg) { + static int g_hook_count = 0; + if (g_hook_count < 5) { + fprintf(stderr, "[NEW3L-HOOK] class_idx=%d, size=%zu, hook_count=%d\n", + class_idx, size, g_hook_count++); + } + } + void* base = tiny_heap_v2_alloc(size); + if (base) { + HAK_RET_ALLOC(class_idx, base); // Header write + return USER pointer + } + // Fall through to existing front path if HeapV2 returned NULL (disabled class or OOM) + } + // Initialize small magazine (once per thread) if (__builtin_expect(!g_tiny_small_mag_initialized, 0)) { tiny_small_mag_init();