Skip to content

Conversation

@jlaanstra
Copy link
Collaborator

Allows unsafe access to the underlying buffer data.

@jlaanstra jlaanstra force-pushed the user/jlaans/getdataunsafe branch from 9dadf5a to bdb090a Compare March 26, 2024 03:33
@jlaanstra jlaanstra changed the title Add TryGetDataUnsafe and use it in various places. Add IBuffer.TryGetDataUnsafe and use it in various places. Mar 26, 2024
@jlaanstra jlaanstra force-pushed the user/jlaans/getdataunsafe branch from bdb090a to ef7a2c3 Compare March 26, 2024 04:25
@jlaanstra jlaanstra force-pushed the user/jlaans/getdataunsafe branch from 4ce4fb2 to db6c137 Compare March 26, 2024 17:02
@jlaanstra jlaanstra force-pushed the user/jlaans/getdataunsafe branch from dec5ec2 to c3bb701 Compare April 19, 2024 20:50
@jlaanstra jlaanstra changed the title Add IBuffer.TryGetDataUnsafe and use it in various places. Add WindowsRuntimeMarshal.TryGetDataUnsafe and use it in various places. Apr 19, 2024
@manodasanW
Copy link
Member

D:\a_work\1\s\src\Projections\Windows\Generated Files\net8.0\CsWinRT\Windows.Storage.Streams.cs(8700,37): error CS0234: The type or namespace name 'IBufferByteAccessMethods' does not exist in the namespace 'ABI.Windows.Storage.Streams' (are you missing an assembly reference?) [D:\a_work\1\s\src\Projections\Windows\Windows.csproj::TargetFramework=net8.0]

@jlaanstra jlaanstra force-pushed the user/jlaans/getdataunsafe branch from 9683419 to 16f72ab Compare April 30, 2024 23:40
@jlaanstra jlaanstra requested a review from Sergio0694 April 30, 2024 23:41
if (ComWrappersSupport.TryUnwrapObject(buffer, out var unwrapped) &&
unwrapped.TryAs(global::WinRT.Interop.IID.IID_IBufferByteAccess, out IntPtr ThisPtr) >= 0)
{
try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could optimize this to not need this try/finally:

IntPtr __retval;
int hresult = (*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)ThisPtr)[3](ThisPtr, &__retval);
dataPtr = __retval;

Marshal.Release(ThisPtr);

global::WinRT.ExceptionHelpers.ThrowExceptionForHR(hresult);

return true;

But also, why are we throwing in the first place here, given the method is named "Try"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try doesn't mean we never throw. Try here means we are not throwing if the type doesn't implement IBufferByteAccess. If it does implement it but the implementation is bad we still throw.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure that's intuitive, but I suppose it's fine. Can we drop the try/finally though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's an optimization though.
try/finally doesn't have performance impact for fast path (no exception being thrown) and it can make sure the finally block being executed once there's an exception. You can check the codegen, unless you don't have catch blocks in a try, it shouldn't make any meaningful impact on the performance.

@manodasanW manodasanW merged commit d8b847e into microsoft:staging/AOT May 7, 2024
@jlaanstra jlaanstra deleted the user/jlaans/getdataunsafe branch May 7, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants