You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider emitting methods with SkipLocalsInitAttribute #595
We can completely control the methods we emit to ensure they are safe. Structs we emit are expected to be initialized by the users, which are accustomed to .NET behavior of clearing memory for a new struct, so I do not think we should apply this attribute on structs.
And I don't think we emit any classes (with fields, anyway).
This attribute is unsafe, because it may reveal uninitialized memory to the application in certain instances (for example, reading from uninitialized stack-allocated memory). If applied to a method directly, the attribute applies to that method and all its nested functions, including lambdas and local functions. If applied to a type or module, it applies to all methods nested inside. This attribute is intentionally not permitted on assemblies. To apply the attribute to multiple type declarations, use it at the module level instead.
Activity
elachlan commentedon Jul 26, 2022
There are a couple of scenarios to consider when outputting it.
https://www.meziantou.net/csharp-9-improve-performance-using-skiplocalsinit.htm
elachlan commentedon Aug 19, 2022
Do we only want to apply
SkipLocalsInitAttributeto methods and not classes/structs?AArnott commentedon Aug 19, 2022
We can completely control the methods we emit to ensure they are safe. Structs we emit are expected to be initialized by the users, which are accustomed to .NET behavior of clearing memory for a new struct, so I do not think we should apply this attribute on structs.
And I don't think we emit any classes (with fields, anyway).
jnm2 commentedon Aug 22, 2022
Is there any difference between placing
[SkipLocalsInit]on a type and placing it only on each of the type's methods and accessors (and nested types)?elachlan commentedon Aug 22, 2022
From the docs. I actually had no idea. It does apply to all methods. It doesn't say it applied to the fields on the module though.
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.skiplocalsinitattribute?view=net-6.0
jnm2 commentedon Aug 23, 2022
How could it apply to fields? It only affects the locals block of method bodies so that they are not locals-init blocks.
AArnott commentedon Aug 23, 2022
I think you're right. I am not too familiar with the attribute yet so I probably added more confusion than clarity with my last comment.
JeremyKuhne commentedon Sep 7, 2022
One other option here is to use
Unsafe.SkipInit()explicitly on structs that don't need initialization. That doesn't help forstackallocof course.