Refactor: Extract 4 safe Box modules from hakmem_tiny.c (-73% total reduction)

Conservative refactoring with Task-sensei's safety analysis.

## Changes

**hakmem_tiny.c**: 616 → 562 lines (-54 lines, -9% this phase)
**Total reduction**: 2081 → 562 lines (-1519 lines, -73% cumulative) 🏆

## Extracted Modules (4 new LOW-risk boxes)

9. **ss_active_box** (6 lines)
   - ss_active_add() - atomic add to active counter
   - ss_active_inc() - atomic increment active counter
   - Pure utility functions, no dependencies
   - Risk: LOW

10. **eventq_box** (32 lines)
   - hak_thread_id16() - thread ID compression
   - eventq_push_ex() - event queue push with sampling
   - Intelligence/telemetry helpers
   - Risk: LOW

11. **sll_cap_box** (12 lines)
   - sll_cap_for_class() - SLL capacity policy
   - Hot classes get multiplier × mag_cap
   - Cold classes get mag_cap / 2
   - Risk: LOW

12. **ultra_batch_box** (20 lines)
   - g_ultra_batch_override[] - batch size overrides
   - g_ultra_sll_cap_override[] - SLL capacity overrides
   - ultra_batch_for_class() - batch size policy
   - Risk: LOW

## Cumulative Progress (12 boxes total)

**Phase 1** (5 boxes): 2081 → 995 lines (-52%)
**Phase 2** (3 boxes): 995 → 616 lines (-38%)
**Phase 3** (4 boxes): 616 → 562 lines (-9%)

**All 12 boxes**:
1. config_box (211 lines)
2. publish_box (419 lines)
3. globals_box (256 lines)
4. phase6_wrappers_box (122 lines)
5. ace_guard_box (100 lines)
6. tls_state_box (224 lines)
7. legacy_slow_box (96 lines)
8. slab_lookup_box (77 lines)
9. ss_active_box (6 lines) 
10. eventq_box (32 lines) 
11. sll_cap_box (12 lines) 
12. ultra_batch_box (20 lines) 

**Total extracted**: 1,575 lines across 12 coherent modules
**Remaining core**: 562 lines (highly focused)

## Safety Approach

- Task-sensei performed deep dependency analysis
- Extracted only LOW-risk candidates
- All dependencies verified at compile time
- Forward declarations already present
- No circular dependencies
- Build tested after each extraction 

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-11-21 03:20:42 +09:00
parent 922eaac79c
commit 2878459132
5 changed files with 82 additions and 66 deletions

View File

