Swift’s native Clocks are very inefficient

Screenshot of Instruments showing the outline view for a Time Profile, expanded to show dozens of spurious, overhead functions taking up the vast majority of the runtime.

By which I mean, things like ContinuousClock and SuspendingClock. In absolute terms they don’t have much overhead – think sub-microsecond for most uses. Which makes them perfectly acceptable when they’re used sporadically (e.g. only a few times per second). However, if you need to deal with time and timing more frequently, their inefficiency can become… Read more

Matching prefixes in Swift strings

How do you determine if one string starts with another, in Swift? Surely that’s exactly what the hasPrefix(_:) method is for: No can haz etiquette? Wot? The problem is that hasPrefix is not meant for general use with human text; it’s barely better than a byte-wise comparison. It only guarantees that it won’t be fooled… Read more

Including Services in contextual menus in SwiftUI

Screenshot of the good Services submenu (as found in the application menu), with more options, app icons, and better grouping.

SwiftUI provides a way to provide a contextual menu for a view, with contextMenu(menuItems:) and friends, but it requires you to manually specify the entire contents of the contextual menu. That means it does not include the standard Services submenu. A brief history of Contextual Menus Contextual menus were introduced [to the Mac] in 1997… Read more

The privileges & challenges of being a primitive type for Codable in Swift

This is an elaboration of some posts made in the discussion of SE-425: 128-bit Integer Types. I think it warrants sharing a little more broadly – not because most people ever need to care about this, but rather because it’s interesting. You might have noticed that the primitive types e.g.: …all conform to Codable (which… Read more

Beware of specifying isolation requirements for whole protocols

Matt Massicotte has a well-written, brief introduction to isolation in Swift. But it mostly just enumerates the state of things, without offering much guidance. One pitfall in particular is important to call out, regarding isolated protocols. These might seem pretty similar – you’d be forgiven for assuming it’s just a convenience to put @MainActor on… Read more

A brief introduction to type memory layout in Swift

Most of the time we Swift programmers don’t really think about how our types – structs, classes, actors, enums, etc – are represented in memory. We just deal with them abstractly, knowing that somehow the compiler boils them down to a bunch of bytes, handling all the complexity of wrangling those bytes for us. However,… Read more