Giter Club home page Giter Club logo

hotpathallocationanalyzer's Introduction

Hot Path Allocation Analyzer

Roslyn Analyzer detecting heap allocation in hot path (based on https://github.com/microsoft/RoslynClrHeapAllocationAnalyzer)

Detect in hot path:

  • explicit allocations
  • implicit allocations
    • boxing
    • display classes a.k.a closures
    • implicit delegate creations
    • ...

Hot path

Hot path should be flagged as so using the NoAllocation attribute. This attribute indicates that the analyzer should run on the method.

It also forbids calling a method/property that is considered may allocate.

Methods/Properties are considered safe (i.e. they don't allocate) when flagged for analysis (NoAllocation), flagged to be ignored (IgnoreAllocation), whitelisted or in a safe scope. Properties are also considered safe when they are auto properties.

Safe scopes are defined as:

[NoAllocation]
public int Something(string str) 
{
    using var safeScope = new AllocationFreeScope();

    // this is safe because it's in an AllocationFreeScope
    return str.Length;
}

Setup

Install the nuget package HotPathAllocationAnalyzer on the project to analyze

Whitelisting

Whitelisting can be used to mark third party and system methods as safe.

  1. To create a whitelist, you need to create a folder called HotPathAllocationAnalyzer at the root of your solution.
  2. This folder should contain a csproj (name does not matter) and reference the nuget HotPathAllocationAnalyzer.Configuration
  3. You can then add some class in the project that implements the AllocationConfiguration class and define a method listing the whitelisted methods:
public class TestConfiguration : AllocationConfiguration
{
    public void WhitelistString(string str)
    {
        MakeSafe(() => str.Length);
        MakeSafe(() => str.IsNormalized());
        MakeSafe(() => str.Contains(default));
    }

    public void WhitelistNullable<T>(T? arg)
        where T: struct
    {
        MakeSafe(() => arg.Value); 
        MakeSafe(() => arg.HasValue); 
    }
    
    [MakeSafe]
    public void WhitelistSomeCalls()
    {
        Console.WriteLine("Hello");
    }
}

hotpathallocationanalyzer's People

Contributors

alexpaskhin avatar cezarypiatek avatar edespong avatar farnyser avatar gitter-badger avatar iambk avatar jarlob avatar jmarolf avatar ljw1004 avatar mattwarren avatar maxwe11 avatar mendelmonteiro avatar mjsabby avatar nnpcyvivl avatar oktal avatar paulomorgado avatar rehansaeed avatar stebet avatar youssef1313 avatar

Watchers

 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.