Skip to content
Skills Library
Programming · Building & coding

span-memory-refactor

/span-memory-refactor

Refactor hot C# code to use Span<T>, ReadOnlySpan<T>, and Memory<T> to cut allocations in parsing, slicing, and buffer work. Use this skill whenever the user wants to use Span/Memory, avoid substring/array-copy allocations, optimize parsing or string slicing, use stackalloc, or asks "how do I use Span here / make this allocation-free / speed up this parsing". Always use this skill for Span/Memory refactors; it rewrites safely and flags where spans don't fit.

Licence
MIT
Last repository change
09.08.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 09.08.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Refactor hot C# code to use Span<T>, ReadOnlySpan<T>, and Memory<T> to cut allocations in parsing, slicing, and buffer work. Use this skill whenever the user wants to use Span/Memory, avoid substring/array-copy allocations, optimize parsing or string slicing, use stackalloc, or asks "how do I use Span here / make this allocation-free / speed up this parsing". Always use this skill for Span/Memory refactors; it rewrites safely and flags where spans don't fit.

Original author description

Refactor hot C# code to use Span<T>, ReadOnlySpan<T>, and Memory<T> to cut allocations in parsing, slicing, and buffer work. Use this skill whenever the user wants to use Span/Memory, avoid substring/array-copy allocations, optimize parsing or string slicing, use stackalloc, or asks "how do I use Span here / make this allocation-free / speed up this parsing". Always use this skill for Span/Memory refactors; it rewrites safely and flags where spans don't fit.

How it works

  1. Input — point it at your code
  2. Common refactors
  3. Guardrails the skill enforces
  4. Principles
  5. How to use it & best prompts
Original SKILL.md headings · 50 lines, 299 words
SKILL.md preview
---
name: span-memory-refactor
description: Refactor hot C# code to use Span<T>, ReadOnlySpan<T>, and Memory<T> to cut allocations in parsing, slicing, and buffer work. Use this skill whenever the user wants to use Span/Memory, avoid substring/array-copy allocations, optimize parsing or string slicing, use stackalloc, or asks "how do I use Span here / make this allocation-free / speed up this parsing". Always use this skill for Span/Memory refactors; it rewrites safely and flags where spans don't fit.
category: Performance
version: 1.0.0
---

# Span<T> / Memory<T> Refactor Advisor

Rewrite hot parsing/slicing/buffer code to use `Span<T>`/`ReadOnlySpan<T>` (and `Memory<T>` for async) so it stops allocating intermediate strings and arrays — safely, and only where it pays.

## Input — point it at your code
Works on a target: a **file**, **folder**, **project**, or **GitHub URL**. Find candidates: `grep -rn "Substring\|Split(\|ToCharArray\|new byte\[\|Buffer" --include=*.cs <target>`.

## Common refactors
**Substring → slice (no allocation):**
```csharp
// allocates a new string per call
var year = date.Substring(0, 4);
// no allocation: a view over the original
ReadOnlySpan<char> year = date.AsSpan(0, 4);
int y = int.Parse(year);
```

**Split in a hot loop → `MemoryExtensions` enumerator / manual slicing:**
```csharp
ReadOnlySpan<char> rest = line;
int comma;
while ((comma = rest.IndexOf(',')) >= 0)
{
    ReadOnlySpan<char> field = rest[..comma];
    // use field without allocating
    rest = rest[(comma + 1)..];
}
```

**Small temporary buffer → `stackalloc` (no heap):**
```csharp
Span<byte> buffer = stackalloc byte[32];   // keep small; this is stack memory
```

**Async boundaries → `Memory<T>`** (you can't store a `Span<T>` across `await`; use `Memory<T>`/`ReadOnlyMemory<T>`).

## Guardrails the skill enforces
- `Span<T>` is a `ref struct`: **can't** be a field, captured in a lambda, used across `await`, or boxed. Use `Memory<T>` there.
- Keep `stackalloc` **small and bounded** (no user-controlled size) to avoid stack overflow.
- Don't return a span over `stackalloc`/local memory — it dangles.

## Principles
- **Only refactor hot paths.** Spans add cognitive cost; cold code stays readable.
- The win is **eliminating intermediate allocations** (substrings, split arrays), not raw arithmetic.
- Measure with `[MemoryDiagnoser]` — confirm allocations actually dropped.

## How to use it & best prompts
"Make this parser allocation-free with Span", "replace these Substring calls with slices", "use stackalloc for this buffer", "why can't I use Span across await here". Pairs with `memory-allocation-analyzer` and `benchmarkdotnet-setup`.
Files in this package (2)
  • SKILL.md 3 KB
  • USAGE.md 1 KB

Versions

The database has no other skills named span-memory-refactor.

Database snapshot: 13.11.2026 · package version de2df07ff9d5 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 84optimizing-memory-allocation
    Implements Zero Allocation patterns using Span, ArrayPool, and ObjectPool for memory efficiency in .NET. Use when reducing GC pressure or optimizing high-performance memory operations.
  • 100memory-allocation-analyzer
    Find unnecessary heap allocations in C# hot paths — LINQ in loops, boxing, closures, string concatenation, params arrays, async state machines — and rewrite to reduce GC pressure. Use this skill whenever the user wants to reduce allocations, cut GC pressure, find…
  • 100port-span-names
    Port a span op to low-cardinality span names, applied only when span streaming is enabled. Use when asked to make a span op's names low cardinality, remove raw URLs/IDs from span names, apply the Sentry span name conventions to an op, or "port <op> spans to low cardinality…
  • 100145-java-refactoring-high-performance
    Use when you need to refactor Java code for high performance — including memory/allocation reduction, CPU hot-path optimization, and syntax/API/control-flow improvements. This should trigger for requests such as Review Java code for high performance; Optimize Java hot path;…
  • 100memory
    Swift 6.2 InlineArray and Span types for zero-overhead memory access, fixed-size collections, and safe pointer alternatives. Use when optimizing performance-critical code paths.
  • 100spanify-buffers
    Find and fix unsafe buffer operations in a C++ file by removing UNSAFE_TODO markers and replacing unsafe raw pointers/C-style functions with base::span and standard safe containers. Use when the user asks to fix unsafe buffer warnings or `-Wunsafe-buffer-usage` errors. Don't use…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.