Only generate the polyfills used by the code

Merged
ME
2mo ago
+42-1

What

MeziantouPolyfill_IncludedPolyfills was set to *, which Meziantou.Polyfill does not treat as a filter (IncludedMembers: <null> in Debug.g.cs), so ~800 polyfills (3.6 MB of generated source) were compiled into every assembly. It is now an explicit list of the 36 polyfills the code uses.

Assembly size (Release, netstandard2.0, packed in the NuGet package)

RoslynMeziantou.AnalyzerMeziantou.Analyzer.CodeFixers
4.82,778,112 → 1,860,6082,356,736 → 859,648
4.142,780,160 → 1,862,1442,357,248 → 860,160
5.02,780,160 → 1,862,1442,357,248 → 860,160
5.62,780,672 → 1,862,6562,358,272 → 861,184
5.92,787,328 → 1,869,3122,358,272 → 861,184

Total for the 10 packed DLLs: 25.7 MB → 13.6 MB (-47%). The net10.0 builds are also ~90 KB smaller each.

Verification

  • Beyond compiling, I checked that no call silently binds to a different overload (e.g. ReadOnlySpan<char>.Contains falling back to Enumerable.Contains). I disassembled the Meziantou.* types before and after, for every Roslyn version on netstandard2.0 and for net10.0 on Roslyn 5.9, and the IL is identical. The only difference is a [CompilerFeatureRequired("RefStructs")] marker no longer emitted on two ref structs, which only matters to pre-C# 7.2 compilers referencing the assembly.
  • dotnet build of the whole solution: 0 errors, 0 warnings.
  • Tests: Roslyn 5.9 (4,720 passed) and Roslyn 4.8 (4,599 passed). The other versions were built but their tests were not run locally.

Notes for reviewers

  • New code using a polyfill that is not in the list will fail to compile on netstandard2.0; add its XML doc ID to the list. Set MeziantouPolyfill_GenerateDebugFile=true to list the available IDs in Debug.g.cs.
  • Some polyfills use other polyfills without declaring them as dependencies, so they are listed explicitly (worth fixing in Meziantou.Polyfill):
    • T:System.Index / T:System.Range → DoesNotReturnAttribute
    • T:System.Runtime.CompilerServices.DefaultInterpolatedStringHandler → String.TryCopyTo(Span<char>), String.CopyTo(Span<char>)