.NET Performance Patterns
Scan C#/.NET code for performance anti-patterns and produce prioritized findings with concrete fixes. Patterns sourced from the official .NET performance blog series, distilled to customer-actionable guidance.
When to Use
- Reviewing C#/.NET code for performance optimization opportunities
- Auditing hot paths for allocation-heavy or inefficient patterns
- Systematic scan of a codebase for known anti-patterns before release
- Second-opinion analysis after manual performance review
When Not to Use
- Algorithmic complexity analysis — this skill targets API usage patterns, not algorithm design
- Code not on a hot path with no performance requirements — avoid premature optimization
Inputs
Workflow
Step 1: Load Critical Reference
Resolve bundled paths from the directory that contains this SKILL.md, not from the user's workspace. Load this reference file first:
references/critical-patterns.md
If a direct read fails, list this skill's references/ directory once and retry only when the listing shows the expected file. Do not use workspace file or text search to locate the skill installation.
Step 2: Detect Code Signals and Select Topic Recipes
Scan the code for signals that indicate which pattern categories to check. Use the ## Detection section from the critical reference when available and the inline recipes in Step 3 for initial signal detection.
After detecting signals, load only the topic-specific references selected by scan depth:
critical-only: No additional references (use onlycritical-patterns.md)standard(default): Load references matching detected signals from this list:references/async-patterns.md— async/Task/ValueTask signalsreferences/memory-and-strings.md— Span/Memory/string allocation signalsreferences/regex-patterns.md— Regex signalsreferences/collections-and-linq.md— Dictionary/List/LINQ signalsreferences/io-and-serialization.md— JsonSerializer/HttpClient/Stream signalsreferences/structural-patterns.md— always loaded (unsealed classes checked regardless)
comprehensive: Load all six topic-specific references above
For coverage reporting, the selected references are references/critical-patterns.md plus only the topic-specific references selected above. If any selected reference remains unavailable after retry, use the inline recipes in Step 3 for the missing coverage. Include Reference coverage: reduced; unavailable: <paths>; used inline recipes for missing references. in the final report, with <paths> replaced by the missing relative paths of the selected references only.
Use the ## Detection sections from loaded reference files and the inline recipes in Step 3 for categories whose reference files are unavailable.
Always check structural patterns (unsealed classes) regardless of signals.
Scan depth controls scope:
critical-only: Only critical patterns (deadlocks, >10x regressions)standard(default): Critical + detected topic patternscomprehensive: All pattern categories
Step 3: Scan and Report
For files under 500 lines, read the entire file first — you'll spot most patterns faster than running individual grep recipes. Use grep to confirm counts and catch patterns you might miss visually.
For each relevant pattern category, run the detection recipes below. Report exact counts, not estimates.
Core scan recipes (run these when reference files aren't available):
Rules:
- Run every relevant recipe for the detected pattern categories
- Emit a scan execution checklist before classifying findings — list each recipe and the hit count
- A result of 0 hits is valid and valuable (confirms good practice)
- If reference files were loaded, also run their
## Detectionrecipes
Verify-the-Inverse Rule: For absence patterns, always count both sides and report the ratio (e.g., "N of M classes are sealed"). The ratio determines severity — 0/185 is systematic, 12/15 is a consistency fix.
Step 3b: Cross-File Consistency Check
If an optimized pattern is found in one file, check whether sibling files (same directory, same interface, same base class) use the un-optimized equivalent. Flag as 🟡 Moderate with the optimized file as evidence.
Step 3c: Compound Allocation Check
After running scan recipes, look for these multi-allocation patterns that single-line recipes miss:
- Branched
.Replace()chains: Methods that call.Replace()across multipleif/elsebranches — report total allocation count across all branches, not just per-line. - Cross-method chaining: When a public method delegates to another method that itself allocates intermediates (e.g., A calls B which does 3 regex replaces, then A calls C), report the total chain cost as one finding.
- Compound
+=with embedded allocating calls: Lines likeresult += $"...{Foo().ToLower()}"are 2+ allocations (interpolation + ToLower + concatenation) — flag the compound cost, not just the.ToLower(). string.Formatspecificity: Distinguish resource-loaded format strings (not fixable) from compile-time literal format strings (fixable with interpolation). Enumerate the actionable sites.
Step 4: Classify and Prioritize Findings
Assign each finding a severity:
Prioritization rules:
- If the user identified hot-path code, elevate all findings in that code to their maximum severity
- If hot-path context is unknown, report 🔴 Critical findings unconditionally; report 🟡 Moderate findings with a note: "Impactful if this code is on a hot path"
- Never suggest micro-optimizations on code that is clearly not performance-sensitive
Scale-based severity escalation: When the same pattern appears across many instances, escalate severity:
- 1-10 instances of the same anti-pattern → report at the pattern's base severity
- 11-50 instances → escalate ℹ️ Info patterns to 🟡 Moderate
- 50+ instances → escalate to 🟡 Moderate with elevated priority; flag as a codebase-wide systematic issue
Always report exact counts (from scan recipes), not estimates or agent summaries.
Step 5: Generate Findings
Keep findings compact. Each finding is one short block — not an essay. Group by severity (🔴 → 🟡 → ℹ️), not by file.
Format per finding:
Rules for compact output:
- No ❌/✅ code blocks for trivial fixes (adding a keyword, parameter, or type change). A one-line fix description suffices.
- Only include code blocks for non-obvious transformations (e.g., replacing a LINQ chain with a foreach loop, or hoisting a closure).
- File locations as inline comma-separated list, not a table. Use
File.cs:L42format. - No explanatory prose beyond the Impact line — the severity icon already conveys urgency.
- Merge related findings that share the same fix (e.g., all
.ToLower()calls go in one finding, not split by file). - Positive findings in a bullet list, not a table. One line per pattern:
✅ Pattern — evidence.
End with a summary table and disclaimer:
Validation
Before delivering results, verify:
- All critical patterns were checked (from reference files or inline recipes)
- Topic-specific recipes run only when matching signals detected
- Each finding includes a concrete code fix
- Scan execution checklist is complete (all recipes run)
- Summary table included at end
