Skip to content

Consider emitting methods with SkipLocalsInitAttribute #595

Open

Description

The LibraryImportAttribute source generator emits code that applies SkipLocalsInitAttribute to each method. Perhaps we should too for perf reasons.

Activity

  1. elachlan commented on Jul 26, 2022

    @elachlan
    Contributor
  2. elachlan commented on Aug 19, 2022

    @elachlan
    Contributor

    Do we only want to apply SkipLocalsInitAttribute to methods and not classes/structs?

  3. AArnott commented on Aug 19, 2022

    @AArnott
    MemberAuthor

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

  4. jnm2 commented on Aug 22, 2022

    @jnm2
    Contributor

    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)?

  5. elachlan commented on Aug 22, 2022

    @elachlan
    Contributor

    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

    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.

  6. jnm2 commented on Aug 23, 2022

    @jnm2
    Contributor

    How could it apply to fields? It only affects the locals block of method bodies so that they are not locals-init blocks.

  7. AArnott commented on Aug 23, 2022

    @AArnott
    MemberAuthor

    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.

  8. JeremyKuhne commented on Sep 7, 2022

    @JeremyKuhne
    Member

    One other option here is to use Unsafe.SkipInit() explicitly on structs that don't need initialization. That doesn't help for stackalloc of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions