mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-06-28 00:16:23 +02:00
implement quickAccess for fast look-up
some values are reused often and will be quicker to access by first checking an array of the last used items. removed and renamed some older functions.
This commit is contained in:
parent
be8f4897a2
commit
ce438f7276
2 changed files with 85 additions and 184 deletions
|
@ -335,7 +335,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
ulong alignedEndAddress = (endAddress + alignmentMask) & ~alignmentMask;
|
||||
ulong alignedSize = alignedEndAddress - alignedAddress;
|
||||
|
||||
Buffer buffer = _buffers.FindFirstOverlap(alignedAddress, alignedSize);
|
||||
Buffer buffer = _buffers.FindOverlapFast(alignedAddress, alignedSize);
|
||||
BufferRange bufferRange = buffer.GetRange(alignedAddress, alignedSize, false);
|
||||
|
||||
alignedSubRanges[i] = new MemoryRange(alignedAddress, alignedSize);
|
||||
|
@ -402,7 +402,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
if (subRange.Address != MemoryManager.PteUnmapped)
|
||||
{
|
||||
Buffer buffer = _buffers.FindFirstOverlap(subRange.Address, subRange.Size);
|
||||
Buffer buffer = _buffers.FindOverlapFast(subRange.Address, subRange.Size);
|
||||
|
||||
virtualBuffer.AddPhysicalDependency(buffer, subRange.Address, dstOffset, subRange.Size);
|
||||
physicalBuffers.Add(buffer);
|
||||
|
@ -890,7 +890,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
{
|
||||
MemoryRange subRange = range.GetSubRange(i);
|
||||
|
||||
Buffer subBuffer = _buffers.FindFirstOverlap(subRange.Address, subRange.Size);
|
||||
Buffer subBuffer = _buffers.FindOverlapFast(subRange.Address, subRange.Size);
|
||||
|
||||
subBuffer.SynchronizeMemory(subRange.Address, subRange.Size);
|
||||
|
||||
|
@ -938,7 +938,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
if (size != 0)
|
||||
{
|
||||
buffer = _buffers.FindFirstOverlap(address, size);
|
||||
buffer = _buffers.FindOverlapFast(address, size);
|
||||
|
||||
buffer.CopyFromDependantVirtualBuffers();
|
||||
buffer.SynchronizeMemory(address, size);
|
||||
|
@ -950,7 +950,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
}
|
||||
else
|
||||
{
|
||||
buffer = _buffers.FindFirstOverlap(address, 1);
|
||||
buffer = _buffers.FindOverlapFast(address, 1);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
|
@ -988,7 +988,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
{
|
||||
if (size != 0)
|
||||
{
|
||||
Buffer buffer = _buffers.FindFirstOverlap(address, size);
|
||||
Buffer buffer = _buffers.FindOverlapFast(address, size);
|
||||
|
||||
if (copyBackVirtual)
|
||||
{
|
||||
|
|
|
@ -1,27 +1,17 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Memory.Range
|
||||
{
|
||||
public readonly struct RangeItem<TValue> where TValue : IRange
|
||||
public readonly struct RangeItem<TValue>(TValue value) where TValue : IRange
|
||||
{
|
||||
public readonly ulong Address;
|
||||
public readonly ulong EndAddress;
|
||||
public readonly ulong Address = value.Address;
|
||||
public readonly ulong EndAddress = value.Address + value.Size;
|
||||
|
||||
public readonly TValue Value;
|
||||
|
||||
public RangeItem(TValue value)
|
||||
{
|
||||
Value = value;
|
||||
|
||||
Address = value.Address;
|
||||
EndAddress = value.Address + value.Size;
|
||||
}
|
||||
public readonly TValue Value = value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool OverlapsWith(ulong address, ulong endAddress)
|
||||
|
@ -29,6 +19,7 @@ namespace Ryujinx.Memory.Range
|
|||
return Address < endAddress && address < EndAddress;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Result of an Overlaps Finder function.
|
||||
/// </summary>
|
||||
|
@ -62,7 +53,12 @@ namespace Ryujinx.Memory.Range
|
|||
|
||||
public int Count { get; protected set; }
|
||||
|
||||
public readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
|
||||
public readonly ReaderWriterLockSlim Lock = new();
|
||||
|
||||
private const int QuickAccessLength = 8;
|
||||
private int _offset;
|
||||
private int _count;
|
||||
private RangeItem<T>[] _quickAccess = new RangeItem<T>[QuickAccessLength];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new range list.
|
||||
|
@ -95,7 +91,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
/// <returns>True if the item was located and updated, false otherwise</returns>
|
||||
public bool Update(T item)
|
||||
protected bool Update(T item)
|
||||
{
|
||||
int index = BinarySearchLeftEdge(item.Address);
|
||||
|
||||
|
@ -106,6 +102,10 @@ namespace Ryujinx.Memory.Range
|
|||
if (_items[index].Value.Equals(item))
|
||||
{
|
||||
_items[index] = new RangeItem<T>(item);
|
||||
|
||||
_quickAccess = new RangeItem<T>[QuickAccessLength];
|
||||
_count = 0;
|
||||
_offset = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -147,27 +147,19 @@ namespace Ryujinx.Memory.Range
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RemoveAt(int index)
|
||||
protected void RemoveAt(int index)
|
||||
{
|
||||
if (index < --Count)
|
||||
{
|
||||
Array.Copy(_items, index + 1, _items, index, Count - index);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveRange(int start, int end)
|
||||
{
|
||||
if (end <= Count)
|
||||
{
|
||||
Array.Copy(_items, end, _items, start, Count - end);
|
||||
Count -= end - start;
|
||||
}
|
||||
else if (end == Count)
|
||||
{
|
||||
Count = start;
|
||||
}
|
||||
_quickAccess = new RangeItem<T>[QuickAccessLength];
|
||||
_count = 0;
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RemoveRange(OverlapResult overlapResult)
|
||||
{
|
||||
if (overlapResult.EndIndex < Count)
|
||||
|
@ -179,6 +171,10 @@ namespace Ryujinx.Memory.Range
|
|||
{
|
||||
Count = overlapResult.StartIndex;
|
||||
}
|
||||
|
||||
_quickAccess = new RangeItem<T>[QuickAccessLength];
|
||||
_count = 0;
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -214,62 +210,39 @@ namespace Ryujinx.Memory.Range
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an item's end address.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
public void UpdateEndAddress(T item)
|
||||
{
|
||||
int index = BinarySearchLeftEdge(item.Address);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
while (index < Count)
|
||||
{
|
||||
if (_items[index].Value.Equals(item))
|
||||
{
|
||||
_items[index] = new RangeItem<T>(item);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_items[index].Address > item.Address)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first item on the list overlapping in memory with the specified item.
|
||||
/// Gets an item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Despite the name, this has no ordering guarantees of the returned item.
|
||||
/// It only ensures that the item returned overlaps the specified item.
|
||||
/// </remarks>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
||||
public T FindFirstOverlap(T item)
|
||||
{
|
||||
return FindFirstOverlap(item.Address, item.Size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Despite the name, this has no ordering guarantees of the returned item.
|
||||
/// This has no ordering guarantees of the returned item.
|
||||
/// It only ensures that the item returned overlaps the specified memory range.
|
||||
/// </remarks>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
||||
public T FindFirstOverlap(ulong address, ulong size)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T FindOverlapFast(ulong address, ulong size)
|
||||
{
|
||||
for (int i = 0; i < _quickAccess.Length; i++)
|
||||
{
|
||||
ref RangeItem<T> item = ref _quickAccess[(i + _offset) % _quickAccess.Length];
|
||||
|
||||
if (item.OverlapsWith(address, address + size))
|
||||
{
|
||||
return item.Value;
|
||||
}
|
||||
}
|
||||
|
||||
int index = BinarySearch(address, address + size);
|
||||
|
||||
if (_count < _quickAccess.Length)
|
||||
{
|
||||
_quickAccess[_count++] = _items[index];
|
||||
}
|
||||
else
|
||||
{
|
||||
_quickAccess[_offset++ % _quickAccess.Length] = _items[index];
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return default;
|
||||
|
@ -277,18 +250,7 @@ namespace Ryujinx.Memory.Range
|
|||
|
||||
return _items[index].Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items overlapping with the specified item in memory.
|
||||
/// </summary>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public OverlapResult FindOverlaps(T item, ref RangeItem<T>[] output)
|
||||
{
|
||||
return FindOverlaps(item.Address, item.Size, ref output);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
|
@ -301,10 +263,13 @@ namespace Ryujinx.Memory.Range
|
|||
int outputIndex = 0;
|
||||
|
||||
ulong endAddress = address + size;
|
||||
|
||||
|
||||
int startIndex = BinarySearchLeftEdge(address);
|
||||
if (startIndex < 0)
|
||||
startIndex = ~startIndex;
|
||||
int endIndex = -1;
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
for (int i = startIndex; i < Count; i++)
|
||||
{
|
||||
ref RangeItem<T> item = ref _items[i];
|
||||
|
||||
|
@ -325,29 +290,26 @@ namespace Ryujinx.Memory.Range
|
|||
endIndex = Count;
|
||||
}
|
||||
|
||||
if (endIndex - outputIndex >= 0)
|
||||
if (outputIndex > 0 && outputIndex == endIndex - startIndex)
|
||||
{
|
||||
Array.Resize(ref output, outputIndex);
|
||||
Array.Copy(_items, endIndex - outputIndex, output, 0, outputIndex);
|
||||
|
||||
return new OverlapResult(startIndex, endIndex);
|
||||
}
|
||||
|
||||
return new OverlapResult(endIndex - outputIndex, endIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items overlapping with the specified item in memory.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method only returns correct results if none of the items on the list overlaps with
|
||||
/// each other. If that is not the case, this method should not be used.
|
||||
/// This method is faster than the regular method to find all overlaps.
|
||||
/// </remarks>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public OverlapResult FindOverlapsNonOverlapping(T item, ref RangeItem<T>[] output)
|
||||
{
|
||||
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
|
||||
else if (outputIndex > 0)
|
||||
{
|
||||
Array.Resize(ref output, outputIndex);
|
||||
int arrIndex = 0;
|
||||
for (int i = startIndex; i < endIndex; i++)
|
||||
{
|
||||
output[arrIndex++] = _items[i];
|
||||
}
|
||||
|
||||
return new OverlapResult(endIndex - outputIndex, endIndex);
|
||||
}
|
||||
|
||||
return new OverlapResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -362,6 +324,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public OverlapResult FindOverlapsNonOverlapping(ulong address, ulong size, ref RangeItem<T>[] output)
|
||||
{
|
||||
// This is a bit faster than FindOverlaps, but only works
|
||||
|
@ -380,6 +343,7 @@ namespace Ryujinx.Memory.Range
|
|||
return new OverlapResult(index, endIndex);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public OverlapResult FindOverlapsNonOverlappingAsSpan(ulong address, ulong size, out ReadOnlySpan<RangeItem<T>> span)
|
||||
{
|
||||
// This is a bit faster than FindOverlaps, but only works
|
||||
|
@ -410,6 +374,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>Range information of overlapping items found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public OverlapResult FindOverlapsNonOverlapping(ulong address, ulong size)
|
||||
{
|
||||
// This is a bit faster than FindOverlaps, but only works
|
||||
|
@ -428,6 +393,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// <param name="address">Address to find</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of matches found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public OverlapResult FindOverlaps(ulong address, ref RangeItem<T>[] output)
|
||||
{
|
||||
(int index, int endIndex) = BinarySearchEdges(address);
|
||||
|
@ -446,6 +412,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// </summary>
|
||||
/// <param name="address">Address to find</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int BinarySearch(ulong address)
|
||||
{
|
||||
int left = 0;
|
||||
|
@ -482,6 +449,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// </summary>
|
||||
/// <param name="address">Address to find</param>
|
||||
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int BinarySearchLeftEdge(ulong address)
|
||||
{
|
||||
if (Count == 0)
|
||||
|
@ -550,6 +518,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private (int, int) BinarySearchEdges(ulong address)
|
||||
{
|
||||
if (Count == 0)
|
||||
|
@ -679,6 +648,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int BinarySearch(ulong address, ulong endAddress)
|
||||
{
|
||||
int left = 0;
|
||||
|
@ -716,76 +686,7 @@ namespace Ryujinx.Memory.Range
|
|||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
|
||||
private int BinarySearchLeftEdge(ulong address, ulong endAddress)
|
||||
{
|
||||
if (Count == 0)
|
||||
return ~0;
|
||||
|
||||
if (Count == 1)
|
||||
{
|
||||
ref RangeItem<T> item = ref _items[0];
|
||||
|
||||
if (item.OverlapsWith(address, endAddress))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
return ~0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ~1;
|
||||
}
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref _items[middle];
|
||||
|
||||
bool match = item.OverlapsWith(address, endAddress);
|
||||
|
||||
if (range == 0)
|
||||
{
|
||||
if (match)
|
||||
return middle;
|
||||
else if (address < item.Address)
|
||||
return ~(right);
|
||||
else
|
||||
return ~(right + 1);
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
right = middle;
|
||||
}
|
||||
else if(address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private (int, int) BinarySearchEdges(ulong address, ulong endAddress)
|
||||
{
|
||||
if (Count == 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue