dotnet / dotnet/wpf

Replace use of `Marshal.SizeOf` with `sizeof` for blittable structs

Open
#3,218 5 comments 4 reactions 0 assignees View on GitHub
Enhancement Requested
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

https://github.com/dotnet/pinvoke/pull/463#discussion_r448457195

>`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.

There are many uses of `Marshal.SizeOf` in the codebase - many of them likely for blittable types - that could be identified and updated to use faster `sizeof`.

/cc @dotnet/dotnet-winforms - This probably applies to Winforms as well.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.