From c08479877ecebfdea808f067368b29d082807f08 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 19 Sep 2018 12:16:20 -0300 Subject: [PATCH] Fix performance regression caused by the new scheduler changes (#422) * Call interrupt less often, remove some leftovers from the old scheduler code * Remove unneeded attribute --- AThread.cs | 2 -- State/AThreadState.cs | 30 +++++++++++++++++++++++------- Translation/AILEmitterCtx.cs | 2 ++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/AThread.cs b/AThread.cs index 76b36da..38c2b53 100644 --- a/AThread.cs +++ b/AThread.cs @@ -46,8 +46,6 @@ namespace ChocolArm64 return false; } - Work.Name = "cpu_thread_" + Work.ManagedThreadId; - Work.Start(); return true; diff --git a/State/AThreadState.cs b/State/AThreadState.cs index 783f5a1..22e3df1 100644 --- a/State/AThreadState.cs +++ b/State/AThreadState.cs @@ -2,6 +2,7 @@ using ChocolArm64.Events; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; namespace ChocolArm64.State @@ -14,6 +15,8 @@ namespace ChocolArm64.State internal const int ErgSizeLog2 = 4; internal const int DczSizeLog2 = 4; + private const int MinInstForCheck = 4000000; + internal AExecutionMode ExecutionMode; //AArch32 state. @@ -45,6 +48,8 @@ namespace ChocolArm64.State private bool Interrupted; + private int SyncCount; + public long TpidrEl0 { get; set; } public long Tpidr { get; set; } @@ -101,13 +106,16 @@ namespace ChocolArm64.State TickCounter.Start(); } - internal bool Synchronize() + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal bool Synchronize(int BbWeight) { - if (Interrupted) - { - Interrupted = false; + //Firing a interrupt frequently is expensive, so we only + //do it after a given number of instructions has executed. + SyncCount += BbWeight; - OnInterrupt(); + if (SyncCount >= MinInstForCheck) + { + CheckInterrupt(); } return Running; @@ -118,9 +126,17 @@ namespace ChocolArm64.State Interrupted = true; } - private void OnInterrupt() + [MethodImpl(MethodImplOptions.NoInlining)] + private void CheckInterrupt() { - Interrupt?.Invoke(this, EventArgs.Empty); + SyncCount = 0; + + if (Interrupted) + { + Interrupted = false; + + Interrupt?.Invoke(this, EventArgs.Empty); + } } internal void OnBreak(long Position, int Imm) diff --git a/Translation/AILEmitterCtx.cs b/Translation/AILEmitterCtx.cs index 40e33ba..4870071 100644 --- a/Translation/AILEmitterCtx.cs +++ b/Translation/AILEmitterCtx.cs @@ -123,6 +123,8 @@ namespace ChocolArm64.Translation { EmitLdarg(ATranslatedSub.StateArgIdx); + EmitLdc_I4(CurrBlock.OpCodes.Count); + EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.Synchronize)); EmitLdc_I4(0);