type-design-performance

Guide .NET type design to reduce allocations and improve performance.

Updated Jan 29, 2024
One-click install
npx skills add https://github.com/riandeoliveira/aspnet-template --skill type-design-performance-riandeoliveira
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-design-performance
Source: https://github.com/riandeoliveira/aspnet-template/tree/main/.claude/skills/csharp-type-design-performance
Command: npx skills add https://github.com/riandeoliveira/aspnet-template --skill type-design-performance-riandeoliveira

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Slow .NET code often comes from inefficient type design, leaky abstractions, and unnecessary allocations that reduce throughput in hot paths.

Core Features & Use Cases

  • Seal types and clarify extension intent for faster calls and safer API evolution, especially in libraries and public domain models.
  • Prefer readonly structs and record structs for small immutable value types to avoid defensive copies and improve locality, especially for IDs, money/value objects, and coordinates.
  • Use allocation-aware patterns like static pure functions, deferred enumeration, appropriate Task vs ValueTask, and Span/Memory to reduce LINQ/materialization overhead and improve performance in request/response workloads and streaming pipelines.
  • Choose correct collection boundaries by returning immutable or frozen collections at API edges to prevent accidental mutation and stabilize performance.

Quick Start

Apply the performance type design rules to your codebase by reviewing a specific set of types and methods for sealing, readonly/value semantics, enumeration strategy, and collection return types.

Frequently Asked Questions about type-design-performance

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

FAQPage Schema
How do I reduce allocations and improve .NET performance in hot paths?

Reduce .NET allocations in hot paths by applying type design rules like sealing types, using readonly structs for immutable values, and selecting allocation-aware patterns such as Span/Memory and deferred enumeration. These choices minimize garbage collection pressure and improve throughput.

When should I use a readonly struct versus a class for immutable value types in C#?

Use readonly structs for small immutable value types like IDs, money, or coordinates to avoid defensive copies and improve memory locality. Prefer classes when you need reference semantics or when the type is large and frequently mutated.

What is the best way to choose between Task and ValueTask for async methods?

Choose ValueTask over Task in high-throughput async paths to avoid heap allocations when operations often complete synchronously. Use Task for general-purpose async methods where allocation overhead is negligible compared to overall operation cost.

Why should I seal .NET classes and return immutable collections at API boundaries?

Seal .NET classes by default to enable devirtualization and faster method calls, and return immutable or frozen collections at API boundaries to prevent accidental mutation, stabilize performance, and ensure safer API evolution in libraries.

Does deferring enumeration reduce LINQ materialization overhead in streaming pipelines?

Deferring enumeration reduces LINQ materialization overhead by evaluating sequences lazily, which prevents intermediate allocations and improves performance in streaming pipelines and request/response workloads.

What are the limitations of using readonly structs for high-performance type design?

Readonly structs are unsuitable for large value types or mutable state, as passing them by value incurs copying overhead. They require careful sizing and immutability discipline to avoid performance regressions in complex .NET application workloads.