Add TinyHeap class mask and extend routing

This commit is contained in:
Moe Charm (CI)
2025-12-07 22:49:28 +09:00
parent 9c68073557
commit a6991ec9e4
23 changed files with 433 additions and 211 deletions

View File

@ -36,7 +36,9 @@
#include "../hakmem_tiny.h" // For hak_tiny_size_to_class
#include "../box/tiny_front_hot_box.h" // Phase 4-Step2: Hot Path Box
#include "../box/tiny_front_cold_box.h" // Phase 4-Step2: Cold Path Box
#include "../box/tiny_c7_hotpath_box.h" // Optional: C7 専用ホット
#include "../box/tiny_c7_hotbox.h" // Optional: C7 専用ホットボック
#include "../box/tiny_heap_box.h" // TinyHeap 汎用 Box
#include "../box/tiny_heap_env_box.h" // ENV gate for TinyHeap front (A/B)
// Helper: current thread id (low 32 bits) for owner check
#ifndef TINY_SELF_U32_LOCAL_DEFINED
@ -99,9 +101,16 @@ static inline void* malloc_tiny_fast(size_t size) {
// 1. size → class_idx (inline table lookup, 1-2 instructions)
int class_idx = hak_tiny_size_to_class(size);
// Optional: C7 専用ホットパス(環境変数 HAKMEM_TINY_C7_HOT でON
if (__builtin_expect(class_idx == 7 && tiny_c7_hot_enabled(), 0)) {
return tiny_c7_alloc_hot(size);
// Optional: TinyHeap frontENV: HAKMEM_TINY_HEAP_BOX=1 + HAKMEM_TINY_HEAP_CLASSES bitmask
const int use_tiny_heap = (class_idx == 7)
? tiny_c7_heap_mode_enabled()
: tiny_heap_class_route_enabled(class_idx);
if (__builtin_expect(use_tiny_heap, 0)) {
tiny_heap_ctx_t* ctx = tiny_heap_ctx_for_thread();
if (class_idx == 7 && size == 1024) {
return tiny_c7_alloc_fast(size);
}
return tiny_heap_alloc_class_fast(ctx, class_idx, size);
}
// 2. Phase 4-Step2: Hot/Cold Path Box
@ -171,7 +180,7 @@ static inline int free_tiny_fast(void* ptr) {
}
#endif // !HAKMEM_BUILD_RELEASE
// Cross-thread free detection (Larson MT crash fix, ENV gated)
// Cross-thread free detection (Larson MT crash fix, ENV gated) + TinyHeap free path
{
static __thread int g_larson_fix = -1;
if (__builtin_expect(g_larson_fix == -1, 0)) {
@ -183,7 +192,10 @@ static inline int free_tiny_fast(void* ptr) {
#endif
}
if (__builtin_expect(g_larson_fix, 0)) {
const int use_tiny_heap = (class_idx == 7)
? tiny_c7_heap_mode_enabled()
: tiny_heap_class_route_enabled(class_idx);
if (__builtin_expect(g_larson_fix || use_tiny_heap, 0)) {
// Phase 12 optimization: Use fast mask-based lookup (~5-10 cycles vs 50-100)
SuperSlab* ss = ss_fast_lookup(base);
if (ss) {
@ -219,9 +231,22 @@ static inline int free_tiny_fast(void* ptr) {
return 1; // handled via remote queue
}
return 0; // remote push failed; fall back to normal path
} else if (__builtin_expect(use_tiny_heap, 0)) {
tiny_heap_ctx_t* ctx = tiny_heap_ctx_for_thread();
if (class_idx == 7) {
tiny_c7_free_fast_with_meta(ss, slab_idx, base);
} else {
tiny_heap_free_class_fast_with_meta(ctx, class_idx, ss, slab_idx, base);
}
return 1;
}
}
}
if (use_tiny_heap) {
// fallback: lookup failed but TinyHeap front is ON → use generic TinyHeap free
tiny_heap_free_class_fast(tiny_heap_ctx_for_thread(), class_idx, ptr);
return 1;
}
}
}
@ -241,14 +266,6 @@ static inline int free_tiny_fast(void* ptr) {
}
#endif
// Optional: C7 専用ホットパス(キャッシュのみで完了させる)
if (__builtin_expect(class_idx == 7 && tiny_c7_hot_enabled(), 0)) {
if (tiny_c7_free_hot(base)) {
return 1;
}
// fallthrough to unified cache push on failure
}
int pushed = unified_cache_push(class_idx, HAK_BASE_FROM_RAW(base));
if (__builtin_expect(pushed, 1)) {
return 1; // Success