2025-11-05 12:31:14 +09:00
|
|
|
// hakmem_bigcache.h - Big-Block Cache (Box理論)
|
|
|
|
|
// Purpose: Per-site ring cache for large allocations (>= 1MB)
|
|
|
|
|
//
|
|
|
|
|
// License: MIT
|
|
|
|
|
// Date: 2025-10-21
|
feat: Phase 7 + Phase 2 - Massive performance & stability improvements
Performance Achievements:
- Tiny allocations: +180-280% (21M → 59-70M ops/s random mixed)
- Single-thread: +24% (2.71M → 3.36M ops/s Larson)
- 4T stability: 0% → 95% (19/20 success rate)
- Overall: 91.3% of System malloc average (target was 40-55%) ✓
Phase 7 (Tasks 1-3): Core Optimizations
- Task 1: Header validation removal (Region-ID direct lookup)
- Task 2: Aggressive inline (TLS cache access optimization)
- Task 3: Pre-warm TLS cache (eliminate cold-start penalty)
Result: +180-280% improvement, 85-146% of System malloc
Critical Bug Fixes:
- Fix 64B allocation crash (size-to-class +1 for header)
- Fix 4T wrapper recursion bugs (BUG #7, #8, #10, #11)
- Remove malloc fallback (30% → 50% stability)
Phase 2a: SuperSlab Dynamic Expansion (CRITICAL)
- Implement mimalloc-style chunk linking
- Unlimited slab expansion (no more OOM at 32 slabs)
- Fix chunk initialization bug (bitmap=0x00000001 after expansion)
Files: core/hakmem_tiny_superslab.c/h, core/superslab/superslab_types.h
Result: 50% → 95% stability (19/20 4T success)
Phase 2b: TLS Cache Adaptive Sizing
- Dynamic capacity: 16-2048 slots based on usage
- High-water mark tracking + exponential growth/shrink
- Expected: +3-10% performance, -30-50% memory
Files: core/tiny_adaptive_sizing.c/h (new)
Phase 2c: BigCache Dynamic Hash Table
- Migrate from fixed 256×8 array to dynamic hash table
- Auto-resize: 256 → 512 → 1024 → 65,536 buckets
- Improved hash function (FNV-1a) + collision chaining
Files: core/hakmem_bigcache.c/h
Expected: +10-20% cache hit rate
Design Flaws Analysis:
- Identified 6 components with fixed-capacity bottlenecks
- SuperSlab (CRITICAL), TLS Cache (HIGH), BigCache/L2.5 (MEDIUM)
- Report: DESIGN_FLAWS_ANALYSIS.md (11 chapters)
Documentation:
- 13 comprehensive reports (PHASE*.md, DESIGN_FLAWS*.md)
- Implementation guides, test results, production readiness
- Bug fix reports, root cause analysis
Build System:
- Makefile: phase7 targets, PREWARM_TLS flag
- Auto dependency generation (-MMD -MP) for .inc files
Known Issues:
- 4T stability: 19/20 (95%) - investigating 1 failure for 100%
- L2.5 Pool dynamic sharding: design only (needs 2-3 days integration)
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 17:08:00 +09:00
|
|
|
// Phase 2c: Dynamic hash table implementation
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// BigCache Box - サイト別大規模ブロックキャッシュ
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
feat: Phase 7 + Phase 2 - Massive performance & stability improvements
Performance Achievements:
- Tiny allocations: +180-280% (21M → 59-70M ops/s random mixed)
- Single-thread: +24% (2.71M → 3.36M ops/s Larson)
- 4T stability: 0% → 95% (19/20 success rate)
- Overall: 91.3% of System malloc average (target was 40-55%) ✓
Phase 7 (Tasks 1-3): Core Optimizations
- Task 1: Header validation removal (Region-ID direct lookup)
- Task 2: Aggressive inline (TLS cache access optimization)
- Task 3: Pre-warm TLS cache (eliminate cold-start penalty)
Result: +180-280% improvement, 85-146% of System malloc
Critical Bug Fixes:
- Fix 64B allocation crash (size-to-class +1 for header)
- Fix 4T wrapper recursion bugs (BUG #7, #8, #10, #11)
- Remove malloc fallback (30% → 50% stability)
Phase 2a: SuperSlab Dynamic Expansion (CRITICAL)
- Implement mimalloc-style chunk linking
- Unlimited slab expansion (no more OOM at 32 slabs)
- Fix chunk initialization bug (bitmap=0x00000001 after expansion)
Files: core/hakmem_tiny_superslab.c/h, core/superslab/superslab_types.h
Result: 50% → 95% stability (19/20 4T success)
Phase 2b: TLS Cache Adaptive Sizing
- Dynamic capacity: 16-2048 slots based on usage
- High-water mark tracking + exponential growth/shrink
- Expected: +3-10% performance, -30-50% memory
Files: core/tiny_adaptive_sizing.c/h (new)
Phase 2c: BigCache Dynamic Hash Table
- Migrate from fixed 256×8 array to dynamic hash table
- Auto-resize: 256 → 512 → 1024 → 65,536 buckets
- Improved hash function (FNV-1a) + collision chaining
Files: core/hakmem_bigcache.c/h
Expected: +10-20% cache hit rate
Design Flaws Analysis:
- Identified 6 components with fixed-capacity bottlenecks
- SuperSlab (CRITICAL), TLS Cache (HIGH), BigCache/L2.5 (MEDIUM)
- Report: DESIGN_FLAWS_ANALYSIS.md (11 chapters)
Documentation:
- 13 comprehensive reports (PHASE*.md, DESIGN_FLAWS*.md)
- Implementation guides, test results, production readiness
- Bug fix reports, root cause analysis
Build System:
- Makefile: phase7 targets, PREWARM_TLS flag
- Auto dependency generation (-MMD -MP) for .inc files
Known Issues:
- 4T stability: 19/20 (95%) - investigating 1 failure for 100%
- L2.5 Pool dynamic sharding: design only (needs 2-3 days integration)
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 17:08:00 +09:00
|
|
|
// Phase 2c: Dynamic hash table configuration
|
|
|
|
|
#define BIGCACHE_INITIAL_CAPACITY 256 // Initial bucket count (power of 2)
|
|
|
|
|
#define BIGCACHE_MAX_CAPACITY 65536 // Max 64K buckets (power of 2)
|
|
|
|
|
#define BIGCACHE_LOAD_FACTOR 0.75f // Resize at 75% load
|
|
|
|
|
#define BIGCACHE_MIN_SIZE 524288 // 512KB minimum for caching
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
// Phase 6.11: Expanded size classes (4 → 8 classes, finer granularity to reduce internal fragmentation)
|
|
|
|
|
#define BIGCACHE_NUM_CLASSES 8 // Number of size classes (Phase 6.11: increased from 4)
|
|
|
|
|
#define BIGCACHE_CLASS_512KB 524288 // 512KB size class (NEW)
|
|
|
|
|
#define BIGCACHE_CLASS_1MB 1048576 // 1MB size class
|
|
|
|
|
#define BIGCACHE_CLASS_2MB 2097152 // 2MB size class
|
|
|
|
|
#define BIGCACHE_CLASS_3MB 3145728 // 3MB size class (NEW: reduces fragmentation)
|
|
|
|
|
#define BIGCACHE_CLASS_4MB 4194304 // 4MB size class
|
|
|
|
|
#define BIGCACHE_CLASS_6MB 6291456 // 6MB size class (NEW)
|
|
|
|
|
#define BIGCACHE_CLASS_8MB 8388608 // 8MB size class
|
|
|
|
|
#define BIGCACHE_CLASS_16MB 16777216 // 16MB size class (NEW)
|
|
|
|
|
|
|
|
|
|
// BigCache API (Box Interface)
|
|
|
|
|
|
|
|
|
|
// Try to get cached block for this site
|
|
|
|
|
// Returns: 1 if hit (out_ptr set), 0 if miss
|
|
|
|
|
int hak_bigcache_try_get(size_t size, uintptr_t site, void** out_ptr);
|
|
|
|
|
|
|
|
|
|
// Try to put freed block into cache
|
|
|
|
|
// Returns: 1 if cached, 0 if rejected (full/not cacheable)
|
|
|
|
|
int hak_bigcache_put(void* ptr, size_t class_bytes, uintptr_t site);
|
|
|
|
|
|
|
|
|
|
// Initialize BigCache (called from hak_init)
|
|
|
|
|
void hak_bigcache_init(void);
|
|
|
|
|
|
|
|
|
|
// Cleanup BigCache (called from hak_shutdown)
|
|
|
|
|
void hak_bigcache_shutdown(void);
|
|
|
|
|
|
|
|
|
|
// Stats (for debugging)
|
|
|
|
|
void hak_bigcache_print_stats(void);
|
|
|
|
|
|
|
|
|
|
// Set free callback (for proper cleanup on eviction)
|
|
|
|
|
typedef void (*hak_bigcache_free_fn_t)(void* ptr, size_t size);
|
|
|
|
|
void hak_bigcache_set_free_callback(hak_bigcache_free_fn_t fn);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|