Skip to content
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

Generic CreateMarshaler #1079

Merged
merged 3 commits into from
Jan 19, 2022
Merged

Generic CreateMarshaler #1079

merged 3 commits into from
Jan 19, 2022

Conversation

j0shuams
Copy link
Contributor

@j0shuams j0shuams commented Jan 18, 2022

This PR adds a new public API to MarshalInspectable, which allows calls to CreateMarshaler to pass in a VTable as generic param and an IID as a function param. This reduces .As() call and prevents unnecessary allocations.

In the table below, "Unwrap" refers to the case where CreateMarshaler returns early, and "CreateCCW" is the case where CreateMarshaler calls the CreateCCW function. Time is measured in microseconds (us) and memory is measured in bytes (B).

Net5.0

Codepath Master - time Topic - time Master - memory Topic - memory
Unwrap 5.243 us 4.947 us 464 B 344 B
CreateCCW 10.784 us 8.52 us 768 B 648 B

NetStandard2.0

Codepath Master - time Topic - time Master - memory Topic - memory
Unwrap 8.997 us 8.288 us 1560 B 1440 B
CreateCCW 69.445 us 67.744 us 4400 B 4241 B

While saving only a microsecond or two, we consistently use at least 120 B less of memory everytime we call CreateMarshaler.

Will provide a diff of generated code

Below is an example of a common pattern in projected code for structs used with marshaling, we provide a new (generic) function that handles the implementation that does not need the same subsequent method calls.

namespace ABI.Windows.System.Profile
{
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    public struct AnalyticsVersionInfo
    {
        public static IObjectReference CreateMarshaler(global::Windows.System.Profile.AnalyticsVersionInfo obj) => 
          obj is null ? 
            null : 
            // new 
             MarshalInspectable<global::Windows.System.Profile.AnalyticsVersionInfo>.CreateMarshaler<IUnknownVftbl>(obj, GuidGenerator.GetIID(typeof(global::Windows.System.Profile.IAnalyticsVersionInfo).GetHelperType())
            /* old 
             MarshalInspectable<global::Windows.System.Profile.AnalyticsVersionInfo>.CreateMarshaler(obj).As<IUnknownVftbl>(GuidGenerator.GetIID(typeof(global::Windows.System.Profile.IAnalyticsVersionInfo).GetHelperType()) */
           );
    }
}

@j0shuams j0shuams merged commit bfd6846 into master Jan 19, 2022
@j0shuams j0shuams deleted the jlarkin/new-create-marshaler branch January 19, 2022 21:18
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.

2 participants