From fd8a78ad73be1829581bb0eb7657a71e416273c0 Mon Sep 17 00:00:00 2001 From: kuba Date: Wed, 30 Sep 2026 00:11:15 +0200 Subject: [PATCH] [Cpu] Read import stack arguments from the thread's own stack without a region query Every import copies six stack argument slots (twice, with the per-thread diagnostics). The per-thread readable-range cache is keyed by the host mapping generation, which every tracker protection change bumps, so on macOS nearly each slot fell through to a locked host region query: about half of the busy managed samples at Octopath Traveler II's title were in that query or waiting for its lock. Addresses inside the calling guest thread's stack, which the backend maps read-write for the thread's lifetime, are now read directly. --- .../Cpu/Native/DirectExecutionBackend.Imports.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/SharpEmu.Core/Cpu/Native/DirectExecutionBackend.Imports.cs b/src/SharpEmu.Core/Cpu/Native/DirectExecutionBackend.Imports.cs index dc4ef3a..74184ba 100644 --- a/src/SharpEmu.Core/Cpu/Native/DirectExecutionBackend.Imports.cs +++ b/src/SharpEmu.Core/Cpu/Native/DirectExecutionBackend.Imports.cs @@ -1231,6 +1231,17 @@ public sealed partial class DirectExecutionBackend return TryReadHostQword(address, out value); } + // Stack arguments sit on the calling guest thread's own stack, which the backend mapped read-write + // for the thread's lifetime and no tracker ever protects. Reading inside it needs no query: the + // cached range below is keyed by a mapping generation that every tracker protection change bumps, + // so falling through here cost a locked host region query per argument on nearly every import. + if (_activeGuestThreadState is { StackSize: > 0 } thread && + address >= thread.StackBase && address <= thread.StackBase + thread.StackSize - sizeof(ulong)) + { + value = *(ulong*)address; + return true; + } + var generation = HostMemory.MappingGeneration; if (generation == _importReadableGeneration && address >= _importReadableStart && address <= _importReadableEnd - 8)