optimizing-msbuild-performance

Guides performance optimization of MSBuild evaluation and execution engine code.

1.2k|337|Updated Oct 13, 2022
One-click install
npx skills add https://github.com/dotnet/dotnet --skill optimizing-msbuild-performance
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-msbuild-performance
Source: https://github.com/dotnet/dotnet/tree/main/src/msbuild/.github/skills/optimizing-msbuild-performance
Command: npx skills add https://github.com/dotnet/dotnet --skill optimizing-msbuild-performance

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

MSBuild evaluates and builds thousands of projects in enterprise solutions, and inefficient code in hot paths like Expander.cs or Evaluator.cs causes GC pressure and slow builds. This Skill provides concrete rules for writing allocation-aware, high-performance C# code in the MSBuild engine.

Core Features & Use Cases

  • Allocation Awareness: Rules for avoiding LINQ, string allocations, and unnecessary enumerations on hot paths, with Span<T> and stackalloc guidance.
  • String Comparison Standards: Correct comparer selection for case-insensitive MSBuild names, file paths on Windows vs Linux, and dictionary keys.
  • Collection Type Selection: Decision tables for choosing arrays, ImmutableArray<T>, Dictionary, or linear scans based on access patterns.
  • Use Case: When reviewing a PR that modifies Evaluator.cs, apply the anti-pattern table to catch double dictionary lookups, uncompiled Regex in loops, or string.Format in filtered log messages before merge.

Quick Start

Ask the AI to review my changes to Expander.cs for performance regressions and allocation issues on hot paths.

Frequently Asked Questions about optimizing-msbuild-performance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize MSBuild evaluation performance?

Profile first using the /profileevaluation flag, BenchmarkDotNet, or ETW traces to find real bottlenecks. Then minimize import chain depth, avoid overly broad glob patterns, and order condition checks so cheap evaluations short-circuit first.

How to reduce memory allocations in C# hot paths?

Replace LINQ operators like Where and First with foreach loops to avoid enumerator and closure allocations. Use Span<T> and stackalloc for short-lived buffers, cache computed values outside loops, and prefer TryGetValue over ContainsKey plus indexer.

What string comparison should I use for MSBuild property names?

Use MSBuildNameIgnoreCaseComparer for property, item, and target names and as dictionary key comparers. Never use ToLower or ToUpper for comparisons since they allocate new strings, and never use CurrentCulture for MSBuild identifiers.

Should I use ImmutableList or ImmutableArray in C#?

ImmutableArray<T> is almost always the right choice for build-once, read-many collections because it offers O(1) indexed access. ImmutableList<T> has O(log n) access time and is rarely appropriate for performance-critical code.

Why is string.Format bad in logging hot paths?

string.Format allocates a new string even when the log message is filtered out by verbosity level. Use structured logging or guard the call with a verbosity check so allocation only happens when the message will actually be written.

When should I use AggressiveInlining in MSBuild code?

Apply MethodImplOptions.AggressiveInlining only to small methods called millions of times in extremely hot paths. Also consider avoiding virtual dispatch in inner loops and using struct enumerators to eliminate heap allocation.