From a2e65716b3d46511b2d209b3245603511aee3d2a Mon Sep 17 00:00:00 2001 From: "Moe Charm (CI)" Date: Wed, 26 Nov 2025 15:05:03 +0900 Subject: [PATCH] Port: Optimize tiny_get_max_size inline (e81fe783d) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move tiny_get_max_size to header for inlining - Use cached static variable to avoid repeated env lookup - Larson: 51.99M ops/s (stable) 🤖 Generated with Claude Code Co-Authored-By: Claude --- core/hakmem_tiny.h | 21 +++++++++++++++++++- core/hakmem_tiny_config_box.inc | 35 ++------------------------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/core/hakmem_tiny.h b/core/hakmem_tiny.h index fb79718f..c25a77d9 100644 --- a/core/hakmem_tiny.h +++ b/core/hakmem_tiny.h @@ -33,7 +33,26 @@ int hak_is_initializing(void); // HAKMEM_TINY_MAX_CLASS=5 → Tiny handles up to 255B (C0-C5) // HAKMEM_TINY_MAX_CLASS=7 → Tiny handles up to 1023B (C0-C7, default) // Forward declaration (implementation in hakmem_tiny.c) -size_t tiny_get_max_size(void); +// Optimized: Inline for hot path (0.95% overhead removal) +#include +#include +extern bool smallmid_is_enabled(void); + +static inline size_t tiny_get_max_size(void) { + static size_t g_cached = 0; + if (__builtin_expect(g_cached == 0, 0)) { + const char* env = getenv("HAKMEM_TINY_MAX_CLASS"); + int max_class = 7; + if (env && *env) { + int parsed = atoi(env); + if (parsed >= 0 && parsed < TINY_NUM_CLASSES) max_class = parsed; + } + if (smallmid_is_enabled() && max_class > 5) max_class = 5; + static const size_t sizes[8] = {7, 15, 31, 63, 127, 255, 511, 2047}; + g_cached = sizes[max_class]; + } + return g_cached; +} // ============================================================================ // Phase 3d-B: TLS Cache Merge - Unified TLS SLL Structure diff --git a/core/hakmem_tiny_config_box.inc b/core/hakmem_tiny_config_box.inc index 65e8b130..7cfed33c 100644 --- a/core/hakmem_tiny_config_box.inc +++ b/core/hakmem_tiny_config_box.inc @@ -27,39 +27,8 @@ const size_t g_tiny_class_sizes[TINY_NUM_CLASSES] = { // Forward declaration for Small-Mid check extern bool smallmid_is_enabled(void); -// Get dynamic max size for Tiny allocator based on ENV configuration -// Default: 1023B (C0-C7), can be reduced to 255B (C0-C5) -// Phase 17-1: Auto-reduces to 255B when Small-Mid is enabled -size_t tiny_get_max_size(void) { - static int g_max_class = -1; - if (__builtin_expect(g_max_class == -1, 0)) { - const char* env = getenv("HAKMEM_TINY_MAX_CLASS"); - if (env && *env) { - int max_class = atoi(env); - if (max_class >= 0 && max_class < TINY_NUM_CLASSES) { - g_max_class = max_class; - } else { - g_max_class = 7; // Default: all classes (C0-C7) - } - } else { - g_max_class = 7; // Default: all classes - } - } - - // Phase 17-1: Auto-adjust when Small-Mid enabled - // Small-Mid handles 256B-1KB, so Tiny should only handle 0-255B - int effective_class = g_max_class; - if (smallmid_is_enabled() && effective_class > 5) { - effective_class = 5; // Limit to C0-C5 (0-255B) - } - - // Map class to max usable size (stride - 1) - // C0=8B, C1=16B, C2=32B, C3=64B, C4=128B, C5=256B, C6=512B, C7=2048B - static const size_t class_to_max_size[TINY_NUM_CLASSES] = { - 7, 15, 31, 63, 127, 255, 511, 2047 - }; - return class_to_max_size[effective_class]; -} +// Optimized: Cached max size for hot path +// Moved to hakmem_tiny.h for global inlining // ============================================================================ // PRIORITY 1-4: Integrity Check Counters