@ -80,12 +80,10 @@ static void tiny_remote_drain_locked(struct TinySlab* slab);
__attribute__((always_inline)) __attribute__((always_inline))
static inline void* hak_tiny_alloc_wrapper(int class_idx); static inline void* hak_tiny_alloc_wrapper(int class_idx);
// Helpers for SuperSlab active block accounting (atomic, saturating dec) // Helpers for SuperSlab active block accounting (atomic, saturating dec)
void ss_active_add(SuperSlab* ss, uint32_t n) {
atomic_fetch_add_explicit(&ss->total_active_blocks, n, memory_order_relaxed); // SuperSlab Active Counter Helpers - EXTRACTED to hakmem_tiny_ss_active_box.inc
} #include "hakmem_tiny_ss_active_box.inc"
static inline __attribute__((always_inline)) void ss_active_inc(SuperSlab* ss) {
atomic_fetch_add_explicit(&ss->total_active_blocks, 1u, memory_order_relaxed);
}
// EXTRACTED: ss_active_dec_one() moved to hakmem_tiny_superslab.h (Phase 2C-2) // EXTRACTED: ss_active_dec_one() moved to hakmem_tiny_superslab.h (Phase 2C-2)
// Front refill count global config (declare before init.inc uses them) // Front refill count global config (declare before init.inc uses them)
@ -240,38 +238,10 @@ _Atomic uint32_t g_frontend_fill_target[TINY_NUM_CLASSES];
static inline int ultra_batch_for_class(int class_idx); static inline int ultra_batch_for_class(int class_idx);
enum { HAK_TIER_SLL=1, HAK_TIER_MAG=2, HAK_TIER_SLAB=3, HAK_TIER_SUPER=4, HAK_TIER_FRONT=5 }; enum { HAK_TIER_SLL=1, HAK_TIER_MAG=2, HAK_TIER_SLAB=3, HAK_TIER_SUPER=4, HAK_TIER_FRONT=5 };
static inline uint16_t hak_thread_id16(void) {
// best-effort compress cached thread id to 16 bits
uint32_t tid = tiny_self_u32();
return (uint16_t)(tid ^ (tid >> 16));
}
static inline void eventq_push_ex(int class_idx, uint32_t size, uint8_t tier, uint8_t flags, // Event Queue & Telemetry Helpers - EXTRACTED to hakmem_tiny_eventq_box.inc
uint32_t site_id, uint16_t lat_bucket) { #include "hakmem_tiny_eventq_box.inc"
(void)flags;
(void)lat_bucket;
(void)site_id;
if (!g_int_engine) return;
// Lightweight sampling: if mask set, log 1 out of 2^N
unsigned m = g_int_sample_mask;
if (m != 0) {
unsigned x = g_tls_ev_seq++;
if ((x & m) != 0) return;
}
uint32_t t = atomic_fetch_add_explicit(&g_ev_tail, 1u, memory_order_relaxed);
AllocEvent ev;
ev.ts_ns = g_int_event_ts ? hak_now_ns() : 0;
ev.size = size;
ev.site_id = 0; // keep minimal
ev.latency_bucket = 0;
ev.tier_hit = tier;
ev.flags = 0;
ev.class_idx = (uint16_t)class_idx;
ev.thread_id = 0;
g_ev_ring[t & EVENTQ_MASK] = ev; // best-effort overwrite on overflow
}
// Background refill workers and intelligence engine // Background refill workers and intelligence engine
#include "hakmem_tiny_background.inc" #include "hakmem_tiny_background.inc"
@ -344,18 +314,10 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
// SLL capacity policy: for hot tiny classes (0..3), allow larger SLL up to multiplier * mag_cap // SLL capacity policy: for hot tiny classes (0..3), allow larger SLL up to multiplier * mag_cap
// for >=4 keep current conservative half (to limit footprint). // for >=4 keep current conservative half (to limit footprint).
static inline uint32_t sll_cap_for_class(int class_idx, uint32_t mag_cap) {
// Phase12: g_sll_cap_override は非推奨。ここでは無視して通常capを返す。 // SLL Capacity Policy - EXTRACTED to hakmem_tiny_sll_cap_box.inc
uint32_t cap = mag_cap; #include "hakmem_tiny_sll_cap_box.inc"
if (class_idx <= 3) {
uint32_t mult = (g_sll_multiplier > 0 ? (uint32_t)g_sll_multiplier : 1u);
uint64_t want = (uint64_t)cap * (uint64_t)mult;
if (want > (uint64_t)TINY_TLS_MAG_CAP) cap = TINY_TLS_MAG_CAP; else cap = (uint32_t)want;
} else if (class_idx >= 4) {
cap = (mag_cap > 1u ? (mag_cap / 2u) : 1u);
}
return cap;
}
// ============================================================================ // ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1) // EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
@ -366,25 +328,9 @@ static inline uint32_t sll_cap_for_class(int class_idx, uint32_t mag_cap) {
static inline int ultra_sll_cap_for_class(int class_idx); static inline int ultra_sll_cap_for_class(int class_idx);
static inline int ultra_validate_sll_head(int class_idx, void* head); static inline int ultra_validate_sll_head(int class_idx, void* head);
// Ultra-mode (SLL-only) helpers // Ultra-Mode Batch Configuration - EXTRACTED to hakmem_tiny_ultra_batch_box.inc
// Ultra batch overrides via env: HAKMEM_TINY_ULTRA_BATCH_C{0..7} #include "hakmem_tiny_ultra_batch_box.inc"
static int g_ultra_batch_override[TINY_NUM_CLASSES] = {0};
static int g_ultra_sll_cap_override[TINY_NUM_CLASSES] = {0};
static inline int ultra_batch_for_class(int class_idx) {
int ov = g_ultra_batch_override[class_idx];
if (ov > 0) return ov;
switch (class_idx) {
case 0: return 64; // 8B
case 1: return 96; // 16BA/B最良
case 2: return 96; // 32BA/B最良
case 3: return 224; // 64BA/B最良
case 4: return 96; // 128B (promote front refill a bit)
case 5: return 64; // 256B (promote front refill)
case 6: return 64; // 512B (promote front refill)
default: return 32; // 1024B and others
}
}
// ============================================================================ // ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1) // EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)

View File

@ -0,0 +1,32 @@
static inline uint16_t hak_thread_id16(void) {
// best-effort compress cached thread id to 16 bits
uint32_t tid = tiny_self_u32();
return (uint16_t)(tid ^ (tid >> 16));
}
static inline void eventq_push_ex(int class_idx, uint32_t size, uint8_t tier, uint8_t flags,
uint32_t site_id, uint16_t lat_bucket) {
(void)flags;
(void)lat_bucket;
(void)site_id;
if (!g_int_engine) return;
// Lightweight sampling: if mask set, log 1 out of 2^N
unsigned m = g_int_sample_mask;
if (m != 0) {
unsigned x = g_tls_ev_seq++;
if ((x & m) != 0) return;
}
uint32_t t = atomic_fetch_add_explicit(&g_ev_tail, 1u, memory_order_relaxed);
AllocEvent ev;
ev.ts_ns = g_int_event_ts ? hak_now_ns() : 0;
ev.size = size;
ev.site_id = 0; // keep minimal
ev.latency_bucket = 0;
ev.tier_hit = tier;
ev.flags = 0;
ev.class_idx = (uint16_t)class_idx;
ev.thread_id = 0;
g_ev_ring[t & EVENTQ_MASK] = ev; // best-effort overwrite on overflow
}

View File

@ -0,0 +1,12 @@
static inline uint32_t sll_cap_for_class(int class_idx, uint32_t mag_cap) {
// Phase12: g_sll_cap_override は非推奨。ここでは無視して通常capを返す。
uint32_t cap = mag_cap;
if (class_idx <= 3) {
uint32_t mult = (g_sll_multiplier > 0 ? (uint32_t)g_sll_multiplier : 1u);
uint64_t want = (uint64_t)cap * (uint64_t)mult;
if (want > (uint64_t)TINY_TLS_MAG_CAP) cap = TINY_TLS_MAG_CAP; else cap = (uint32_t)want;
} else if (class_idx >= 4) {
cap = (mag_cap > 1u ? (mag_cap / 2u) : 1u);
}
return cap;
}

View File

@ -0,0 +1,6 @@
void ss_active_add(SuperSlab* ss, uint32_t n) {
atomic_fetch_add_explicit(&ss->total_active_blocks, n, memory_order_relaxed);
}
static inline __attribute__((always_inline)) void ss_active_inc(SuperSlab* ss) {
atomic_fetch_add_explicit(&ss->total_active_blocks, 1u, memory_order_relaxed);
}

View File

@ -0,0 +1,20 @@
// Ultra-mode (SLL-only) helpers
// Ultra batch overrides via env: HAKMEM_TINY_ULTRA_BATCH_C{0..7}
static int g_ultra_batch_override[TINY_NUM_CLASSES] = {0};
static int g_ultra_sll_cap_override[TINY_NUM_CLASSES] = {0};
static inline int ultra_batch_for_class(int class_idx) {
int ov = g_ultra_batch_override[class_idx];
if (ov > 0) return ov;
switch (class_idx) {
case 0: return 64; // 8B
case 1: return 96; // 16BA/B最良
case 2: return 96; // 32BA/B最良
case 3: return 224; // 64BA/B最良
case 4: return 96; // 128B (promote front refill a bit)
case 5: return 64; // 256B (promote front refill)
case 6: return 64; // 512B (promote front refill)
default: return 32; // 1024B and others
}
}