From 04c6dc383c79461f031027b5ab3c615f4858e264 Mon Sep 17 00:00:00 2001 From: Matthew Stanley <1379tech@gmail.com> Date: Sat, 19 Sep 2026 20:01:14 -0700 Subject: [PATCH] Cache loaded overlay membership during lazy candidate discovery --- runtime/src/overlay_loader.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/runtime/src/overlay_loader.c b/runtime/src/overlay_loader.c index df7a2c33..d3e0da60 100644 --- a/runtime/src/overlay_loader.c +++ b/runtime/src/overlay_loader.c @@ -1207,6 +1207,8 @@ typedef struct { uint8_t load_failed; int resident; int capacity_suppressed; + int loaded_check_count; + int loaded_check_result; char path[768]; } CacheEntry; static CacheEntry s_cache_idx[CACHE_IDX_CAP]; @@ -1729,6 +1731,8 @@ static int add_posix_cache_file(const PsxOverlayCacheFile *file, void *opaque) { e->tier = (uint8_t)tier; e->manifest_ok = 0; e->load_failed = 0; + e->loaded_check_count = -1; + e->loaded_check_result = 0; e->resident = cache_path_is_bios_resident(file->path); e->capacity_suppressed = 0; snprintf(e->path, sizeof(e->path), "%s", file->path); @@ -1773,6 +1777,8 @@ static void scan_one_cache_dir(const char *dir, int tier) { e->tier = (uint8_t)tier; e->manifest_ok = 0; e->load_failed = 0; + e->loaded_check_count = -1; + e->loaded_check_result = 0; e->capacity_suppressed = 0; snprintf(e->path, sizeof(e->path), "%s", full); e->resident = cache_path_is_bios_resident(full); @@ -2999,6 +3005,20 @@ static int dll_already_loaded(const char *path) { return 0; } +/* A lazy manifest chain may have hundreds of functions from the same DLL. + * Re-comparing every long Windows path (including locale case folding) for + * each function dominates uncovered CPS continuation dispatches. Loaded paths + * are append-only: cache membership per artifact until that set grows. This + * caches only path membership; live code/manifest validation is unchanged. */ +static int cache_entry_already_loaded(int ci) { + CacheEntry *e = &s_cache_idx[ci]; + if (e->loaded_check_count != s_nloaded_paths) { + e->loaded_check_result = dll_already_loaded(e->path); + e->loaded_check_count = s_nloaded_paths; + } + return e->loaded_check_result; +} + static void overlay_library_close(OverlayLibraryHandle handle) { if (!handle) return; #ifdef _WIN32 @@ -3327,7 +3347,7 @@ static int lazy_is_loadable(int li, uint32_t region_start, uint32_t phys, s_cache_idx[ci].region_start == region_start) && !s_cache_idx[ci].load_failed && !s_cache_idx[ci].capacity_suppressed && - !dll_already_loaded(s_cache_idx[ci].path) && + !cache_entry_already_loaded(ci) && lazy_man_contains(&lm->fn, phys) && lazy_man_matches(lm); } @@ -3347,7 +3367,7 @@ static int lazy_load_selected(int li) { if (ci < 0 || ci >= s_cache_idx_count || ci >= CACHE_IDX_CAP || s_cache_idx[ci].load_failed || s_cache_idx[ci].capacity_suppressed || - dll_already_loaded(s_cache_idx[ci].path) || + cache_entry_already_loaded(ci) || s_cache_idx[ci].func_count <= 0) return 0; /* If proactive warming has not reached this fragment yet, prefer the * historical synchronous path over running a potentially hot function in