Repository navigation
Performance
generate is faster and uses less memory than 2.2.0 on graphs whose screens open one another, as in issue #646, and on deep mock trees. Every graph in issue #646 now generates in less wall time, CPU and peak memory than with 2.2.0. On that issue’s mocks, earlier 2.3 releases used more memory and wall time than 2.2.0. This release repays that regression and then improves on 2.2.0.
| Graph | Wall (2.3.3 vs 2.2.0) | CPU (2.3.3 vs 2.2.0) | Peak memory (2.3.3 vs 2.2.0) |
|---|---|---|---|
| Issue #646, 16 screens | 1.60 s vs 2.33 s | 1.92 s vs 5.85 s | 130 MB vs 457 MB |
| Issue #646, 12 screens with mocks | 4.45 s vs 4.93 s | 7.62 s vs 12.01 s | 242 MB vs 339 MB |
| Issue #646, 12 screens | 0.126 s vs 0.157 s | 0.173 s vs 0.314 s | 44 MB vs 48 MB |
| Mock chain 1,500 deep | 1.44 s vs 5.08 s | 1.56 s vs 5.12 s | 260 MB vs 265 MB (level within noise) |
These were measured on Linux with 4 cores, with generate run once per module the way Bazel and Tuist run it. Each figure is the median of 7 runs, or of 15 for the chain.
- Graphs whose screens open one another through instantiators and builders, as in issue #646. This includes graphs with nested lazy cycles.
generateis faster on them and uses less memory (see the table). - Deep mock trees. At every level of a mock tree, earlier 2.3 releases copied the bookkeeping for the levels above it, so that bookkeeping grew with the square of the tree’s depth. It is now shared and grows only with the depth. This repays that regression and brings a 1,500-deep chain’s peak memory back level with 2.2.0. Wall time on such a chain is under a third of 2.2.0’s.
- Seams.
generateis faster on modules with many seams that don’t reach themselves and call no other seam or public mock. This repays part of the cost that 2.3.0 added: on 1,200 such seams it takes 0.36 s less than 2.3.2. - Smaller files.
.safedifiles andscan’s cache are smaller, because the optional and Boolean fields of most stored values take fewer bytes.
Generated code and diagnostics are unchanged by all of the above.
New
- Spread mocks over 32 files (opt-in). Dependency trees stay in the combined file, and mocks are spread over
<name>_Mocks1.swiftto<name>_Mocks32.swift. An edit then recompiles only the files that hold the mocks it changed. On a module of 2,401 mocks, an incremental debug rebuild after an edit took 9 seconds instead of 20 (Linux, swiftc incremental debug build, 4 jobs). The option is recommended for modules with a few hundred mocks or more. It is off by default, and output without it does not change. See Build integrations for how to turn it on. - Hole builders on calling mocks. A generated
mock()whose tree calls another module’s public mock now takes an optional builder for each hole type that its calls fill, for exampleapiBuilder: (() -> any Api)? = nil.- If you pass a builder, it replaces every fill of that hole type in every call the mock makes. It also builds every place of that type in a public type that the mock builds inline instead of calling its mock. That includes places held in
ErasedInstantiator,SendableErasedInstantiatororLazilyInstantiated, and places inside a fill. An override of a place still takes precedence. - If you leave it
nil, the mock behaves as before. - An override of a called type receives the builder in place of the fill.
- No existing parameter is renamed. Where the mock already takes a required builder of the same hole type, no new parameter is added, and that builder is used at all of these places.
.publiccalling mocks take these builders too.
- If you pass a builder, it replaces every fill of that hole type in every call the mock makes. It also builds every place of that type in a public type that the mock builds inline instead of calling its mock. That includes places held in
Fixes
- In graphs whose types open one another through instantiators,
mock()could passnilinstead of the value bound at the mock’s root. This happened where a cycle builds a type a second time and a type in that type’s tree declares@Received(onlyIfAvailable: true). That type gotnileven when the test overrode the value. The unread override also caused a warning, which fails builds that treat warnings as errors. This was introduced in 2.3.0. Production code was not affected. generateno longer crashes with a stack overflow on macOS and other Apple platforms when a mock calls another module’s public mock (generateMock: .public) and the hole left for the caller is a few hundred levels deep.- A debug build of SafeDITool on Apple platforms no longer crashes when it reads a
.safedifile orscan’s cache that holds a deeply nested type, such as generics within generics. The deepest nesting SafeDI reads is now 32 levels, down from 128. A deeper type gets the existing error that asks you to declare a typealias for part of it.
Diagnostics
- When
generatecannot read a file passed with--transitive-module-info-file-path, the error is now the same on every run. Before, its wording could vary between runs, and when several files were unreadable, which one it named could vary too. The error now names the file and gives the reason: no file exists at that path, or the path names a directory or a file SafeDI may not open. When several files are unreadable, the first in path order is reported.
Build integrations
- Bazel: set
split_mocks = Trueto spread mocks over 32 files. An earlier measurement, not repeated since, found Bazel’s wall time about 8% higher at 32 files without the rules_swift persistent worker. That is why the option is off by default. - Custom builds: pass
generate --split-combined-output-mocks. - SwiftPM and Xcode plugins: they already write one file per mock and are not affected.
- Tuist plugin: it cannot pass the option yet.
Upgrading caveats
- Compared with 2.3.2, one shape changes. It occurs in a graph whose types open one another through instantiators, where a cycle builds a type a second time in a mock’s tree. If a type in that type’s tree has a
@Receiveddependency withoutonlyIfAvailable: true, that value is now an optional override inSafeDIOverridesinstead of a requiredmock()parameter. This matches every other shape and restores 2.2.0’s signature. A test written against 2.3.0 to 2.3.2 that passed that argument must drop it or pass it as an override.
Full Changelog: 2.3.2...2.3.3