mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 21:05:56 +00:00
Check the camera's player-state pointers against the guest layout instead of asking the OS
player_state follows about fifteen pointers from the player root to its action verbs, and each was checked with plat_mem_readable, a VirtualQuery. That query costs tens of microseconds in this process, so the read stalled the game thread while the mouse moved. mem_guest_readable answers from the layout for the committed heap and the loaded modules, where the player and its components live, and asks the OS only for anything else. Together with the same change in the menu pointer, a Ryzen 5 4500 runs the scene that fell to 50-54 fps at 58-60 fps again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
27f4d40e80
commit
27f64d6a29
4 files changed
+34
-2
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
@@ -173,7 +173,7 @@ enum { VERB_USE_BOW = 59, VERB_USE_SPECIAL_POWER = 61, VERB_DRAWN_BOW = 205, VER
|
||||
enum { PLAYER_FOUND = 1, PLAYER_ABILITY = 2, PLAYER_BOW = 4 };
|
||||
|
||||
static int readable(uint64_t p, uint64_t n) {
|
||||
return p >= 0x10000 && p < (1ull << 47) && !(p & 7) && plat_mem_readable(p, n);
|
||||
return p >= 0x10000 && p < (1ull << 47) && !(p & 7) && mem_guest_readable(p, n);
|
||||
}
|
||||
static int32_t guest_s32(uint64_t at) { int32_t v; memcpy(&v, (void *)(uintptr_t)at, sizeof v); return v; }
|
||||
static int game_object(uint64_t p, uint64_t n) { // readable, its vtable in main's image
|
||||
|
||||
Reference in new issue
Block a user