skip unnecessary copy

merge-requests/195/head
LotP1 2025-10-26 19:22:40 +01:00
parent 7adea58204
commit 1dcda7f8fd
8 changed files with 78 additions and 15 deletions

View File

@ -151,6 +151,11 @@ namespace Ryujinx.Cpu.AppleHv
}
}
public override bool TryReadUnsafe(ulong va, int length, out Span<byte> data)
{
throw new NotImplementedException();
}
public override void Write(ulong va, ReadOnlySpan<byte> data)
{
try

View File

@ -175,6 +175,11 @@ namespace Ryujinx.Cpu.Jit
}
}
public override bool TryReadUnsafe(ulong va, int length, out Span<byte> data)
{
throw new NotImplementedException();
}
public override void Write(ulong va, ReadOnlySpan<byte> data)
{
try

View File

@ -152,6 +152,11 @@ namespace Ryujinx.Cpu.Jit
}
}
public override bool TryReadUnsafe(ulong va, int length, out Span<byte> data)
{
throw new NotImplementedException();
}
public override T ReadTracked<T>(ulong va)
{
try

View File

@ -228,6 +228,11 @@ namespace Ryujinx.Cpu.Jit
}
}
public override bool TryReadUnsafe(ulong va, int length, out Span<byte> data)
{
throw new NotImplementedException();
}
public override bool WriteWithRedundancyCheck(ulong va, ReadOnlySpan<byte> data)
{
if (data.Length == 0)

View File

@ -123,15 +123,15 @@ namespace Ryujinx.HLE.HOS.Services.Nv
return NvResult.InvalidSize;
}
byte[] outputData = new byte[outputDataSize];
byte[] temp = new byte[inputDataSize];
context.Memory.Read(inputDataPosition, temp);
Buffer.BlockCopy(temp, 0, outputData, 0, temp.Length);
arguments = new Span<byte>(outputData);
if (!context.Memory.TryReadUnsafe(inputDataPosition, (int)inputDataSize, out arguments))
{
arguments = new byte[inputDataSize];
context.Memory.Read(inputDataPosition, arguments);
}
else
{
arguments = arguments.ToArray();
}
}
else if (isWrite)
{
@ -471,11 +471,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
byte[] temp = new byte[inlineInBufferSize];
context.Memory.Read(inlineInBufferPosition, temp);
Span<byte> inlineInBuffer = new(temp);
if (!context.Memory.TryReadUnsafe(inlineInBufferPosition, (int)inlineInBufferSize, out Span<byte> inlineInBuffer))
{
inlineInBuffer = new byte[inlineInBufferSize];
context.Memory.Read(inlineInBufferPosition, inlineInBuffer);
}
if (errorCode == NvResult.Success)
{

View File

@ -61,6 +61,15 @@ namespace Ryujinx.Memory
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
void Read(ulong va, Span<byte> data);
/// <summary>
/// Gets a span of CPU mapped memory.
/// </summary>
/// <param name="va">Virtual address of the data in memory</param>
/// <param name="length">Length of the data in memory</param>
/// <param name="data">Span that references the data being read</param>
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
bool TryReadUnsafe(ulong va, int length, out Span<byte> data);
/// <summary>
/// Writes data to CPU mapped memory.
/// </summary>

View File

@ -159,6 +159,13 @@ namespace Ryujinx.Memory
AssertValidAddressAndSize(va, data.Length);
if (IsContiguousAndMapped(va, data.Length))
{
nuint pa = TranslateVirtualAddressChecked(va);
GetPhysicalAddressSpan(pa, data.Length).CopyTo(data);
}
int offset = 0, size;
if ((va & PageMask) != 0)
@ -182,6 +189,28 @@ namespace Ryujinx.Memory
}
}
public virtual bool TryReadUnsafe(ulong va, int length, out Span<byte> data)
{
if (!IsContiguousAndMapped(va, length))
{
data = default;
return false;
}
if (length == 0)
{
data = Span<byte>.Empty;
return true;
}
AssertValidAddressAndSize(va, length);
nuint pa = TranslateVirtualAddressChecked(va);
data = GetPhysicalAddressSpan(pa, length);
return true;
}
public virtual T ReadTracked<T>(ulong va) where T : unmanaged
{
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);

View File

@ -43,6 +43,11 @@ namespace Ryujinx.Tests.Memory
throw new NotImplementedException();
}
public bool TryReadUnsafe(ulong va, int lenfth, out Span<byte> data)
{
throw new NotImplementedException();
}
public void Write<T>(ulong va, T value) where T : unmanaged
{
throw new NotImplementedException();