Skip to content

Eliminate allocation in StreamAsIStream.Read #6632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using MS.Internal;
using MS.Win32;
using System.Buffers;
using System.Reflection;
using System.Collections;
using System.Diagnostics;
Expand All @@ -28,7 +29,7 @@ namespace System.Windows.Media
internal struct StreamDescriptor
{
internal delegate void Dispose(ref StreamDescriptor pSD);
internal delegate int Read(ref StreamDescriptor pSD, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), Out]byte[] buffer, uint cb, out uint cbRead);
internal delegate int Read(ref StreamDescriptor pSD, IntPtr buffer, uint cb, out uint cbRead);

internal unsafe delegate int Seek(ref StreamDescriptor pSD, long offset, uint origin, long* plibNewPostion);
internal delegate int Stat(ref StreamDescriptor pSD, out System.Runtime.InteropServices.ComTypes.STATSTG statstg, uint grfStatFlag);
Expand Down Expand Up @@ -237,7 +238,7 @@ public int CopyTo(IntPtr /* IStream */ pstm, long cb, out long cbRead, out long

uint read = 0;

hr = Read(buffer, toRead, out read);
hr = Read(buffer.AsSpan(0, (int) toRead), out read);

if (read == 0)
{
Expand Down Expand Up @@ -290,7 +291,7 @@ public int LockRegion(long libOffset, long cb, uint dwLockType)
return NativeMethods.E_NOTIMPL;
}

public int Read(byte[] buffer, uint cb, out uint cbRead)
public int Read(Span<byte> buffer, out uint cbRead)
{
cbRead = 0;

Expand All @@ -301,7 +302,7 @@ public int Read(byte[] buffer, uint cb, out uint cbRead)
Verify();
ActualizeVirtualPosition();

cbRead = (uint) dataStream.Read(buffer, 0, (int) cb);
cbRead = (uint) dataStream.Read(buffer);
}
catch (Exception e)
{
Expand Down Expand Up @@ -597,9 +598,10 @@ internal static int LockRegion(ref StreamDescriptor pSD, long libOffset, long cb
return (StreamAsIStream.FromSD(ref pSD)).LockRegion(libOffset, cb, dwLockType);
}

internal static int Read(ref StreamDescriptor pSD, byte[] buffer, uint cb, out uint cbRead)
internal static unsafe int Read(ref StreamDescriptor pSD, IntPtr buffer, uint cb, out uint cbRead)
{
return (StreamAsIStream.FromSD(ref pSD)).Read(buffer, cb, out cbRead);
var span = new Span<byte>(buffer.ToPointer(), (int) cb);
return (StreamAsIStream.FromSD(ref pSD)).Read(span, out cbRead);
}

internal static int Revert(ref StreamDescriptor pSD)
Expand Down