dotnet-techne-csharp-type-design-performance

Solid

Use when designing types and collections for hot paths and low-allocation .NET code. Keywords: readonly struct, sealed class, ValueTask, Span, FrozenDictionary, FrozenSet, allocation optimisation.

Web & Frontend 12 stars 1 forks Updated 2 days ago MIT

Install

View on GitHub

Quality Score: 85/100

Stars 20%
37
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# Type Design for Performance ## When to Use This Skill Use this skill when: - Designing new types and APIs - Reviewing code for performance issues - Choosing between class, struct, and record - Working with collections and enumerables --- ## Core Principles 1. **Seal your types** - Unless explicitly designed for inheritance 2. **Prefer readonly structs** - For small, immutable value types 3. **Prefer static pure functions** - Better performance and testability 4. **Defer enumeration** - Don't materialize until you need to 5. **Return immutable collections** - From API boundaries --- ## Seal Classes by Default Sealing classes enables JIT devirtualization and communicates API intent. ```csharp // DO: Seal classes not designed for inheritance public sealed class OrderProcessor { public void Process(Order order) { } } // DO: Seal records (they're classes) public sealed record OrderCreated(OrderId Id, CustomerId CustomerId); // DON'T: Leave unsealed without reason public class OrderProcessor // Can be subclassed - intentional? { public virtual void Process(Order order) { } // Virtual = slower } ``` **Benefits:** - JIT can devirtualize method calls - Communicates "this is not an extension point" - Prevents accidental breaking changes --- ## Readonly Structs for Value Types Structs should be `readonly` when immutable. This prevents defensive copies. ```csharp // DO: Readonly struct for immutable value types public readonly record struct OrderId(Guid Value)...

Details

Author
Metalnib
Repository
Metalnib/dotnet-episteme-skills
Created
6 months ago
Last Updated
2 days ago
Language
C#
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category