Repository navigation
Correct resolution under concurrency and in tasks, a faster resolution, and four breaking corrections to the registration, environment, and property wrapper API.
Breaking Changes
- Instance registration takes a label —
register(_:)andregisterMain(_:)for an existing instance are nowregister(instance:)andregisterMain(instance:). Before, a service type passed without a factory, such asregisterMain(ViewModel.self), compiled and registered the metatype. Now it does not compile registerMainforServiceKeytypes is removed — A type with a main-actor-isolated initializer cannot conform toServiceKey. ReplaceregisterMain(X.self)withregisterMain(X.self) { X.default }ServiceEnvequality compares identity — Two values are equal if they share one registry.ServiceEnv(name: "test") == .testis nowfalse. To branch on the name, compareenv.name. The compiler does not report the affected call sites- An optional property wrapper is
nilonly for an unregistered service —@Service var x: Foo?wasnilfor every resolution error. IfFoois registered and cannot be created, the property now stops the process withfatalError, as a non-optional property does. That is the case when its factory throws, when a service that its factory needs is not registered, and when there is a dependency cycle. To getnilfor these, calltry? env.resolve(Foo.self)
Features
- Default MainActor Isolation — The MainActor Services guide covers modules built with
.defaultIsolation(MainActor.self), the setting of new Xcode app targets. A test target built with that setting covers the public API - Supported Swift Versions — The package supports the latest Swift release and the two minor releases before it, currently Swift 6.2, 6.3, and 6.4. CI builds and tests all three on Linux and macOS
- Benchmarks — A benchmark package measures the resolution paths. Its README records a baseline
Fixes
- Tasks started in a factory — A task that a factory or an initializer started got
circularDependencywhen it later resolved that service, or a service further up the same resolution chain. With@Service, that was afatalError. Such a task also kept the graph-scoped instances of that chain - Concurrent registration — Concurrent
registercalls lost registrations - Factory runs once — Concurrent first resolutions of a singleton or custom-scoped service ran the factory once for each caller. The factory now runs once, and the other callers wait for it
- False circular dependency — Two service types with the same short name, such as
A.ConfigandB.Config, were reported as a cycle. So was a factory that resolved its own type from another environment - Crash during reset — A cached instance whose
deinitused its environment stopped the process duringresetCaches(),resetScope(_:), orresetAll() - Graph scope across environments — A resolution chain that resolved a graph-scoped type from two environments got the instance of the first environment for both
ServiceisSendablewithout@unchecked
Performance
Instructions per operation on an Apple M4 Max with Swift 6.4:
| Operation | 1.3.0 | 1.4.0 |
|---|---|---|
| Resolve a cached singleton | 16007 | 953 |
| Resolve a transient service | 16092 | 2796 |
| Resolve a graph-scoped service | 18078 | 5428 |
| Resolve a transient service with two cached dependencies | 42624 | 5031 |
@Service, first access |
19824 | 3905 |
@Service, later accesses |
248 | 148 |
@MainService, later accesses |
766 | 132 |
Behavior Changes
- A resolution that returns a cached instance runs no cycle check and does not count toward
maxResolutionDepth - For a type that is not registered,
notRegisteredis thrown before the cycle check and the depth check - Registering a type again with another scope drops the instance that was cached for the previous scope. With the same scope, the cached instance stays until a reset
- Registering one type with both
registerandregisterMainis a programming error and stops a debug build. In a release build, the later registration now replaces the earlier one - If a reset runs while a factory is running, the resolutions that wait for that factory get its instance, and the instance is not cached
- A factory can run a nested run loop, which can run another task on the same thread. If that task resolves a singleton or custom-scoped service that the thread is creating, the resolution throws
circularDependency. In 1.3.0 the factory ran a second time - A factory must not wait for a task that it started if that task resolves the same service. Both then wait for each other. In 1.3.0 the task got
circularDependency - The package uses the thread-local storage of the platform's C library. Windows, which was not a supported platform, does not compile