{"id":13415003,"url":"https://github.com/antiufo/roslyn-linq-rewrite","last_synced_at":"2026-01-16T10:07:46.533Z","repository":{"id":62925378,"uuid":"67799709","full_name":"antiufo/roslyn-linq-rewrite","owner":"antiufo","description":"Compiles C# code by first rewriting the syntax trees of LINQ expressions using plain procedural code, minimizing allocations and dynamic dispatch.","archived":false,"fork":false,"pushed_at":"2020-02-02T08:47:28.000Z","size":332,"stargazers_count":717,"open_issues_count":18,"forks_count":31,"subscribers_count":41,"default_branch":"master","last_synced_at":"2024-07-31T21:53:21.429Z","etag":null,"topics":[],"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/antiufo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-09T13:07:03.000Z","updated_at":"2024-07-05T17:52:58.000Z","dependencies_parsed_at":"2022-11-09T08:00:31.037Z","dependency_job_id":null,"html_url":"https://github.com/antiufo/roslyn-linq-rewrite","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antiufo%2Froslyn-linq-rewrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antiufo%2Froslyn-linq-rewrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antiufo%2Froslyn-linq-rewrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antiufo%2Froslyn-linq-rewrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antiufo","download_url":"https://codeload.github.com/antiufo/roslyn-linq-rewrite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221513942,"owners_count":16835746,"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":[],"created_at":"2024-07-30T21:00:41.335Z","updated_at":"2026-01-16T10:07:46.503Z","avatar_url":"https://github.com/antiufo.png","language":"C#","funding_links":[],"categories":["Compilers, Transpilers and Languages","编译器、透明机和语言","Open Source Projects"],"sub_categories":[],"readme":"# roslyn-linq-rewrite\nThis tool compiles C# code by first rewriting the syntax trees of LINQ expressions using plain procedural code, minimizing allocations and dynamic dispatch.\n\n## Example input code\n```csharp\npublic int Method1()\n{\n    var arr = new[] { 1, 2, 3, 4 };\n    var q = 2;\n    return arr.Where(x =\u003e x \u003e q).Select(x =\u003e x + 3).Sum();\n}\n```\n**Allocations**: input array, array enumerator, closure for `q`, `Where` delegate, `Select` delegate, `Where` enumerator, `Select` enumerator. \n## Decompiled output code\n```csharp\npublic int Method1()\n{\n    int[] arr = new[] { 1, 2, 3, 4 };\n    int q = 2;\n    return this.Method1_ProceduralLinq1(arr, q);\n}\nprivate int Method1_ProceduralLinq1(int[] _linqitems, int q)\n{\n    if (_linqitems == null) throw new ArgumentNullException();\n\n    int num = 0;\n    for (int i = 0; i \u003c _linqitems.Length; i++)\n    {\n        int num2 = _linqitems[i]; \n        if (num2 \u003e q)\n            num += num2 + 3;\n    }\n    return num;\n}\n```\n**Allocations**: input array.\n\n*Note: in this example, the optimizing compiler has generated code using arrays, because the type was known at compile time. When this is not the case, the generated code will instead use a `foreach` and `IEnumerable\u003c\u003e`.*\n*Non-optimizable call chains and `IQueryable\u003c\u003e` are left intact.*\n## Supported LINQ methods\n* `Select`, `Where`, `Reverse`, `Cast`, `OfType`\n* `First`, `FirstOrDefault`, `Single`, `SingleOrDefault`, `Last`, `LastOrDefault`\n* `ToList`, `ToArray`, `ToDictionary`\n* `Count`, `LongCount`, `Any`, `All`\n* `ElementAt`, `ElementAtOrDefault`\n* `Contains`, `ForEach`\n\n## Usage (.sln/.csproj)\n* Download the [latest binaries](https://github.com/antiufo/roslyn-linq-rewrite/releases)\n* `roslyn-linq-rewrite \u003cpath-to-sln-or-csproj\u003e [--release]`\n\nNote: you can more deeply integrate the optimizing compiler with MSBuild by setting `CscToolPath` to the roslyn-linq-rewrite directory.\n\n## Usage (project.json)\n* Add the following to your `project.json`:\n```json\n    \"tools\": {\n        \"dotnet-compile-csc-linq-rewrite\": {\n          \"version\": \"1.0.1.9\",\n          \"imports\": \"portable-net45+win8+wp8+wpa81\"\n        }\n    }\n```\n* In the `buildOptions` of your `project.json`, specify the custom compiler:\n```json\n    \"buildOptions\": {\n        \"compilerName\": \"csc-linq-rewrite\"\n    }\n```\n* Compile your project with `dotnet restore` and `dotnet build`.\n* If you need to exclude a specific method, apply a `[NoLinqRewrite]` attribute to that method:\n```csharp\nnamespace Shaman.Runtime\n{\n    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method)]\n    public class NoLinqRewriteAttribute : Attribute\n    {\n    }\n}\n```\n\n## Tests\nHere's a [diff](https://github.com/antiufo/linqtests/blob/master/tests/Shaman.Roslyn.LinqRewrite.Tests/Results_diff.diff) (and its [code](https://github.com/antiufo/linqtests/blob/master/tests/Shaman.Roslyn.LinqRewrite.Tests/)) between the ordinary LINQ tests and the version compiled with roslyn-linq-rewrite. The only difference seems to be related about a `Min`/`Max` with `NaN` values, I'm planning to fix it soon.\n\n## Shaman.FastLinq\nTo further reduce allocations, install `Shaman.FastLinq` or `Shaman.FastLinq.Sources`. These packages include LINQ methods specific for `T[]` and `List\u003c\u003e` (not all method calls are optimized by LINQ rewrite, like individual, non-chained `.First()` or `.Last()` calls).\n\n## Comparison to LinqOptimizer\n* Code is optimized at build time (as opposed to run time)\n* Uses existing LINQ syntax, no need for `AsQueryExpr().Run()`\n* No allocations for `Expression\u003c\u003e` trees and enumerator boxing\n* Parallel LINQ is not supported (i.e. left intact)\n* No support for F#\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantiufo%2Froslyn-linq-rewrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantiufo%2Froslyn-linq-rewrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantiufo%2Froslyn-linq-rewrite/lists"}