Menu

1 / 2

Span, InlineArray and ~Escapable in Practice: Safe Low-Level Memory in Swift 6.2

DEV Community: swift·Dheerendra Chaturvedi·about 7 hours ago
#ylBZjN4i
#devtoswift#devto#dev#inlinearray#buffer
Long article (2440 words) — no AI summary yet.
Reading 0:00
15s threshold

For as long as I've written performance-sensitive Swift, "go fast" has meant "reach for withUnsafeBufferPointer ". When you need to parse a binary packet, checksum a blob, or walk a buffer without paying for bounds checks on a Collection you don't fully trust the optimizer to hoist, the unsafe pointer APIs were the only real option. They work. They're also a trap: the pointer is valid for exactly the duration of the closure, and nothing in the type system stops you — or the person who refactors your code next quarter — from letting it escape. One stray return buffer.baseAddress and you have a use-after-free that compiles cleanly and crashes in production.

Swift 6.2 finally gives us the tools to do this work safely : Span and RawSpan , MutableSpan , InlineArray , and the ~Escapable machinery that makes all of it sound. I spent a weekend porting some buffer-wrangling code to them and writing a small package ( SpanPlayground ) to pin down what actually works today.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.