Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Simplify sspicli interop marshaling #29031

Merged
merged 3 commits into from
Apr 11, 2018
Merged

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Apr 11, 2018

This makes it both faster and more AOT friendly

Contributes to dotnet/corert#5674

jkotas added 2 commits April 11, 2018 08:41
This makes it both faster and more AOT friendly
@davidsh davidsh requested a review from stephentoub April 11, 2018 16:58
@@ -38,27 +36,23 @@ public static unsafe byte[] ToByteArray(List<SslApplicationProtocol> application
}

Sec_Application_Protocols protocols = new Sec_Application_Protocols();
protocols.ProtocolListsSize = (uint)(ProtocolListConstSize + protocolListSize);
protocols.ProtocolExtenstionType = ApplicationProtocolNegotiationExt.ALPN;
protocols.ProtocolListsSize = (uint)((sizeof(Sec_Application_Protocols) - sizeof(uint)) + protocolListSize);
Copy link
Member

Choose a reason for hiding this comment

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

The previous code was semi-self-documenting as to what was being calculated. That's lost a bit here. Can you add a comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@@ -55,11 +55,11 @@ internal static class WinHttpCertificateHelper
unsafe
{
var cppStruct = new Interop.Crypt32.CERT_CHAIN_POLICY_PARA();
cppStruct.cbSize = (uint)Marshal.SizeOf<Interop.Crypt32.CERT_CHAIN_POLICY_PARA>();
cppStruct.cbSize = (uint)sizeof(Interop.Crypt32.CERT_CHAIN_POLICY_PARA);
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the rules regarding when it is safe to use sizeof() vs. Marshal.SizeOf<T>() ?

Copy link
Member

Choose a reason for hiding this comment

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

Basically you can use sizeof unless there is marshaling logic involved that would change the size of the type, e.g. if a field was attributed with MarshalAs that would convert it from one thing to another of a different size during the marshaling process. If the type is blittable, sizeof is the way to go.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is safe to use sizeof for blittable types. Both sizeof() and Marshal.SizeOf<T>() return same value for blittable types, except that sizeof is faster.

We have some of the best practices for interop perf documented in: https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/interop-guidelines.md#definition

protocols.ProtocolListSize = (short)protocolListSize;

Span<byte> pBuffer = new byte[protocolListSize];
byte[] buffer = new byte[sizeof(Sec_Application_Protocols) + protocolListSize];
Copy link
Member

Choose a reason for hiding this comment

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

This appears to be a larger value than it was before. Was it broken before?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see, this is replacing a second allocation made below. Nice.

Copy link
Member Author

Choose a reason for hiding this comment

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

The original code allocated two buffers: First temporary buffer was protocolListSize, and second final buffer was sizeof(Sec_Application_Protocols) + protocolListSize. I got rid of the temporary buffer allocation while I was on it.

@davidsh
Copy link
Contributor

davidsh commented Apr 11, 2018

cc: @dotnet/ncl This is a good PR to learn about best-practice rules for optimum p/invokes.

@stephentoub
Copy link
Member

@dotnet-bot test Outerloop Windows x64 Debug Build please

@stephentoub
Copy link
Member

@dotnet-bot test Windows x86 Release Build please
@dotnet-bot test OSX x64 Debug Build please

@stephentoub stephentoub added this to the 2.1.0 milestone Apr 11, 2018
@stephentoub
Copy link
Member

@dotnet-bot test Outerloop Windows x64 Debug Build please

@stephentoub
Copy link
Member

stephentoub commented Apr 11, 2018

Outerloop failures all due to https://github.com/dotnet/corefx/issues/28631

@stephentoub stephentoub merged commit 437e882 into dotnet:master Apr 11, 2018
@jkotas jkotas deleted the simple-interop branch April 11, 2018 22:49
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Delete unnecessary wrapper

* Simplify sspicli interop marshaling

This makes it both faster and more AOT friendly

* CR feedback


Commit migrated from dotnet/corefx@437e882
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants