scratch-buffer-strategy

Identify optimal scratch-buffer strategies for hot-path .NET code.

9|1|Updated Jun 6, 2025
One-click install
npx skills add https://github.com/JeremyKuhne/touki --skill scratch-buffer-strategy
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: scratch-buffer-strategy
Source: https://github.com/JeremyKuhne/touki/tree/main/.agents/skills/scratch-buffer-strategy
Command: npx skills add https://github.com/JeremyKuhne/touki --skill scratch-buffer-strategy

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Identify and apply the most efficient scratch-buffer strategy to minimize allocations and zeroing on hot paths in .NET applications across net481 and net10.

Core Features & Use Cases

  • Options: [SkipLocalsInit] with stackalloc, zeroed stackalloc, BufferScope<T> with stack-then-pool fallback, and ArrayPool<T>.Shared renting.
  • Guidance: Provides a decision tree and crossover thresholds to pick the cheapest valid path for given buffer size and lifetime, with cross-framework considerations.
  • Use Case: When profiling a hot path that allocates a short-lived scratch buffer and you need to minimize allocations and zeroing while staying within stack limits.

Quick Start

Use the recommended scratch-buffer strategy for a hot-path buffer decision in your codebase.

Frequently Asked Questions about scratch-buffer-strategy

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

FAQPage Schema
What is the best way to allocate a scratch buffer on a .NET hot path?

The best way to allocate a scratch buffer on a hot path is to select the cheapest valid strategy based on size and lifetime, using a decision tree across stackalloc, BufferScope, and ArrayPool renting.

When should I use stackalloc versus ArrayPool renting for memory management?

Use stackalloc for small, short-lived buffers to avoid allocation overhead, and switch to ArrayPool renting when buffer sizes exceed safe stack limits, applying crossover thresholds to determine the exact switch point.

How does SkipLocalsInit improve scratch buffer performance in .NET?

SkipLocalsInit improves scratch buffer performance by skipping the automatic zeroing of local variables, reducing overhead for stackalloc buffers when the initial zero values are not required by the hot path logic.

Does the BufferScope stack-then-pool fallback strategy work across net481 and net10?

Yes, the BufferScope stack-then-pool fallback strategy works across net481 and net10, providing cross-framework memory cost considerations and thresholds to safely transition from stack allocation to pooled arrays.

What are the limitations of using stackalloc for variable buffer sizes in .NET?

The primary limitation of stackalloc for variable buffer sizes is the risk of stack overflow, requiring a maximum size threshold to determine when you must fall back to ArrayPool renting for hot path safety.