From 9d82d27df2d28c96f854b050c65e58eb1a48a22d Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Tue, 16 Feb 2021 19:16:20 -0300
Subject: [PATCH] Fix memory tracking performance regression (#2026)

* Fix memory tracking performance regression

* Set PTC version
---
 ARMeilleure/Instructions/InstEmitMemoryHelper.cs | 6 +++---
 ARMeilleure/Translation/PTC/Ptc.cs               | 2 +-
 Ryujinx.Cpu/MemoryManager.cs                     | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/ARMeilleure/Instructions/InstEmitMemoryHelper.cs b/ARMeilleure/Instructions/InstEmitMemoryHelper.cs
index cb4fae8f9b..2d7b6799ea 100644
--- a/ARMeilleure/Instructions/InstEmitMemoryHelper.cs
+++ b/ARMeilleure/Instructions/InstEmitMemoryHelper.cs
@@ -334,14 +334,14 @@ namespace ARMeilleure.Instructions
             {
                 if (write)
                 {
-                    pte = context.ShiftLeft(pte, Const(1));
                     context.BranchIf(lblSlowPath, pte, Const(0L), Comparison.LessOrEqual);
-                    pte = context.ShiftRightUI(pte, Const(1));
+                    pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by C# memory access)
                 }
                 else
                 {
+                    pte = context.ShiftLeft(pte, Const(1));
                     context.BranchIf(lblSlowPath, pte, Const(0L), Comparison.LessOrEqual);
-                    pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by C# memory access)
+                    pte = context.ShiftRightUI(pte, Const(1));
                 }
             }
             else
diff --git a/ARMeilleure/Translation/PTC/Ptc.cs b/ARMeilleure/Translation/PTC/Ptc.cs
index f3f209f0c3..40d2ad5c1a 100644
--- a/ARMeilleure/Translation/PTC/Ptc.cs
+++ b/ARMeilleure/Translation/PTC/Ptc.cs
@@ -22,7 +22,7 @@ namespace ARMeilleure.Translation.PTC
     {
         private const string HeaderMagic = "PTChd";
 
-        private const int InternalVersion = 1987; //! To be incremented manually for each change to the ARMeilleure project.
+        private const int InternalVersion = 2026; //! To be incremented manually for each change to the ARMeilleure project.
 
         private const string ActualDir = "0";
         private const string BackupDir = "1";
diff --git a/Ryujinx.Cpu/MemoryManager.cs b/Ryujinx.Cpu/MemoryManager.cs
index 8c8bd3a4c2..c85a481ee5 100644
--- a/Ryujinx.Cpu/MemoryManager.cs
+++ b/Ryujinx.Cpu/MemoryManager.cs
@@ -561,7 +561,7 @@ namespace Ryujinx.Cpu
             long tag = protection switch
             {
                 MemoryPermission.None => 0L,
-                MemoryPermission.Read => 2L << PointerTagBit,
+                MemoryPermission.Write => 2L << PointerTagBit,
                 _ => 3L << PointerTagBit
             };
 
@@ -631,7 +631,7 @@ namespace Ryujinx.Cpu
             // tracking using host guard pages in future, but also supporting platforms where this is not possible.
 
             // Write tag includes read protection, since we don't have any read actions that aren't performed before write too.
-            long tag = (write ? 3L : 2L) << PointerTagBit;
+            long tag = (write ? 3L : 1L) << PointerTagBit;
 
             ulong endVa = (va + size + PageMask) & ~(ulong)PageMask;