Check the menu pointer's game pointers against the guest layout instead of asking the OS

The map detection reads every open screen of eui::ScreenMgr every 100 ms, and each pointer it follows was checked
with plat_mem_readable, a VirtualQuery. That query costs tens of microseconds in this process, so each scan stalled
the game thread for milliseconds: on a Ryzen 5 4500 the mean frame rate fell from 59 to 50-54 fps and the 1% low from
44 to 23 fps. mem_guest_readable answers from the layout for the committed heap and the loaded modules, where the
game's objects and globals live, and asks the OS only for anything else. With it the same scene runs at 58-60 fps,
1% low 45-48 fps, as without the map detection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tempus3214andClaude Opus 5.5 committed 2026-11-05 00:21:45 -03:00
1 parent 366150cba4
commit 5b4b300a1e
3 files changed
+34 -2

No files matched your search

+3
View File
@@ -167,6 +167,9 @@ void mem_thaw(uint64_t a, uint64_t n); // makes the frozen r
void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws);
// regions frozen now; freezes and thaws since the last call
void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit
int mem_guest_readable(uint64_t a, uint64_t n); // [a, a+n) can be read without a fault: inside a loaded
// module or the committed heap answered from the layout,
// anything else by plat_mem_readable (an OS query)
// ---- modules (loader.c)
typedef struct {
+29
View File
@@ -288,6 +288,35 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) {
return R_OK;
}
// The runtime's readers of game objects (menu_cursor.c, mouse_direct.c) check every pointer before they follow it. An
// OS query (plat_mem_readable: VirtualQuery) costs tens of microseconds in this process, and a reader that checks a
// few dozen pointers at a time stalls the game thread for milliseconds. The game's objects live in the committed heap
// and its globals in the modules' images, which are readable for as long as they are mapped: those answer from the
// layout. Any other range asks the OS. The heap only shrinks when the game asks for it (mem_set_heap_size), not while
// it runs.
int mem_guest_readable(uint64_t a, uint64_t n) {
static uint64_t fast, slow;
if (a + n < a) return 0;
uint64_t hs = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE);
int ok = a >= L.heap.base && a + n <= L.heap.base + hs;
for (int i = 0; !ok && i < ldr_count(); i++) {
const RtLoaded *m = ldr_module(i);
ok = a >= m->base && a + n <= m->base + m->size;
}
if (ok) {
if (__atomic_add_fetch(&fast, 1, __ATOMIC_RELAXED) == 100000)
rt_log("memory: 100000 guest reads checked by the layout, %llu by the OS",
(unsigned long long)__atomic_load_n(&slow, __ATOMIC_RELAXED));
} else {
ok = plat_mem_readable(a, n);
uint64_t s = __atomic_add_fetch(&slow, 1, __ATOMIC_RELAXED);
if (!(s & (s - 1)) && s >= 1024) // 1024, 2048, 4096...: how often the layout could not answer
rt_log("memory: guest reads checked by the layout %llu, by the OS %llu",
(unsigned long long)__atomic_load_n(&fast, __ATOMIC_RELAXED), (unsigned long long)s);
}
return ok;
}
// ---- module data permissions (0x02, 0x03) and module pages for QueryMemory
static int module_index(uint64_t a) {
for (int i = 0; i < ldr_count(); i++) {
+2 -2
View File
@@ -53,7 +53,7 @@ enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59
static uint64_t main_lo, main_hi;
static int ok_at(uint64_t p, uint64_t n) {
return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && plat_mem_readable(p, n);
return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && mem_guest_readable(p, n);
}
static uint64_t rd64(uint64_t a) { uint64_t v; memcpy(&v, (const void *)(uintptr_t)a, 8); return v; }
static uint32_t rd32(uint64_t a) { uint32_t v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; }
@@ -157,7 +157,7 @@ static Focus focus_now(void) {
static void layout_name(uint64_t scr, char *out, size_t n) {
snprintf(out, n, "?");
uint64_t lyt = rd64(scr + SCR_LAYOUT), name = ok_at(lyt, LYT_NAME + 8) ? rd64(lyt + LYT_NAME) : 0;
if (name < 0x10000 || name >= (1ull << 47) || !plat_mem_readable(name, n)) return;
if (name < 0x10000 || name >= (1ull << 47) || !mem_guest_readable(name, n)) return;
memcpy(out, (const void *)(uintptr_t)name, n - 1);
out[n - 1] = 0;
for (char *q = out; *q; q++) if (*q < 0x20 || *q > 0x7e) { *q = 0; break; }