Skip to content

Conversation

h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Jun 6, 2024

Description

Replaces Marshal.SizeOf calls for primitive types with sizeof operator, saving some unnecessary overhead as the codegen results in a constant number generated in compile time.

I've also replaced the calls for Marshal.SizeOf(typeof(Guid)) with Unsafe.SizeOf<Guid>() as the source counts on 16 bytes in all instances I could find anyways.

Finally, I've replaced some instances of weird ways to retrieve IntPtr size, using its property instead, which also results in a constant in runtime.

Method Mean [ns] Error [ns] StdDev [ns] Code Size [B] Allocated [B]
Original 117.3583 ns 0.3339 ns 0.2960 ns 689 B -
PR__EDIT 0.2285 ns 0.0056 ns 0.0047 ns 81 B -
[Benchmark]
public void Original()
{
    SizeOfInt = (uint)Marshal.SizeOf(typeof(int));
    SizeOfUInt = (uint)Marshal.SizeOf(typeof(uint));
    SizeOfUShort = (uint)Marshal.SizeOf(typeof(ushort));
    SizeOfByte = (uint)Marshal.SizeOf(typeof(byte));
    SizeOfFloat = (uint)Marshal.SizeOf(typeof(float));
    SizeOfDouble = (uint)Marshal.SizeOf(typeof(double));
    SizeOfGuid = (uint)Marshal.SizeOf(typeof(Guid));
    SizeOfDecimal = (uint)Marshal.SizeOf(typeof(decimal));
}

[Benchmark]
public void PR__EDIT()
{
    SizeOfInt = sizeof(int);
    SizeOfUInt = sizeof(uint);
    SizeOfUShort = sizeof(ushort);
    SizeOfByte = sizeof(byte);
    SizeOfFloat = sizeof(float);
    SizeOfDouble = sizeof(double);
    SizeOfGuid = (uint)Unsafe.SizeOf<Guid>();
    SizeOfDecimal = sizeof(decimal);
}

Customer Impact

Improved performance, less overhead. Native class static constructor goes from 120ns to 0.02ns for example.

Regression

No.

Testing

Verifying basic library functionality. Changes are minimal.

Risk

No.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested a review from a team as a code owner June 6, 2024 16:03
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jun 6, 2024
@h3xds1nz h3xds1nz force-pushed the replace-static-sizeof branch from d669eb7 to 6d427c5 Compare July 27, 2024 16:36
@h3xds1nz h3xds1nz requested a review from a team as a code owner July 27, 2024 16:36
@h3xds1nz
Copy link
Member Author

@dipeshmsft I have rebased and fixed the conflict from #9436

@harshit7962 harshit7962 merged commit 812bbba into dotnet:main Oct 1, 2024
8 checks passed
@harshit7962
Copy link
Member

@h3xds1nz Thank you for your contributions.

@h3xds1nz
Copy link
Member Author

h3xds1nz commented Oct 1, 2024

@harshit7962 Awesome, thank you, time for more changes haha.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants