## Summary - ChatGPT により bench_profile.h の setenv segfault を修正(RTLD_NEXT 経由に切り替え) - core/box/pool_zero_mode_box.h 新設:ENV キャッシュ経由で ZERO_MODE を統一管理 - core/hakmem_pool.c で zero mode に応じた memset 制御(FULL/header/off) - A/B テスト結果:ZERO_MODE=header で +15.34% improvement(1M iterations, C6-heavy) ## Files Modified - core/box/pool_api.inc.h: pool_zero_mode_box.h include - core/bench_profile.h: glibc setenv → malloc+putenv(segfault 回避) - core/hakmem_pool.c: zero mode 参照・制御ロジック - core/box/pool_zero_mode_box.h (新設): enum/getter - CURRENT_TASK.md: Phase ML1 結果記載 ## Test Results | Iterations | ZERO_MODE=full | ZERO_MODE=header | Improvement | |-----------|----------------|-----------------|------------| | 10K | 3.06 M ops/s | 3.17 M ops/s | +3.65% | | 1M | 23.71 M ops/s | 27.34 M ops/s | **+15.34%** | 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
60 lines
1.8 KiB
C
60 lines
1.8 KiB
C
// mid_tcache.h - Mid-size TLS tcache (classes 4..7)
|
|
// Box: Per-thread, per-class singly-linked stack. No atomics, no sharing.
|
|
// Goal: O(1) pop/push on alloc/free for 128..1024B while keeping Box boundaries.
|
|
|
|
#pragma once
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
#ifndef TINY_NUM_CLASSES
|
|
#define TINY_NUM_CLASSES 8
|
|
#endif
|
|
|
|
static inline int midtc_enabled(void) {
|
|
static int en = -1;
|
|
if (__builtin_expect(en == -1, 0)) {
|
|
const char* s = getenv("HAKMEM_MID_TC");
|
|
en = (s && *s && *s != '0') ? 1 : 0;
|
|
}
|
|
return en;
|
|
}
|
|
|
|
static inline int midtc_cap_global(void) {
|
|
static int cap = -1;
|
|
if (__builtin_expect(cap == -1, 0)) {
|
|
const char* s = getenv("HAKMEM_MID_TC_CAP");
|
|
int v = (s && *s) ? atoi(s) : 32; // conservative default
|
|
if (v < 0) v = 0;
|
|
if (v > 1024) v = 1024;
|
|
cap = v;
|
|
}
|
|
return cap;
|
|
}
|
|
|
|
// Per-thread state
|
|
static __thread void* g_midtc_head[TINY_NUM_CLASSES];
|
|
static __thread uint16_t g_midtc_count[TINY_NUM_CLASSES];
|
|
|
|
// Push returns 1 on accept, 0 to fallback to existing path.
|
|
static inline int midtc_push(int class_idx, void* p) {
|
|
if (!midtc_enabled()) return 0;
|
|
if (class_idx < 4 || class_idx >= TINY_NUM_CLASSES) return 0; // only 128..1024B
|
|
uint16_t cnt = g_midtc_count[class_idx];
|
|
int cap = midtc_cap_global();
|
|
if (cnt >= (uint16_t)cap) return 0;
|
|
*(void**)p = g_midtc_head[class_idx];
|
|
g_midtc_head[class_idx] = p;
|
|
g_midtc_count[class_idx] = (uint16_t)(cnt + 1);
|
|
return 1;
|
|
}
|
|
|
|
static inline void* midtc_pop(int class_idx) {
|
|
if (!midtc_enabled()) return NULL;
|
|
if (class_idx < 4 || class_idx >= TINY_NUM_CLASSES) return NULL;
|
|
void* h = g_midtc_head[class_idx];
|
|
if (!h) return NULL;
|
|
g_midtc_head[class_idx] = *(void**)h;
|
|
if (g_midtc_count[class_idx] > 0) g_midtc_count[class_idx]--;
|
|
return h;
|
|
}
|