csharp-type-design-performance

Enforce sealing, readonly structs, and immutable returns in .NET type design.

3|Updated Jan 24, 2024
One-click install
npx skills add https://github.com/akoken/dotfiles --skill csharp-type-design-performance
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-type-design-performance
Source: https://github.com/akoken/dotfiles/tree/main/config/.copilot/skills/csharp-type-design-performance
Command: npx skills add https://github.com/akoken/dotfiles --skill csharp-type-design-performance

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Designing .NET types for performance is challenging; this skill provides guidelines to minimize allocations, enable JIT devirtualization, and ensure safe, fast APIs.

Core Features & Use Cases

  • Sealing classes by default to improve inlining and API clarity.
  • Using readonly structs for small immutable value types.
  • Preferring static pure functions to reduce state and improve testability.
  • Deferring enumeration and returning immutable collections at API boundaries.

Quick Start

Command: Review a .NET type for performance improvements and propose changes to seal classes by default, use readonly structs for small value types, switch to static pure functions where possible, defer enumeration, and return immutable collections.

Frequently Asked Questions about csharp-type-design-performance

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

FAQPage Schema
How do I design high-performance .NET types to improve JIT devirtualization?

Design high-performance .NET types by sealing classes by default to enable JIT devirtualization and inlining. Use readonly structs for small immutable value types and prefer static pure functions to reduce state overhead.

Why should I use readonly structs for small immutable value types in C#?

Use readonly structs for small immutable value types in C# to prevent defensive copying and ensure memory layout efficiency. This pattern minimizes allocations and defends against accidental mutation when passing data across API boundaries.

What is the best way to return immutable collections at .NET API boundaries?

The best way to return immutable collections at .NET API boundaries is to defer enumeration and output immutable types. This prevents callers from mutating internal state, reduces memory allocations, and ensures safe, fast API responses.

Does sealing classes by default improve C# API performance and clarity?

Sealing classes by default improves C# API performance and clarity by enabling JIT inlining and preventing uncontrolled inheritance. This restricts extensibility to explicitly designed points, ensuring robust library development.

When do I need to review .NET types for performance improvements and deferred enumeration?

Review .NET types for performance improvements and deferred enumeration when developing performance-critical components, APIs, or libraries where memory layout and allocation overhead matter. Switching to static pure functions and deferred enumeration optimizes execution.

What are the limitations of using static pure functions for .NET type design?

Static pure functions limit state management, requiring all necessary data to be passed as parameters rather than stored internally. This improves testability and reduces side effects but may complicate APIs heavily reliant on shared state.