Skip to content

Commit c9e8778

Browse files
committed
Update AGENTS.md for Geocoding.net
Replace Exceptionless-specific content with accurate Geocoding.net project details including repository overview, project structure, coding standards, testing guidance, and available skills/agents.
1 parent ace8964 commit c9e8778

40 files changed

Lines changed: 6685 additions & 5 deletions
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
---
2+
name: analyzing-dotnet-performance
3+
description: >-
4+
Scans .NET code for ~50 performance anti-patterns across async, memory,
5+
strings, collections, LINQ, regex, serialization, and I/O with tiered
6+
severity classification. Use when analyzing .NET code for optimization
7+
opportunities, reviewing hot paths, or auditing allocation-heavy patterns.
8+
---
9+
10+
# .NET Performance Patterns
11+
12+
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.
13+
14+
## When to Use
15+
16+
- Reviewing C#/.NET code for performance optimization opportunities
17+
- Auditing hot paths for allocation-heavy or inefficient patterns
18+
- Systematic scan of a codebase for known anti-patterns before release
19+
- Second-opinion analysis after manual performance review
20+
21+
## When Not to Use
22+
23+
- **Algorithmic complexity analysis** — this skill targets API usage patterns, not algorithm design
24+
- **Code not on a hot path** with no performance requirements — avoid premature optimization
25+
26+
## Inputs
27+
28+
| Input | Required | Description |
29+
|-------|----------|-------------|
30+
| Source code | Yes | C# files, code blocks, or repository paths to scan |
31+
| Hot-path context | Recommended | Which code paths are performance-critical |
32+
| Target framework | Recommended | .NET version (some patterns require .NET 8+) |
33+
| Scan depth | Optional | `critical-only`, `standard` (default), or `comprehensive` |
34+
35+
## Workflow
36+
37+
### Step 1: Load Reference Files (if available)
38+
39+
Try to load `references/critical-patterns.md` and the topic-specific reference files listed below. These contain detailed detection recipes and grep commands.
40+
41+
**If reference files are not found** (e.g., in a sandboxed environment or when the skill is embedded as instructions only), **skip file loading and proceed directly to Step 3** using the scan recipes listed inline below. Do not spend time searching the filesystem for reference files — if they aren't at the expected relative path, they aren't available.
42+
43+
### Step 2: Detect Code Signals and Select Topic Recipes
44+
45+
Scan the code for signals that indicate which pattern categories to check. If reference files were loaded, use their `## Detection` sections. Otherwise, use the inline recipes in Step 3.
46+
47+
| Signal in Code | Topic |
48+
|----------------|-------|
49+
| `async`, `await`, `Task`, `ValueTask` | Async patterns |
50+
| `Span<`, `Memory<`, `stackalloc`, `ArrayPool`, `string.Substring`, `.Replace(`, `.ToLower()`, `+=` in loops, `params ` | Memory & strings |
51+
| `Regex`, `[GeneratedRegex]`, `Regex.Match`, `RegexOptions.Compiled` | Regex patterns |
52+
| `Dictionary<`, `List<`, `.ToList()`, `.Where(`, `.Select(`, LINQ methods, `static readonly Dictionary<` | Collections & LINQ |
53+
| `JsonSerializer`, `HttpClient`, `Stream`, `FileStream` | I/O & serialization |
54+
55+
Always check structural patterns (unsealed classes) regardless of signals.
56+
57+
**Scan depth controls scope:**
58+
- `critical-only`: Only critical patterns (deadlocks, >10x regressions)
59+
- `standard` (default): Critical + detected topic patterns
60+
- `comprehensive`: All pattern categories
61+
62+
### Step 3: Scan and Report
63+
64+
**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.
65+
66+
For each relevant pattern category, run the detection recipes below. Report exact counts, not estimates.
67+
68+
**Core scan recipes** (run these when reference files aren't available):
69+
```
70+
# Strings & memory
71+
grep -n '\.IndexOf(\"' FILE # Missing StringComparison
72+
grep -n '\.Substring(' FILE # Substring allocations
73+
grep -En '\.(StartsWith|EndsWith|Contains)\s*\(' FILE # Missing StringComparison
74+
grep -n '\.ToLower()\|\.ToUpper()' FILE # Culture-sensitive + allocation
75+
grep -n '\.Replace(' FILE # Chained Replace allocations
76+
grep -n 'params ' FILE # params array allocation
77+
78+
# Collections & LINQ
79+
grep -n '\.Select\|\.Where\|\.OrderBy\|\.GroupBy' FILE # LINQ on hot path
80+
grep -n '\.All\|\.Any' FILE # LINQ on string/char
81+
grep -n 'new Dictionary<\|new List<' FILE # Per-call allocation
82+
grep -n 'static readonly Dictionary<' FILE # FrozenDictionary candidate
83+
84+
# Regex
85+
grep -n 'RegexOptions.Compiled' FILE # Compiled regex budget
86+
grep -n 'new Regex(' FILE # Per-call regex
87+
grep -n 'GeneratedRegex' FILE # Positive: source-gen regex
88+
89+
# Structural
90+
grep -n 'public class \|internal class ' FILE # Unsealed classes
91+
grep -n 'sealed class' FILE # Already sealed
92+
grep -n ': IEquatable' FILE # Positive: struct equality
93+
```
94+
95+
**Rules:**
96+
- Run every relevant recipe for the detected pattern categories
97+
- **Emit a scan execution checklist** before classifying findings — list each recipe and the hit count
98+
- A result of **0 hits** is valid and valuable (confirms good practice)
99+
- If reference files were loaded, also run their `## Detection` recipes
100+
101+
**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.
102+
103+
### Step 3b: Cross-File Consistency Check
104+
105+
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.
106+
107+
### Step 3c: Compound Allocation Check
108+
109+
After running scan recipes, look for these multi-allocation patterns that single-line recipes miss:
110+
111+
1. **Branched `.Replace()` chains:** Methods that call `.Replace()` across multiple `if/else` branches — report total allocation count across all branches, not just per-line.
112+
2. **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.
113+
3. **Compound `+=` with embedded allocating calls:** Lines like `result += $"...{Foo().ToLower()}"` are 2+ allocations (interpolation + ToLower + concatenation) — flag the compound cost, not just the `.ToLower()`.
114+
4. **`string.Format` specificity:** Distinguish resource-loaded format strings (not fixable) from compile-time literal format strings (fixable with interpolation). Enumerate the actionable sites.
115+
116+
### Step 4: Classify and Prioritize Findings
117+
118+
Assign each finding a severity:
119+
120+
| Severity | Criteria | Action |
121+
|----------|----------|--------|
122+
| 🔴 **Critical** | Deadlocks, crashes, security vulnerabilities, >10x regression | Must fix |
123+
| 🟡 **Moderate** | 2-10x improvement opportunity, best practice for hot paths | Should fix on hot paths |
124+
| ℹ️ **Info** | Pattern applies but code may not be on a hot path | Consider if profiling shows impact |
125+
126+
**Prioritization rules:**
127+
1. If the user identified hot-path code, elevate all findings in that code to their maximum severity
128+
2. 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"_
129+
3. Never suggest micro-optimizations on code that is clearly not performance-sensitive
130+
131+
**Scale-based severity escalation:**
132+
When the same pattern appears across many instances, escalate severity:
133+
- 1-10 instances of the same anti-pattern → report at the pattern's base severity
134+
- 11-50 instances → escalate ℹ️ Info patterns to 🟡 Moderate
135+
- 50+ instances → escalate to 🟡 Moderate with elevated priority; flag as a codebase-wide systematic issue
136+
137+
Always report exact counts (from scan recipes), not estimates or agent summaries.
138+
139+
### Step 5: Generate Findings
140+
141+
**Keep findings compact.** Each finding is one short block — not an essay. Group by severity (🔴 → 🟡 → ℹ️), not by file.
142+
143+
Format per finding:
144+
145+
```
146+
#### ID. Title (N instances)
147+
**Impact:** one-line impact statement
148+
**Files:** file1.cs:L1, file2.cs:L2, ... (list locations, don't build tables)
149+
**Fix:** one-line description of the change (e.g., "Add `StringComparison.Ordinal` parameter")
150+
**Caveat:** only if non-obvious (version requirement, correctness risk)
151+
```
152+
153+
**Rules for compact output:**
154+
- **No ❌/✅ code blocks** for trivial fixes (adding a keyword, parameter, or type change). A one-line fix description suffices.
155+
- **Only include code blocks** for non-obvious transformations (e.g., replacing a LINQ chain with a foreach loop, or hoisting a closure).
156+
- **File locations as inline comma-separated list**, not a table. Use `File.cs:L42` format.
157+
- **No explanatory prose** beyond the Impact line — the severity icon already conveys urgency.
158+
- **Merge related findings** that share the same fix (e.g., all `.ToLower()` calls go in one finding, not split by file).
159+
- **Positive findings** in a bullet list, not a table. One line per pattern: `✅ Pattern — evidence`.
160+
161+
End with a summary table and disclaimer:
162+
163+
```markdown
164+
| Severity | Count | Top Issue |
165+
|----------|-------|-----------|
166+
| 🔴 Critical | N | ... |
167+
| 🟡 Moderate | N | ... |
168+
| ℹ️ Info | N | ... |
169+
170+
> ⚠️ **Disclaimer:** These results are generated by an AI assistant and are non-deterministic. Findings may include false positives, miss real issues, or suggest changes that are incorrect for your specific context. Always verify recommendations with benchmarks and human review before applying changes to production code.
171+
```
172+
173+
## Validation
174+
175+
Before delivering results, verify:
176+
177+
- [ ] All critical patterns were checked (from reference files or inline recipes)
178+
- [ ] Topic-specific recipes run only when matching signals detected
179+
- [ ] Each finding includes a concrete code fix
180+
- [ ] Scan execution checklist is complete (all recipes run)
181+
- [ ] Summary table included at end
182+
183+
## Common Pitfalls
184+
185+
| Pitfall | Correct Approach |
186+
|---------|-----------------|
187+
| Flagging every `Dictionary` as needing `FrozenDictionary` | Only flag if the dictionary is never mutated after construction |
188+
| Suggesting `Span<T>` in async methods | Use `Memory<T>` in async code; `Span<T>` only in sync hot paths |
189+
| Reporting LINQ outside hot paths | Only flag LINQ in identified hot paths or tight loops; LINQ is acceptable in code that runs infrequently. Since .NET 7, LINQ Min/Max/Sum/Average are vectorized — blanket bans on LINQ are misguided |
190+
| Suggesting `ConfigureAwait(false)` in app code | Only applicable in library code; not primarily a performance concern |
191+
| Recommending `ValueTask` everywhere | Only for hot paths with frequent synchronous completion |
192+
| Flagging `new HttpClient()` in DI services | Check if `IHttpClientFactory` is already in use |
193+
| Suggesting `[GeneratedRegex]` for dynamic patterns | Only flag when the pattern string is a compile-time literal |
194+
| Suggesting `CollectionsMarshal.AsSpan` broadly | Only for ultra-hot paths with benchmarked evidence; adds complexity and fragility |
195+
| Suggesting `unsafe` code for micro-optimizations | Avoid `unsafe` except where absolutely necessary — do not recommend it for micro-optimizations that don't matter. Safe alternatives like `Span<T>`, `stackalloc` in safe context, and `ArrayPool` cover the vast majority of performance needs |
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Async & Concurrency Patterns
2+
3+
### Don't Expose Async Wrappers for Sync Methods
4+
🟡 **AVOID** wrapping sync methods with `Task.Run` in libraries | .NET Core+
5+
6+
7+
```csharp
8+
public Task<int> ComputeHashAsync(byte[] data) =>
9+
Task.Run(() => ComputeHash(data));
10+
```
11+
12+
```csharp
13+
public int ComputeHash(byte[] data) { /* CPU-bound work */ }
14+
// Consumer decides: var hash = await Task.Run(() => lib.ComputeHash(data));
15+
```
16+
17+
**Impact: Eliminates unnecessary thread pool queue/dequeue overhead per call.**
18+
19+
### Don't Expose Sync Wrappers for Async Methods
20+
🟡 **AVOID** creating sync wrappers that block on async implementations | .NET Core+
21+
22+
23+
```csharp
24+
public string GetData() => GetDataAsync().Result;
25+
```
26+
27+
```csharp
28+
public async Task<string> GetDataAsync() { /* ... */ }
29+
```
30+
31+
**Impact: Prevents deadlocks and thread pool starvation from hidden sync-over-async blocking.**
32+
33+
### Use ValueTask for Hot Paths with Frequent Sync Completion
34+
🟡 **DO** use `ValueTask<T>` on hot paths where sync completion is common | .NET Core 2.1+
35+
36+
37+
```csharp
38+
public async Task<int> ReadAsync(Memory<byte> buffer)
39+
{
40+
if (_bufferedCount > 0)
41+
return ReadFromBuffer(buffer.Span);
42+
return await ReadAsyncCore(buffer);
43+
}
44+
```
45+
46+
```csharp
47+
public ValueTask<int> ReadAsync(Memory<byte> buffer)
48+
{
49+
if (_bufferedCount > 0)
50+
return new ValueTask<int>(ReadFromBuffer(buffer.Span));
51+
return new ValueTask<int>(ReadAsyncCore(buffer));
52+
}
53+
```
54+
55+
**Impact: Eliminates Task\<T\> allocation on synchronous completion — the struct stores results inline.**
56+
57+
### Use Channels for Producer/Consumer
58+
🟡 **DO** use `System.Threading.Channels` for producer-consumer patterns | .NET Core 3.0+
59+
60+
61+
```csharp
62+
var queue = new BlockingCollection<WorkItem>();
63+
var item = queue.Take();
64+
```
65+
66+
```csharp
67+
var channel = Channel.CreateUnbounded<WorkItem>();
68+
69+
// Producer
70+
await channel.Writer.WriteAsync(item);
71+
72+
// Consumer
73+
await foreach (var item in channel.Reader.ReadAllAsync())
74+
Process(item);
75+
```
76+
77+
**Impact: ~25% faster, ~95% fewer GC collections vs manual approaches.**
78+
79+
### Avoid False Sharing with Thread-Local State
80+
🟡 **AVOID** adjacent mutable fields written by different threads | .NET 7+
81+
82+
83+
```csharp
84+
class SharedCounters
85+
{
86+
public long Counter1;
87+
public long Counter2;
88+
}
89+
```
90+
91+
```csharp
92+
[StructLayout(LayoutKind.Explicit, Size = 128)]
93+
struct PaddedCounter
94+
{
95+
[FieldOffset(0)] public long Value;
96+
}
97+
```
98+
99+
**Impact: Eliminates cross-core cache invalidation — can improve multi-threaded throughput by 10x+.**
100+
101+
## Detection
102+
103+
Scan recipes for async anti-patterns. Run these and report exact counts.
104+
105+
```bash
106+
# async void methods (correctness issue — crashes on exception)
107+
grep -rn --include='*.cs' 'async void' --exclude-dir=bin --exclude-dir=obj . | grep -v 'event' | wc -l
108+
```
109+
110+
### Patterns Requiring Manual Review
111+
112+
- **Sync-over-async** (`.Result`, `.Wait()`): `.Result` matches any property named Result — needs type context to confirm it's `Task.Result`

0 commit comments

Comments
 (0)