From f4746d845c5046cb6bfbb875d9791782d8f88671 Mon Sep 17 00:00:00 2001 From: _Neo_ Date: Thu, 14 May 2026 15:48:46 +0300 Subject: [PATCH] Update KThread.cs --- .../HOS/Kernel/Threading/KThread.cs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs b/src/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs index aaa4ccd99..4f06752cc 100644 --- a/src/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs +++ b/src/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs @@ -689,13 +689,26 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading return context.Pstate & 0xFF0FFE20; } + private const long ContextCacheDurationTicks = 800_000; // ~0.8ms cache lifetime + + private ThreadContext _cachedContext; + private long _contextCacheTimestamp; + private bool _hasValidContextCache; + private ThreadContext GetCurrentContext() { + var now = DateTime.UtcNow.Ticks; + + // Cache hit + if (_hasValidContextCache && (now - _contextCacheTimestamp) < ContextCacheDurationTicks) + { + return _cachedContext; + } + const int MaxRegistersAArch32 = 15; const int MaxFpuRegistersAArch32 = 16; ThreadContext context = new(); - Span registersSpan = context.Registers.AsSpan(); Span fpuRegistersSpan = context.FpuRegisters.AsSpan(); @@ -705,12 +718,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading { registersSpan[i] = Context.GetX(i); } - for (int i = 0; i < fpuRegistersSpan.Length; i++) { fpuRegistersSpan[i] = Context.GetV(i); } - context.Fp = Context.GetX(29); context.Lr = Context.GetX(30); context.Sp = Context.GetX(31); @@ -724,12 +735,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading { registersSpan[i] = (uint)Context.GetX(i); } - for (int i = 0; i < MaxFpuRegistersAArch32; i++) { fpuRegistersSpan[i] = Context.GetV(i); } - context.Pc = (uint)Context.Pc; context.Pstate = GetPsr(Context); context.Tpidr = (uint)Context.TpidrroEl0; @@ -738,6 +747,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading context.Fpcr = (uint)Context.Fpcr; context.Fpsr = (uint)Context.Fpsr; + // Update cache + _cachedContext = context; + _contextCacheTimestamp = now; + _hasValidContextCache = true; + return context; }