-
Notifications
You must be signed in to change notification settings - Fork 122
Add WindowsRuntimeMarshal.TryGetDataUnsafe and use it in various places. #1556
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
Add WindowsRuntimeMarshal.TryGetDataUnsafe and use it in various places. #1556
Conversation
9dadf5a to
bdb090a
Compare
bdb090a to
ef7a2c3
Compare
src/cswinrt/strings/additions/Windows.Storage.Streams/IBufferByteAccess.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeBufferExtensions.cs
Outdated
Show resolved
Hide resolved
4ce4fb2 to
db6c137
Compare
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeBufferExtensions.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeBufferExtensions.cs
Outdated
Show resolved
Hide resolved
dec5ec2 to
c3bb701
Compare
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/IBufferByteAccess.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/IBufferByteAccess.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeBufferExtensions.cs
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
|
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] |
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
9683419 to
16f72ab
Compare
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Outdated
Show resolved
Hide resolved
src/cswinrt/strings/additions/Windows.Storage.Streams/WindowsRuntimeMarshal.cs
Show resolved
Hide resolved
| if (ComWrappersSupport.TryUnwrapObject(buffer, out var unwrapped) && | ||
| unwrapped.TryAs(global::WinRT.Interop.IID.IID_IBufferByteAccess, out IntPtr ThisPtr) >= 0) | ||
| { | ||
| try |
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Allows unsafe access to the underlying buffer data.