{"id":15409717,"url":"https://github.com/whiteblackgoose/codegenanalysis","last_synced_at":"2025-07-06T14:03:40.006Z","repository":{"id":45574088,"uuid":"428658899","full_name":"WhiteBlackGoose/CodegenAnalysis","owner":"WhiteBlackGoose","description":"Library for obtaining and analyzing the codegen of JIT for .NET. Can be used for both tests (assertions) and making reports (benchmarks).","archived":false,"fork":false,"pushed_at":"2022-12-04T19:29:10.000Z","size":258,"stargazers_count":24,"open_issues_count":8,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-07-06T14:03:13.567Z","etag":null,"topics":["codegen","dotnet","jit","performance","performance-analysis"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WhiteBlackGoose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-16T13:07:18.000Z","updated_at":"2025-01-01T17:22:16.000Z","dependencies_parsed_at":"2023-01-23T06:30:55.903Z","dependency_job_id":null,"html_url":"https://github.com/WhiteBlackGoose/CodegenAnalysis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/WhiteBlackGoose/CodegenAnalysis","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WhiteBlackGoose%2FCodegenAnalysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WhiteBlackGoose%2FCodegenAnalysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WhiteBlackGoose%2FCodegenAnalysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WhiteBlackGoose%2FCodegenAnalysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WhiteBlackGoose","download_url":"https://codeload.github.com/WhiteBlackGoose/CodegenAnalysis/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WhiteBlackGoose%2FCodegenAnalysis/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263914030,"owners_count":23529074,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","dotnet","jit","performance","performance-analysis"],"created_at":"2024-10-01T16:41:07.323Z","updated_at":"2025-07-06T14:03:39.983Z","avatar_url":"https://github.com/WhiteBlackGoose.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CodegenAnalysis\n\n![](https://img.shields.io/static/v1?label=Lowest+target\u0026message=netstandard2.0\u0026color=purple\u0026logo=dotnet)\n\n![](https://img.shields.io/static/v1?label=Windows\u0026message=Supported\u0026color=brightgreen\u0026logo=windows)\n![](https://img.shields.io/static/v1?label=Linux\u0026message=Supported\u0026color=brightgreen\u0026logo=linux)\n![](https://img.shields.io/static/v1?label=MacOS\u0026message=Supported\u0026color=brightgreen\u0026logo=apple)\n\n\u003cimg align=\"right\" src=\"./logo1t.png\" width=200\u003e\n\nLibrary for analyzing the machine code generated by JIT (codegen). Has API for obtaining the codegen, verifying the characteristics for tests (in a similar manner to xUnit), and generating reports as benchmarks (in a similar manner to BenchmarkDotNet). Supports x86_64 on three major platforms (Windows, MacOS, Linux).\n\n\n## Why should I use it?\n\nFirst of all, most .NET developers don't need this library. If you want to measure performance of your code, use [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet).\n\nIf you're working on low-level, often HW-related project, where it's crucial if a branch or call or etc. is optimized or not, this library is for you.\n\n### Table of contents\n- [CodegenAnalysis](#CodegenAnalysis)\n- [CodegenAnalysis.Assertions](#CodegenAnalysisAssertions)\n- [CodegenAnalysis.Benchmarks](#CodegenAnalysisBenchmarks)\n\n## CodegenAnalysis\n\n[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/CodegenAnalysis?label=NuGet\u0026logo=nuget)](https://www.nuget.org/packages/CodegenAnalysis)\n\n```cs\nstatic int AddAndMul(int a, int b) =\u003e a + b * a;\n\n...\n\nvar ci = CodegenInfo.Obrain(() =\u003e AddAndMul(3, 5));\nConsole.WriteLine(ci);\n```\nOutput:\n```assembly\n00007FFD752E42F0 8BC2                 mov       eax,edx\n00007FFD752E42F2 0FAFC1               imul      eax,ecx\n00007FFD752E42F5 03C1                 add       eax,ecx\n00007FFD752E42F7 C3                   ret\n```\n\n## CodegenAnalysis.Assertions\n\n[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/CodegenAnalysis.Assertions?label=NuGet\u0026logo=nuget)](https://www.nuget.org/packages/CodegenAnalysis.Assertions)\n\n### Verifying the size of the codegen\n\n```cs\nusing CodegenAssertions;\nusing Xunit;\n\npublic class CodegenSizeTest\n{\n    public static int SomeMethod(int a, int b)\n        =\u003e a + b;\n\n    [Fact]\n    public void Test1()\n    {\n        CodegenInfo.Obtain(() =\u003e SomeMethod(4, 5), CompilationTier.Tier1)\n            .ShouldBeNotLargerThan(20);\n    }\n}\n```\n\n\n### Having calls in the codegen\n\n```cs\npublic class Tests\n{\n    public class A\n    {\n        public virtual int H =\u003e 3;\n    }\n\n    public sealed class B : A\n    {\n        public override int H =\u003e 6;\n    }\n\n    // this will get devirtualized at tier1, but not at tier0\n    static int Twice(B b) =\u003e b.H * 2;\n\n    [Fact]\n    public void NotDevirtTier0()\n    {\n        CodegenInfo.Obtain(() =\u003e Twice(new B()), CompilationTier.Default)\n            .ShouldHaveCalls(c =\u003e c \u003e= 1);\n    }\n\n    [Fact]\n    public void DevirtTier1()\n    {\n        CodegenInfo.Obtain(() =\u003e Twice(new B()), CompilationTier.Tier1)\n            .ShouldHaveCalls(0);\n    }\n}\n```\n\n### Testing if we have branches\n\n```cs\n    private static readonly bool True = true;\n\n    static int SmartThing()\n    {\n        if (True)\n            return 5;\n        return 10;\n    }\n\n    [Fact]\n    public void BranchElimination()\n    {\n        CodegenInfo.Obtain(() =\u003e SmartThing())\n            .ShouldHaveBranches(0);\n    }\n\n    [MethodImpl(MethodImplOptions.NoOptimization)]\n    static int StupidThing()\n    {\n        if (True)\n            return 5;\n        return 10;\n    }\n\n    [Fact]\n    public void NoBranchElimination()\n    {\n        CodegenInfo.Obtain(() =\u003e StupidThing(), CompilationTier.Default)\n            .ShouldHaveBranches(b =\u003e b \u003e 0);\n    }\n```\n\n## CodegenAnalysis.Benchmarks\n\n[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/CodegenAnalysis.Benchmarks?label=NuGet\u0026logo=nuget)](https://www.nuget.org/packages/CodegenAnalysis.Benchmarks)\n\n```cs\nCodegenBenchmarkRunner.Run\u003cA\u003e();\n\n[CAJob(Tier = CompilationTier.Default),\n CAJob(Tier = CompilationTier.Tier1)]\n\n[CAColumn(CAColumn.Branches),\n CAColumn(CAColumn.Calls), \n CAColumn(CAColumn.CodegenSize), \n CAColumn(CAColumn.StaticStackAllocations)]\n\n[CAExport(Export.Html),\n CAExport(Export.Md)]\npublic class A\n{\n    [CAAnalyze(3.5f)]\n    [CAAnalyze(13.5f)]\n    public static float Heavy(float a)\n    {\n        var b = Do1(a);\n        var c = Do1(b);\n        if (a \u003e 10)\n            c += Aaa(a);\n        return c + b;\n    }\n\n    [CAAnalyze(6f)]\n    public static float Square(float a)\n    {\n        return a * a;\n    }\n\n    [MethodImpl(MethodImplOptions.NoInlining)]\n    public static float Do1(float a)\n    {\n        return a * 2;\n    }\n\n    [MethodImpl(MethodImplOptions.NoInlining)]\n    public static float Aaa(float h)\n    {\n        return h * h * h;\n    }\n}\n```\n\n[See the output](./Samples/CodegenAnalysis.Benchmarks.Sample/CodegenAnalysis.Artifacts).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhiteblackgoose%2Fcodegenanalysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhiteblackgoose%2Fcodegenanalysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhiteblackgoose%2Fcodegenanalysis/lists"}