memory
Learn Swift 6.2's low-level memory types designed for performance-critical code: InlineArray for fixed-size inline storage without heap allocation, and Span for safe, zero-cost access to contiguous memory. These compiler-checked alternatives replace common uses of UnsafeBufferPointer and hand-tuned tuple storage, helping you eliminate allocations in hot paths while maintaining memory safety.
memory skill teaches Swift 6.2 InlineArray and Span types to eliminate heap allocations in performance-critical code.
AI-generated summary based on this skill's SKILL.md
Install
rshankras/claude-code-apple-skills/memory · repository language: Swift
git clone https://github.com/rshankras/claude-code-apple-skills
cp -r claude-code-apple-skills/skills/swift/memory ~/.claude/skills/memoryFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to use Span for memory access in Swift 6.2?
Swift's Span type provides safe, zero-cost access to contiguous memory without the overhead of Array's copy-on-write semantics. memory uses Span as a non-escapable view over existing storage—pass it to functions that need read-only access to a buffer without copying. For mutable access, use MutableSpan. Both are compiler-checked alternatives to UnsafeBufferPointer, eliminating bounds-checking overhead while preserving memory safety in performance-critical code.
What is InlineArray and when should I use it?
memory's InlineArray is a fixed-size collection that stores elements directly inline—no heap allocation, no copy-on-write. Use InlineArray when you need a small, compile-time-sized collection in hot paths: signal processing, embedded Swift, or any scenario where eliminating allocations matters. InlineArray guarantees zero-copy semantics and provides compile-time size validation, making it ideal for replacing hand-tuned tuple storage or small Array instances.
How does memory help replace UnsafeBufferPointer with Span?
memory's Span types (Span, MutableSpan, RawSpan, UTF8Span) are compiler-checked safe alternatives to UnsafeBufferPointer. They enforce non-escapable lifetimes at compile time, preventing use-after-free bugs. Migrate by replacing UnsafeBufferPointer views with the appropriate Span variant: Span for typed read-only, MutableSpan for typed mutable, RawSpan for untyped binary data, UTF8Span for Unicode text. No runtime overhead—same performance, better safety.
Can memory eliminate heap allocations in Swift hot paths?
Yes. memory combines InlineArray for fixed-size inline storage with Span for zero-copy views over existing buffers. Together they eliminate the heap allocations and copy-on-write overhead that small Array instances incur. In performance-critical loops, replace Array with InlineArray (when size is known) and pass buffers as Span instead of copying. This keeps data on the stack and reduces GC pressure in embedded and real-time contexts.
What are RawSpan and UTF8Span used for?
memory's RawSpan provides safe, untyped access to binary data—ideal for parsing protocols, file formats, or network packets without unsafe casts. UTF8Span specializes in Unicode text processing, offering safe iteration and validation over UTF-8 bytes. Both are non-escapable views that prevent lifetime violations while maintaining zero-copy performance, making them essential for signal processing, embedded systems, and any binary I/O where safety and speed matter equally.
Does memory support value generics and compile-time size guarantees?
Yes. memory's InlineArray uses value generics (let count: Int) to encode fixed sizes at compile time, enabling the compiler to validate bounds and eliminate runtime checks. This compile-time guarantee means InlineArray storage is allocated inline with no heap indirection. The result: type-safe, zero-allocation fixed-size collections that the optimizer can fully specialize, delivering performance equivalent to hand-written C while maintaining Swift's memory safety.
SKILL.md
rendered from the published skill — quoted content, verbatim
InlineArray and Span
Guidance for Swift 6.2's low-level memory types: InlineArray for fixed-size inline storage without heap allocation, and Span for safe, zero-cost access to contiguous memory. These replace common uses of UnsafeBufferPointer and hand-tuned tuple storage with compiler-checked alternatives.
When This Skill Activates
Use this skill when the user:
- Asks about InlineArray, fixed-size arrays, or stack-allocated collections
- Mentions Span, MutableSpan, RawSpan, or UTF8Span
- Wants to eliminate heap allocations in hot paths
- Is replacing UnsafeBufferPointer or UnsafePointer with safe alternatives
- Asks about value generics or let count: Int generic parameters
- Needs zero-copy access to collection storage
- Mentions inline storage, contiguous memory, or memory layout
- Is
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 2 files
skills/swift/memory/SKILL.md
skills/swift/memory/swift-performance.md