In Molecules of Swift #011, I explore scope boundaries, cleanup order, and why cleanup should be registered only after successful acquisition.
#SwiftLang
https://ivantokar.com/posts/defer-put-cleanup-next-to-setup/

@ivantokar.bsky.social
Full-stack & product engineer. Server-side Swift & open source. Mac/iOS apps for fun. Learning robotics simulation. Sharing what I build and learn.
In Molecules of Swift #011, I explore scope boundaries, cleanup order, and why cleanup should be registered only after successful acquisition.
#SwiftLang
https://ivantokar.com/posts/defer-put-cleanup-next-to-setup/
This is useful when rendering: push a drawing state, register its restoration with defer, draw inside the block, then present the result outside it.
A defer block in Swift does not have to wait until the function returns. It runs when execution leaves the scope where it was registered. Put it inside a do block, and you can restore temporary state before the next operation in the same function.
The resulting pipeline supports the same transformations as an encoded image source. You can resize the pixels and export them in another format without first creating an intermediate image file.
#SwiftLang #ServerSideSwift #OpenSource
https://ivantokar.com/oss/hokusai/sources-input/raw-pixels/
Hokusai accepts raw pixel buffers directly. You specify the dimensions, pixel format and, when rows contain padding, the row stride. The input checks dimensions, stride and buffer size before calling native code.
A camera frame or a rendered chart may already be available as raw pixels. Encoding it as PNG just to pass it to an image-processing pipeline adds an unnecessary step.
A separate, initially non-blocking sanitizer job is one way to build confidence before making it a merge requirement.
But adding a sanitizer job to CI is not as simple as turning on a flag. False reports can make a required check unreliable, and the author measured a substantial slowdown for swift-collections.
The investigation also found false reports involving Linux Mutex and a compiler crash when building swift-collections with TSan. That matters for libraries using unsafe pointers or @unchecked Sendable: the compiler relies on promises that runtime instrumentation can help check.
Swift's compile-time concurrency checks and Thread Sanitizer solve different problems. A December 2 investigation into sanitizer coverage for core Swift packages reported a race in swift-async-algorithms after working around toolchain problems.
Perhaps the next major leap in AI won't come from smarter models alone, but from the architecture we build around them: persistent memory, event-driven execution, autonomy, and verification.
The improvement itself was modest. The interesting part was that the agent identified an opportunity, formulated a task, executed code, and produced a verifiable result without being explicitly asked to do so.
Most AI agents still wait for us to tell them what to do. Yesterday, a researcher reported that their OpenAI Dot independently improved a known mathematical result while monitoring a topic they were interested in.
#SwiftLang #OpenSource
https://ivantokar.com/posts/swift-collections-heap-two-ends-one-priority-queue/
The comparison with the minimum is O(1), and an accepted replacement takes O(log k), where k is the retained set size. A min-heap is enough for this particular task, but the two-ended Heap API expresses it directly. My Swift OSS Overview explores this and other practical uses of Heap.
You don't need to sort every score to keep the best 100. With Swift Collections' Heap, you can maintain a bounded set of high scores. Once it is full, compare each new score with the smallest retained value. If the candidate is better, replace that minimum with replaceMin(with:).
This gives a backend a clear startup contract: choose the limits, initialize the native runtime, then start serving requests. There is no public shutdown method to add to your server lifecycle.
#SwiftLang #ServerSideSwift #OpenSource
https://ivantokar.com/oss/hokusai/runtime-performance/runtime/
The runtime is process-wide, so configure it first. Once initialization begins, its configuration is frozen. Building a pipeline alone does not start the runtime.
An image-processing service should discover native initialization problems before it accepts its first request. Hokusai starts libvips lazily by default. That is convenient for scripts, but on a server you can explicitly call Hokusai.initialize() during startup instead.
For server code, the interesting question is how we can make unchecked assumptions about external input visible without treating every array access as a memory-safety violation. #SwiftLang #ServerSideSwift
https://forums.swift.org/t/pre-pitch-mark-array-subscripts-as-unsafe/91284