dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

Replace our use of Marshal.SizeOf with C# sizeof keyword #475

Closed AArnott closed 4 years ago

AArnott commented 4 years ago

Marshal.SizeOf returns the size of the unmanaged view of the struct. sizeof returns the size of the managed view of the struct.

Unmanaged and managed views are identical for the blittable structs. It is better to use sizeof instead of Marshal.SizeOf for blittable structs. They return the same number and sizeof is much cheaper.

Originally posted by @jkotas in https://github.com/dotnet/pinvoke/pull/463

Since all our structs are (or should be) blittable, we should confirm each one and replace the slow method with the faster keyword.