GithubHelp home page GithubHelp logo

localsinit.fody's Introduction

LocalsInit.Fody

Build NuGet package GitHub release License

This is an add-in for Fody which lets you control the value of the localsinit flag on methods.

This is most useful to eliminate the zero-initialization of memory returned by stackalloc.

This is now a compiler feature

Roslyn now lets you control this with SkipLocalsInitAttribute.

Prefer the compiler feature to this weaver whenever possible. See also the spec.

If you want to target frameworks prior to .NET 5, you can still use the compiler feature by defining the attribute in your own assembly:

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Module
                    | AttributeTargets.Class
                    | AttributeTargets.Struct
                    | AttributeTargets.Interface
                    | AttributeTargets.Constructor
                    | AttributeTargets.Method
                    | AttributeTargets.Property
                    | AttributeTargets.Event, Inherited = false)]
    internal sealed class SkipLocalsInitAttribute : Attribute
    {
    }
}

Installation

  • Install the NuGet packages Fody and LocalsInit.Fody. Installing Fody explicitly ensures the latest version is used:

    PM> Install-Package Fody
    PM> Install-Package LocalsInit.Fody
    
  • Add the PrivateAssets="all" metadata attribute to the <PackageReference /> items of Fody and LocalsInit.Fody in your project file, so they won't be listed as dependencies.

  • If you already have a FodyWeavers.xml file in the root directory of your project, add the <LocalsInit /> tag there. This file will be created on the first build if it doesn't exist:

    <?xml version="1.0" encoding="utf-8" ?>
    <Weavers>
      <LocalsInit />
    </Weavers>

See Fody usage for general guidelines, and Fody Configuration for additional options.

Usage

Add a [LocalsInit(false)] attribute to disable the localsinit flag at the desired level.

You can also add [LocalsInit(true)] attributes at lower levels to override the value set at a higher level.

In practice, you'll probably want to set the default to false at the assembly level. You can do so either as an assembly-level attribute:

[assembly: LocalsInit(false)]

Or you can set the default value in FodyWeavers.xml:

<LocalsInit Default="false" />

Then, look in your code for usages of the stackalloc keyword. In each case, if the code assumes the memory to be zero-initialized, add the [LocalsInit(true)] attribute to the method, or change the code to remove that assumption.

Configuration

The LocalsInit element in FodyWeavers.xml accepts the following attribute:

  • Default="true|false" to set the default value of the localsinit flag for the assembly. This is equivalent to setting an assembly-level attribute.

localsinit.fody's People

Contributors

ltrzesniewski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.