// wrapper_env_cache_box.h - TLS cache layer for wrapper env config (Phase 3 D2) // Purpose: Eliminate repeated wrapper_env_cfg() calls in malloc/free hot path // Strategy: Cache the const pointer in TLS (safe, no refresh sync needed) #pragma once #include "wrapper_env_box.h" // wrapper_env_cfg() #include "wrapper_env_cache_env_box.h" // wrap_env_cache_enabled() // Fast pointer cache: TLS caches the pointer to g_wrapper_env // Safety: wrapper_env_refresh_from_env() doesn't change the pointer address, // only the content of g_wrapper_env, so TLS cache remains valid. static inline const wrapper_env_cfg_t* wrapper_env_cfg_fast(void) { if (__builtin_expect(!wrap_env_cache_enabled(), 1)) { // Cache disabled (default): use baseline wrapper_env_cfg() return wrapper_env_cfg(); } // Cache enabled: TLS fast pointer static __thread const wrapper_env_cfg_t* cached_ptr = NULL; if (__builtin_expect(cached_ptr == NULL, 0)) { // First access: initialize TLS cache with pointer to g_wrapper_env cached_ptr = wrapper_env_cfg(); } return cached_ptr; }