diff --git a/core/box/pool_mid_inuse_deferred_box.h b/core/box/pool_mid_inuse_deferred_box.h index d3dc4d78..8512f058 100644 --- a/core/box/pool_mid_inuse_deferred_box.h +++ b/core/box/pool_mid_inuse_deferred_box.h @@ -85,10 +85,20 @@ static inline void mid_inuse_dec_deferred(void* raw) { // Search TLS map for existing page entry MidInuseTlsPageMap* map = &g_mid_inuse_tls_map; + + // Check last match first (60-80% hit rate expected - temporal locality) + if (map->last_idx < map->used && map->pages[map->last_idx] == page) { + map->counts[map->last_idx]++; + MID_INUSE_DEFERRED_STAT_INC(mid_inuse_deferred_hit); + return; + } + + // Fallback to linear search (now rarely executed) for (uint32_t i = 0; i < map->used; i++) { if (map->pages[i] == page) { // Page already in map, increment count map->counts[i]++; + map->last_idx = i; // Update last_idx on hit MID_INUSE_DEFERRED_STAT_INC(mid_inuse_deferred_hit); return; } @@ -104,6 +114,7 @@ static inline void mid_inuse_dec_deferred(void* raw) { uint32_t idx = map->used++; map->pages[idx] = page; map->counts[idx] = 1; + map->last_idx = idx; // Set last_idx to new entry (temporal locality) MID_INUSE_DEFERRED_STAT_INC(mid_inuse_deferred_hit); } @@ -152,6 +163,7 @@ static inline void mid_inuse_deferred_drain(void) { // Clear map (reset for next batch) map->used = 0; + map->last_idx = 0; // Reset last_idx on drain } #endif // POOL_MID_INUSE_DEFERRED_BOX_H diff --git a/core/box/pool_mid_inuse_tls_pagemap_box.h b/core/box/pool_mid_inuse_tls_pagemap_box.h index 8b9edae2..900d50ae 100644 --- a/core/box/pool_mid_inuse_tls_pagemap_box.h +++ b/core/box/pool_mid_inuse_tls_pagemap_box.h @@ -23,6 +23,7 @@ typedef struct { void* pages[MID_INUSE_TLS_MAP_SIZE]; // Page base addresses uint32_t counts[MID_INUSE_TLS_MAP_SIZE]; // Pending dec count per page uint32_t used; // Number of active entries + uint32_t last_idx; // Cache last hit index for temporal locality } MidInuseTlsPageMap; // Thread-local instance (zero-initialized by default)