Phase 8: FREE-STATIC-ROUTE ENV Cache Hardening (GO +2.61%)

Results:
- A/B test: +2.61% on Mixed (10-run, clean env)
- Baseline: 49.26M ops/s
- Optimized: 50.55M ops/s
- Improvement: +1.29M ops/s (+2.61%)

Strategy:
- Fix ENV cache accident (main前キャッシュ事故の修正)
- Add refresh mechanism to sync with bench_profile putenv
- Ensure Phase 3 D1 optimization works reliably

Success factors:
1. Performance improvement: +2.61% (existing win-box now reliable)
2. ENV cache accident fixed: refresh mechanism works correctly
3. Standard deviation improved: 867K → 336K ops/s (61% reduction)
4. Baseline quality improved: existing optimization now guaranteed

Implementation:
- Patch 1: Make ENV gate refreshable (tiny_free_route_cache_env_box.{h,c})
  - Changed static int to extern _Atomic int
  - Added tiny_free_static_route_refresh_from_env()
- Patch 2: Integrate refresh into bench_profile.h
  - Call refresh after bench_setenv_default() group
- Patch 3: Update Makefile for new .c file

ENV cache fix verification:
- [FREE_STATIC_ROUTE] enabled appears twice (refresh working)
- bench_profile putenv now reliably reflected

Files modified:
- core/box/tiny_free_route_cache_env_box.h: extern + refresh API
- core/box/tiny_free_route_cache_env_box.c: NEW (global state + refresh)
- core/bench_profile.h: add refresh call
- Makefile: add new .o file

Health check: PASSED (all profiles)

Rollback: HAKMEM_FREE_STATIC_ROUTE=0 or revert Patch 1/2

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-14 18:49:08 +09:00
parent 17aaa90981
commit be723ca052
5 changed files with 222 additions and 6 deletions

View File

@ -0,0 +1,20 @@
// tiny_free_route_cache_env_box.c - Phase 8: FREE-STATIC-ROUTE ENV cache hardening
// 役割:
// - Global state definition for g_free_static_route_enabled
// - Refresh function to reset ENV cache (called by bench_profile)
//
#include <stdatomic.h>
#include <stdlib.h>
#include <stdio.h>
// Global state for free static route ENV gate
// -1 = uninitialized, 0 = disabled, 1 = enabled
_Atomic int g_free_static_route_enabled = -1;
// tiny_free_static_route_refresh_from_env() - Refresh ENV cache from environment
// Called by bench_profile after putenv() to ensure new values are reflected.
void tiny_free_static_route_refresh_from_env(void) {
// Reset to uninitialized state, forcing next call to tiny_free_static_route_enabled()
// to read from environment again.
atomic_store_explicit(&g_free_static_route_enabled, -1, memory_order_relaxed);
}

View File

@ -3,27 +3,39 @@
// - ENV gate for HAKMEM_FREE_STATIC_ROUTE (default OFF)
// - Enable cached route lookup in free path to eliminate tiny_route_for_class() overhead
//
// Phase 8: ENV cache hardening (refresh support)
// - Global state is now defined in .c file (not static in header)
// - bench_profile can call tiny_free_static_route_refresh_from_env() to reset cache
//
// Target: 4.39% self + 24.78% children in tiny_route_for_class() during free
// Expected gain: +1-2% in Mixed workload
//
#pragma once
#include <stdatomic.h>
#include <stdlib.h>
#include <stdio.h>
static int g_free_static_route_enabled = -1; // -1 = uninitialized
// Global state for free static route ENV gate (defined in .c file)
// -1 = uninitialized, 0 = disabled, 1 = enabled
extern _Atomic int g_free_static_route_enabled;
// tiny_free_static_route_refresh_from_env() - Refresh ENV cache from environment
// Called by bench_profile after putenv() to ensure new values are reflected.
void tiny_free_static_route_refresh_from_env(void);
// tiny_free_static_route_enabled() - Check if free path route cache is enabled
// Returns: 1 if enabled, 0 if disabled (default)
// ENV: HAKMEM_FREE_STATIC_ROUTE=0/1
static inline int tiny_free_static_route_enabled(void) {
if (__builtin_expect(g_free_static_route_enabled >= 0, 1)) {
return g_free_static_route_enabled;
int current = atomic_load_explicit(&g_free_static_route_enabled, memory_order_relaxed);
if (__builtin_expect(current >= 0, 1)) {
return current;
}
const char* e = getenv("HAKMEM_FREE_STATIC_ROUTE");
int enabled = (e && *e && *e != '0') ? 1 : 0;
g_free_static_route_enabled = enabled;
atomic_store_explicit(&g_free_static_route_enabled, enabled, memory_order_relaxed);
if (enabled) {
fprintf(stderr, "[FREE_STATIC_ROUTE] enabled (Phase 3 D1)\n");