From 65f2a82b9745548748aa1d68547aff7a2cac574a Mon Sep 17 00:00:00 2001 From: gdk Date: Fri, 15 Jul 2022 17:05:14 -0300 Subject: [PATCH] Optimize PlaceholderManager.UnreserveRange --- Ryujinx.Memory/WindowsShared/MappingTree.cs | 4 +++- .../WindowsShared/PlaceholderManager.cs | 20 +++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Ryujinx.Memory/WindowsShared/MappingTree.cs b/Ryujinx.Memory/WindowsShared/MappingTree.cs index 8f880f0c..7a18d457 100644 --- a/Ryujinx.Memory/WindowsShared/MappingTree.cs +++ b/Ryujinx.Memory/WindowsShared/MappingTree.cs @@ -9,6 +9,8 @@ namespace Ryujinx.Memory.WindowsShared /// Type of the value stored on the node class MappingTree : IntrusiveRedBlackTree> { + private const int ArrayGrowthSize = 16; + public int GetNodes(ulong start, ulong end, ref RangeNode[] overlaps, int overlapCount = 0) { RangeNode node = GetNode(new RangeNode(start, start + 1UL, default)); @@ -17,7 +19,7 @@ namespace Ryujinx.Memory.WindowsShared { if (overlaps.Length <= overlapCount) { - Array.Resize(ref overlaps, overlapCount + 1); + Array.Resize(ref overlaps, overlapCount + ArrayGrowthSize); } overlaps[overlapCount++] = node; diff --git a/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs b/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs index b08a91e0..1722d528 100644 --- a/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs +++ b/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs @@ -86,26 +86,26 @@ namespace Ryujinx.Memory.WindowsShared { ulong endAddress = address + size; - var overlaps = new RangeNode[InitialOverlapsSize]; - int count; - lock (_mappings) { - count = _mappings.GetNodes(address, endAddress, ref overlaps); + RangeNode node = _mappings.GetNode(new RangeNode(address, address + 1UL, default)); - for (int index = 0; index < count; index++) + for (; node != null; node = node.Successor) { - var overlap = overlaps[index]; - - if (IsMapped(overlap.Value)) + if (IsMapped(node.Value)) { - if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2)) + if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)node.Start, 2)) { throw new WindowsApiException("UnmapViewOfFile2"); } } - _mappings.Remove(overlap); + _mappings.Remove(node); + + if (node.End >= endAddress) + { + break; + } } }