buildanything 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +55 -0
  4. package/README.md +71 -61
  5. package/agents/ios-app-review-guardian.md +49 -0
  6. package/agents/ios-foundation-models-specialist.md +46 -0
  7. package/agents/ios-storekit-specialist.md +52 -0
  8. package/agents/ios-swift-architect.md +102 -0
  9. package/agents/ios-swift-search.md +130 -0
  10. package/agents/ios-swift-ui-design.md +104 -0
  11. package/commands/build.md +80 -176
  12. package/commands/fix.md +65 -0
  13. package/commands/setup.md +73 -0
  14. package/commands/ux-review.md +63 -0
  15. package/commands/verify.md +72 -0
  16. package/hooks/session-start +18 -1
  17. package/package.json +5 -2
  18. package/protocols/brainstorm.md +99 -0
  19. package/protocols/build-fix.md +52 -0
  20. package/protocols/cleanup.md +54 -0
  21. package/protocols/design.md +269 -0
  22. package/protocols/eval-harness.md +61 -0
  23. package/protocols/fake-data-detector.md +64 -0
  24. package/protocols/ios-context.md +235 -0
  25. package/protocols/ios-frameworks-map.md +323 -0
  26. package/protocols/ios-phase-branches.md +162 -0
  27. package/protocols/ios-preflight.md +27 -0
  28. package/protocols/metric-loop.md +93 -0
  29. package/protocols/planning.md +87 -0
  30. package/protocols/smoke-test.md +110 -0
  31. package/protocols/verify.md +67 -0
  32. package/protocols/web-phase-branches.md +201 -0
  33. package/skills/ios/_VENDORED.md +60 -0
  34. package/skills/ios/activitykit/LICENSE +131 -0
  35. package/skills/ios/activitykit/SKILL.md +505 -0
  36. package/skills/ios/activitykit/references/activitykit-patterns.md +868 -0
  37. package/skills/ios/app-intents/LICENSE +131 -0
  38. package/skills/ios/app-intents/SKILL.md +494 -0
  39. package/skills/ios/app-intents/references/appintents-advanced.md +1076 -0
  40. package/skills/ios/apple-on-device-ai/LICENSE +131 -0
  41. package/skills/ios/apple-on-device-ai/SKILL.md +505 -0
  42. package/skills/ios/apple-on-device-ai/references/coreml-conversion.md +425 -0
  43. package/skills/ios/apple-on-device-ai/references/coreml-optimization.md +344 -0
  44. package/skills/ios/apple-on-device-ai/references/foundation-models.md +508 -0
  45. package/skills/ios/apple-on-device-ai/references/mlx-swift.md +285 -0
  46. package/skills/ios/ios-26-platform/SKILL.md +53 -0
  47. package/skills/ios/ios-26-platform/references/automatic-adoption.md +161 -0
  48. package/skills/ios/ios-26-platform/references/backward-compat.md +238 -0
  49. package/skills/ios/ios-26-platform/references/liquid-glass.md +255 -0
  50. package/skills/ios/ios-26-platform/references/swiftui-apis.md +277 -0
  51. package/skills/ios/ios-26-platform/references/toolbar-navigation.md +250 -0
  52. package/skills/ios/ios-bootstrap/SKILL.md +98 -0
  53. package/skills/ios/ios-bootstrap/references/apple-docs-mcp-config.md +28 -0
  54. package/skills/ios/ios-bootstrap/references/new-project-dialog.md +41 -0
  55. package/skills/ios/ios-bootstrap/references/xcode-mcp-config.md +29 -0
  56. package/skills/ios/ios-debugger-agent/LICENSE +21 -0
  57. package/skills/ios/ios-debugger-agent/SKILL.md +58 -0
  58. package/skills/ios/ios-debugger-agent/agents/openai.yaml +4 -0
  59. package/skills/ios/ios-entitlements-generator/SKILL.md +47 -0
  60. package/skills/ios/ios-hig/SKILL.md +41 -0
  61. package/skills/ios/ios-hig/references/accessibility.md +81 -0
  62. package/skills/ios/ios-hig/references/content.md +142 -0
  63. package/skills/ios/ios-hig/references/feedback.md +123 -0
  64. package/skills/ios/ios-hig/references/interaction.md +199 -0
  65. package/skills/ios/ios-hig/references/performance-platform.md +129 -0
  66. package/skills/ios/ios-hig/references/privacy-permissions.md +181 -0
  67. package/skills/ios/ios-hig/references/visual-design.md +84 -0
  68. package/skills/ios/ios-info-plist-hardening/SKILL.md +130 -0
  69. package/skills/ios/ios-maestro-flow-author/SKILL.md +68 -0
  70. package/skills/ios/ios-maestro-flow-author/references/input-and-scroll.yaml +17 -0
  71. package/skills/ios/ios-maestro-flow-author/references/modal-and-dismiss.yaml +14 -0
  72. package/skills/ios/ios-maestro-flow-author/references/onboarding-flow.yaml +16 -0
  73. package/skills/ios/ios-maestro-flow-author/references/tab-navigation.yaml +13 -0
  74. package/skills/ios/ios-maestro-flow-author/references/tap-and-assert.yaml +9 -0
  75. package/skills/ios/swift-accessibility/LICENSE +21 -0
  76. package/skills/ios/swift-accessibility/SKILL.md +371 -0
  77. package/skills/ios/swift-accessibility/examples/before-after-appkit.md +446 -0
  78. package/skills/ios/swift-accessibility/examples/before-after-swiftui.md +441 -0
  79. package/skills/ios/swift-accessibility/examples/before-after-uikit.md +464 -0
  80. package/skills/ios/swift-accessibility/references/assistive-access.md +441 -0
  81. package/skills/ios/swift-accessibility/references/display-settings.md +491 -0
  82. package/skills/ios/swift-accessibility/references/dynamic-type.md +420 -0
  83. package/skills/ios/swift-accessibility/references/media-accessibility.md +421 -0
  84. package/skills/ios/swift-accessibility/references/motor-input.md +393 -0
  85. package/skills/ios/swift-accessibility/references/nutrition-labels.md +362 -0
  86. package/skills/ios/swift-accessibility/references/platform-specifics.md +515 -0
  87. package/skills/ios/swift-accessibility/references/semantic-structure.md +585 -0
  88. package/skills/ios/swift-accessibility/references/testing-auditing.md +507 -0
  89. package/skills/ios/swift-accessibility/references/voice-control.md +317 -0
  90. package/skills/ios/swift-accessibility/references/voiceover-swiftui.md +584 -0
  91. package/skills/ios/swift-accessibility/references/voiceover-uikit.md +519 -0
  92. package/skills/ios/swift-accessibility/references/wcag-mapping.md +167 -0
  93. package/skills/ios/swift-accessibility/resources/audit-template.swift +128 -0
  94. package/skills/ios/swift-accessibility/resources/qa-checklist.md +258 -0
  95. package/skills/ios/swift-concurrency/LICENSE +21 -0
  96. package/skills/ios/swift-concurrency/SKILL.md +171 -0
  97. package/skills/ios/swift-concurrency/references/_index.md +50 -0
  98. package/skills/ios/swift-concurrency/references/actors.md +660 -0
  99. package/skills/ios/swift-concurrency/references/async-algorithms.md +847 -0
  100. package/skills/ios/swift-concurrency/references/async-await-basics.md +266 -0
  101. package/skills/ios/swift-concurrency/references/async-sequences.md +710 -0
  102. package/skills/ios/swift-concurrency/references/core-data.md +560 -0
  103. package/skills/ios/swift-concurrency/references/glossary.md +135 -0
  104. package/skills/ios/swift-concurrency/references/linting.md +155 -0
  105. package/skills/ios/swift-concurrency/references/memory-management.md +569 -0
  106. package/skills/ios/swift-concurrency/references/migration.md +1104 -0
  107. package/skills/ios/swift-concurrency/references/performance.md +593 -0
  108. package/skills/ios/swift-concurrency/references/sendable.md +598 -0
  109. package/skills/ios/swift-concurrency/references/tasks.md +636 -0
  110. package/skills/ios/swift-concurrency/references/testing.md +592 -0
  111. package/skills/ios/swift-concurrency/references/threading.md +495 -0
  112. package/skills/ios/swift-security-expert/LICENSE +21 -0
  113. package/skills/ios/swift-security-expert/SKILL.md +470 -0
  114. package/skills/ios/swift-security-expert/references/biometric-authentication.md +565 -0
  115. package/skills/ios/swift-security-expert/references/certificate-trust.md +592 -0
  116. package/skills/ios/swift-security-expert/references/common-anti-patterns.md +690 -0
  117. package/skills/ios/swift-security-expert/references/compliance-owasp-mapping.md +537 -0
  118. package/skills/ios/swift-security-expert/references/credential-storage-patterns.md +721 -0
  119. package/skills/ios/swift-security-expert/references/cryptokit-public-key.md +505 -0
  120. package/skills/ios/swift-security-expert/references/cryptokit-symmetric.md +497 -0
  121. package/skills/ios/swift-security-expert/references/keychain-access-control.md +508 -0
  122. package/skills/ios/swift-security-expert/references/keychain-fundamentals.md +596 -0
  123. package/skills/ios/swift-security-expert/references/keychain-item-classes.md +476 -0
  124. package/skills/ios/swift-security-expert/references/keychain-sharing.md +458 -0
  125. package/skills/ios/swift-security-expert/references/migration-legacy-stores.md +727 -0
  126. package/skills/ios/swift-security-expert/references/secure-enclave.md +539 -0
  127. package/skills/ios/swift-security-expert/references/testing-security-code.md +781 -0
  128. package/skills/ios/swift-testing-expert/LICENSE +21 -0
  129. package/skills/ios/swift-testing-expert/SKILL.md +79 -0
  130. package/skills/ios/swift-testing-expert/references/_index.md +12 -0
  131. package/skills/ios/swift-testing-expert/references/async-testing-and-waiting.md +127 -0
  132. package/skills/ios/swift-testing-expert/references/expectations.md +145 -0
  133. package/skills/ios/swift-testing-expert/references/fundamentals.md +141 -0
  134. package/skills/ios/swift-testing-expert/references/migration-from-xctest.md +127 -0
  135. package/skills/ios/swift-testing-expert/references/parallelization-and-isolation.md +95 -0
  136. package/skills/ios/swift-testing-expert/references/parameterized-testing.md +284 -0
  137. package/skills/ios/swift-testing-expert/references/performance-and-best-practices.md +187 -0
  138. package/skills/ios/swift-testing-expert/references/traits-and-tags.md +114 -0
  139. package/skills/ios/swift-testing-expert/references/xcode-workflows.md +70 -0
  140. package/skills/ios/swiftdata-pro/LICENSE +21 -0
  141. package/skills/ios/swiftdata-pro/SKILL.md +102 -0
  142. package/skills/ios/swiftdata-pro/agents/openai.yaml +10 -0
  143. package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.png +0 -0
  144. package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.svg +29 -0
  145. package/skills/ios/swiftdata-pro/references/class-inheritance.md +104 -0
  146. package/skills/ios/swiftdata-pro/references/cloudkit.md +10 -0
  147. package/skills/ios/swiftdata-pro/references/core-rules.md +20 -0
  148. package/skills/ios/swiftdata-pro/references/indexing.md +27 -0
  149. package/skills/ios/swiftdata-pro/references/predicates.md +73 -0
  150. package/skills/ios/swiftui-design-principles/AGENTS.md +21 -0
  151. package/skills/ios/swiftui-design-principles/LICENSE +21 -0
  152. package/skills/ios/swiftui-design-principles/README.md +41 -0
  153. package/skills/ios/swiftui-design-principles/SKILL.md +605 -0
  154. package/skills/ios/swiftui-design-principles/metadata.json +10 -0
  155. package/skills/ios/swiftui-liquid-glass/LICENSE +21 -0
  156. package/skills/ios/swiftui-liquid-glass/SKILL.md +95 -0
  157. package/skills/ios/swiftui-liquid-glass/agents/openai.yaml +4 -0
  158. package/skills/ios/swiftui-liquid-glass/references/liquid-glass.md +280 -0
  159. package/skills/ios/swiftui-performance-audit/LICENSE +21 -0
  160. package/skills/ios/swiftui-performance-audit/SKILL.md +111 -0
  161. package/skills/ios/swiftui-performance-audit/agents/openai.yaml +4 -0
  162. package/skills/ios/swiftui-performance-audit/references/code-smells.md +150 -0
  163. package/skills/ios/swiftui-performance-audit/references/demystify-swiftui-performance-wwdc23.md +46 -0
  164. package/skills/ios/swiftui-performance-audit/references/optimizing-swiftui-performance-instruments.md +29 -0
  165. package/skills/ios/swiftui-performance-audit/references/profiling-intake.md +44 -0
  166. package/skills/ios/swiftui-performance-audit/references/report-template.md +47 -0
  167. package/skills/ios/swiftui-performance-audit/references/understanding-hangs-in-your-app.md +33 -0
  168. package/skills/ios/swiftui-performance-audit/references/understanding-improving-swiftui-performance.md +52 -0
  169. package/skills/ios/swiftui-pro/LICENSE +21 -0
  170. package/skills/ios/swiftui-pro/SKILL.md +108 -0
  171. package/skills/ios/swiftui-pro/agents/openai.yaml +10 -0
  172. package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.png +0 -0
  173. package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.svg +29 -0
  174. package/skills/ios/swiftui-pro/references/accessibility.md +13 -0
  175. package/skills/ios/swiftui-pro/references/api.md +39 -0
  176. package/skills/ios/swiftui-pro/references/data.md +43 -0
  177. package/skills/ios/swiftui-pro/references/design.md +31 -0
  178. package/skills/ios/swiftui-pro/references/hygiene.md +9 -0
  179. package/skills/ios/swiftui-pro/references/navigation.md +14 -0
  180. package/skills/ios/swiftui-pro/references/performance.md +46 -0
  181. package/skills/ios/swiftui-pro/references/swift.md +56 -0
  182. package/skills/ios/swiftui-pro/references/views.md +35 -0
  183. package/skills/ios/swiftui-ui-patterns/LICENSE +21 -0
  184. package/skills/ios/swiftui-ui-patterns/SKILL.md +100 -0
  185. package/skills/ios/swiftui-ui-patterns/agents/openai.yaml +4 -0
  186. package/skills/ios/swiftui-ui-patterns/references/app-wiring.md +201 -0
  187. package/skills/ios/swiftui-ui-patterns/references/async-state.md +96 -0
  188. package/skills/ios/swiftui-ui-patterns/references/components-index.md +50 -0
  189. package/skills/ios/swiftui-ui-patterns/references/controls.md +57 -0
  190. package/skills/ios/swiftui-ui-patterns/references/deeplinks.md +66 -0
  191. package/skills/ios/swiftui-ui-patterns/references/focus.md +90 -0
  192. package/skills/ios/swiftui-ui-patterns/references/form.md +97 -0
  193. package/skills/ios/swiftui-ui-patterns/references/grids.md +71 -0
  194. package/skills/ios/swiftui-ui-patterns/references/haptics.md +71 -0
  195. package/skills/ios/swiftui-ui-patterns/references/input-toolbar.md +51 -0
  196. package/skills/ios/swiftui-ui-patterns/references/lightweight-clients.md +93 -0
  197. package/skills/ios/swiftui-ui-patterns/references/list.md +86 -0
  198. package/skills/ios/swiftui-ui-patterns/references/loading-placeholders.md +38 -0
  199. package/skills/ios/swiftui-ui-patterns/references/macos-settings.md +71 -0
  200. package/skills/ios/swiftui-ui-patterns/references/matched-transitions.md +59 -0
  201. package/skills/ios/swiftui-ui-patterns/references/media.md +73 -0
  202. package/skills/ios/swiftui-ui-patterns/references/menu-bar.md +101 -0
  203. package/skills/ios/swiftui-ui-patterns/references/navigationstack.md +159 -0
  204. package/skills/ios/swiftui-ui-patterns/references/overlay.md +45 -0
  205. package/skills/ios/swiftui-ui-patterns/references/performance.md +62 -0
  206. package/skills/ios/swiftui-ui-patterns/references/previews.md +48 -0
  207. package/skills/ios/swiftui-ui-patterns/references/scroll-reveal.md +133 -0
  208. package/skills/ios/swiftui-ui-patterns/references/scrollview.md +87 -0
  209. package/skills/ios/swiftui-ui-patterns/references/searchable.md +71 -0
  210. package/skills/ios/swiftui-ui-patterns/references/sheets.md +155 -0
  211. package/skills/ios/swiftui-ui-patterns/references/split-views.md +72 -0
  212. package/skills/ios/swiftui-ui-patterns/references/tabview.md +114 -0
  213. package/skills/ios/swiftui-ui-patterns/references/theming.md +71 -0
  214. package/skills/ios/swiftui-ui-patterns/references/title-menus.md +93 -0
  215. package/skills/ios/swiftui-ui-patterns/references/top-bar.md +49 -0
  216. package/skills/ios/swiftui-view-refactor/LICENSE +21 -0
  217. package/skills/ios/swiftui-view-refactor/SKILL.md +207 -0
  218. package/skills/ios/swiftui-view-refactor/agents/openai.yaml +4 -0
  219. package/skills/ios/swiftui-view-refactor/references/mv-patterns.md +161 -0
  220. package/skills/ios/widgetkit/LICENSE +131 -0
  221. package/skills/ios/widgetkit/SKILL.md +502 -0
  222. package/skills/ios/widgetkit/references/widgetkit-advanced.md +871 -0
@@ -0,0 +1,593 @@
1
+ # Performance
2
+
3
+ Use this when:
4
+
5
+ - Async code is slower than expected or causing UI hangs.
6
+ - You need to choose between synchronous, asynchronous, and parallel execution.
7
+ - You are profiling concurrency overhead with Instruments.
8
+
9
+ Skip this file if:
10
+
11
+ - The issue is a compiler diagnostic about isolation or Sendable. Use `actors.md` or `sendable.md`.
12
+ - You mainly need to fix a memory leak. Use `memory-management.md`.
13
+
14
+ Jump to:
15
+
16
+ - Core Principles
17
+ - Common Performance Issues
18
+ - Using Xcode Instruments
19
+ - Suspension Points / Reducing Suspensions
20
+ - Choosing Execution Style
21
+ - Parallelism Costs
22
+ - Optimization Checklist
23
+
24
+ ## Core Principles
25
+
26
+ ### Measurement is essential
27
+
28
+ Can't improve what you don't measure. Establish baseline before optimizing.
29
+
30
+ ### Start simple, optimize later
31
+
32
+ ```
33
+ Synchronous → Asynchronous → Parallel
34
+ ```
35
+
36
+ Move right only when proven necessary.
37
+
38
+ ### Three phases of concurrency
39
+
40
+ 1. **No concurrency** - Synchronous method
41
+ 2. **Suspend without parallelism** - Asynchronous method
42
+ 3. **Advanced concurrency** - Parallel execution
43
+
44
+ ## Common Performance Issues
45
+
46
+ ### UI hangs
47
+
48
+ Too much work on main thread causes interface freezes.
49
+
50
+ ### Poor parallelization
51
+
52
+ Heavy work funneled into single task instead of parallel execution.
53
+
54
+ ### Actor contention
55
+
56
+ Tasks waiting on busy actor, causing unnecessary suspensions.
57
+
58
+ ## Using Xcode Instruments
59
+
60
+ ### Swift Concurrency template
61
+
62
+ Profile with CMD + I → Select "Swift Concurrency" template.
63
+
64
+ **Instruments included**:
65
+ - **Swift Tasks**: Track running, alive, total tasks
66
+ - **Swift Actors**: Show actor execution and queue size
67
+
68
+ ### Key metrics
69
+
70
+ ```
71
+ Tasks:
72
+ - Total count
73
+ - Running vs suspended
74
+ - Task states (Creating, Running, Suspended, Ending)
75
+
76
+ Actors:
77
+ - Queue size
78
+ - Execution time
79
+ - Contention points
80
+
81
+ Main Thread:
82
+ - Hangs
83
+ - Blocked time
84
+ ```
85
+
86
+ ### Task states
87
+
88
+ - **Creating**: Task being initialized
89
+ - **Running**: Actively executing
90
+ - **Suspended**: Waiting (at await)
91
+ - **Ending**: Completing
92
+
93
+ > **Course Deep Dive**: This topic is covered in detail in [Lesson 10.1: Using Xcode Instruments to find performance bottlenecks](https://www.swiftconcurrencycourse.com?utm_source=github&utm_medium=agent-skill&utm_campaign=lesson-reference)
94
+
95
+ ## Identifying Issues
96
+
97
+ ### Main thread blocked
98
+
99
+ ```swift
100
+ // ❌ All work on main thread
101
+ @MainActor
102
+ func generateWallpapers() {
103
+ Task {
104
+ for _ in 0..<100 {
105
+ let image = generator.generate() // Blocks main thread
106
+ wallpapers.append(image)
107
+ }
108
+ }
109
+ }
110
+ ```
111
+
112
+ **Instruments shows**: Long main thread hang, no parallelism.
113
+
114
+ ### Solution: Move to background
115
+
116
+ ```swift
117
+ @MainActor
118
+ func generateWallpapers() {
119
+ Task {
120
+ for _ in 0..<100 {
121
+ let image = await backgroundGenerator.generate()
122
+ wallpapers.append(image)
123
+ }
124
+ }
125
+ }
126
+
127
+ actor BackgroundGenerator {
128
+ func generate() -> Image {
129
+ // Heavy work in background
130
+ }
131
+ }
132
+ ```
133
+
134
+ ### Actor contention
135
+
136
+ ```swift
137
+ actor Generator {
138
+ func generate() -> Image {
139
+ // Heavy work
140
+ }
141
+ }
142
+
143
+ // ❌ Sequential through actor
144
+ for _ in 0..<100 {
145
+ let image = await generator.generate() // Queue size = 1
146
+ }
147
+ ```
148
+
149
+ **Instruments shows**: Actor queue never exceeds 1, no parallelism.
150
+
151
+ ### Solution: Remove unnecessary actor
152
+
153
+ ```swift
154
+ struct Generator {
155
+ @concurrent
156
+ static func generate() async -> Image {
157
+ // Heavy work, no shared state
158
+ }
159
+ }
160
+
161
+ // ✅ Parallel execution
162
+ for i in 0..<100 {
163
+ Task(name: "Image \(i)") {
164
+ let image = await Generator.generate()
165
+ await addToCollection(image)
166
+ }
167
+ }
168
+ ```
169
+
170
+ ## Suspension Points
171
+
172
+ ### What creates suspension
173
+
174
+ Every `await` is potential suspension point:
175
+
176
+ ```swift
177
+ let data = await fetchData() // May suspend
178
+ ```
179
+
180
+ **Not guaranteed** - if isolation matches, may not suspend.
181
+
182
+ ### Suspension surface area
183
+
184
+ Code between suspension points. Larger = harder to reason about:
185
+ - Actor invariants
186
+ - Performance
187
+ - Thread hops
188
+ - Reentrancy
189
+ - State consistency
190
+
191
+ ### Goal
192
+
193
+ - Do work before crossing isolation
194
+ - Cross once
195
+ - Finish job
196
+ - Only cross again when necessary
197
+
198
+ ## Reducing Suspensions
199
+
200
+ ### 1. Use synchronous methods
201
+
202
+ ```swift
203
+ // ❌ Unnecessary async
204
+ private func scale(_ image: CGImage) async { }
205
+
206
+ func process(_ image: CGImage) async {
207
+ let scaled = await scale(image) // Suspension point
208
+ }
209
+
210
+ // ✅ Synchronous helper
211
+ private func scale(_ image: CGImage) { }
212
+
213
+ func process(_ image: CGImage) async {
214
+ let scaled = scale(image) // No suspension
215
+ }
216
+ ```
217
+
218
+ **Rule**: If method doesn't need to suspend, don't mark async.
219
+
220
+ ### 2. Prevent actor reentrancy
221
+
222
+ ```swift
223
+ // ❌ Reenters actor
224
+ actor BankAccount {
225
+ func deposit(_ amount: Int) async {
226
+ balance += amount
227
+ await logTransaction() // Leaves actor
228
+ balance += bonus // Reenters - state may have changed
229
+ }
230
+ }
231
+
232
+ // ✅ Complete work before leaving
233
+ actor BankAccount {
234
+ func deposit(_ amount: Int) async {
235
+ balance += amount
236
+ balance += bonus
237
+ await logTransaction() // Leave after state changes
238
+ }
239
+ }
240
+ ```
241
+
242
+ ### 3. Inherit isolation
243
+
244
+ ```swift
245
+ // ❌ Switches isolation
246
+ @MainActor
247
+ func update() async {
248
+ await process() // Switches away from main actor
249
+ }
250
+
251
+ // ✅ Inherits isolation (still requires await -- but no executor hop)
252
+ @MainActor
253
+ func update() async {
254
+ await process() // Stays on main actor when nonisolated(nonsending)
255
+ }
256
+
257
+ nonisolated(nonsending) func process() async { }
258
+ ```
259
+
260
+ > **Course Deep Dive**: This topic is covered in detail in [Lesson 10.2: Reducing suspension points by managing isolation effectively](https://www.swiftconcurrencycourse.com?utm_source=github&utm_medium=agent-skill&utm_campaign=lesson-reference)
261
+
262
+ ### 4. Use non-suspending APIs
263
+
264
+ ```swift
265
+ // ❌ May suspend
266
+ try await Task.checkCancellation()
267
+
268
+ // ✅ No suspension
269
+ if Task.isCancelled {
270
+ return
271
+ }
272
+ ```
273
+
274
+ ### 5. Embrace parallelism
275
+
276
+ ```swift
277
+ // ❌ Sequential
278
+ for url in urls {
279
+ let image = await download(url)
280
+ images.append(image)
281
+ }
282
+
283
+ // ✅ Parallel
284
+ await withTaskGroup(of: Image.self) { group in
285
+ for url in urls {
286
+ group.addTask { await download(url) }
287
+ }
288
+ for await image in group {
289
+ images.append(image)
290
+ }
291
+ }
292
+ ```
293
+
294
+ ## Analyzing Suspensions in Instruments
295
+
296
+ ### View task states
297
+
298
+ 1. Select Swift Tasks instrument
299
+ 2. Switch to "Task States" view
300
+ 3. Look for Suspended states
301
+ 4. Check suspension duration
302
+
303
+ ### Navigate to code
304
+
305
+ 1. Click task state (Running/Suspended)
306
+ 2. Open Extended Detail
307
+ 3. Click related method
308
+ 4. Use "Open in Source Viewer"
309
+
310
+ ### Predict suspensions
311
+
312
+ ```swift
313
+ Task {
314
+ // State 1: Running
315
+ // State 2: Suspended (switch to background)
316
+ let data = await backgroundWork()
317
+ // State 3: Running (in background)
318
+ // State 4: Suspended (switch to main actor)
319
+ // State 5: Running (on main actor)
320
+ await MainActor.run {
321
+ updateUI(data)
322
+ }
323
+ }
324
+ ```
325
+
326
+ ### Optimization example
327
+
328
+ ```swift
329
+ // Before: Two suspensions
330
+ Task {
331
+ let data = await generate() // Suspension 1
332
+ self.items.append(data) // Suspension 2 (back to main)
333
+ }
334
+
335
+ > **Course Deep Dive**: This topic is covered in detail in [Lesson 10.3: Using Xcode Instruments to detect and remove suspension points](https://www.swiftconcurrencycourse.com?utm_source=github&utm_medium=agent-skill&utm_campaign=lesson-reference)
336
+
337
+ // After: One suspension
338
+ Task { @concurrent in
339
+ let data = generate() // No suspension (synchronous)
340
+ await MainActor.run {
341
+ self.items.append(data) // Suspension 1 (to main)
342
+ }
343
+ }
344
+ ```
345
+
346
+ ## Choosing Execution Style
347
+
348
+ ### Decision checklist
349
+
350
+ **Use async/parallel if**:
351
+ - [ ] Blocks main actor visibly (>16ms)
352
+ - [ ] Scales with data (N items → N cost)
353
+ - [ ] Involves I/O (network, disk)
354
+ - [ ] Benefits from combining operations
355
+ - [ ] Called frequently
356
+
357
+ **2+ checks** → async/parallel justified.
358
+
359
+ ### Start synchronous
360
+
361
+ ```swift
362
+ // Start here
363
+ func processData(_ data: Data) -> Result {
364
+ // Fast, in-memory work
365
+ }
366
+ ```
367
+
368
+ **Only move to async if**:
369
+ - Instruments show main thread hang
370
+ - User reports sluggishness
371
+ - Work scales with input size
372
+
373
+ ### When to use async
374
+
375
+ ```swift
376
+ func processData(_ data: Data) async -> Result {
377
+ // Use when:
378
+ // - Touches persistent storage
379
+ // - Parses large datasets
380
+ // - Network communication
381
+ // - Proven slow by profiling
382
+ }
383
+ ```
384
+
385
+ ### When to use parallel
386
+
387
+ ```swift
388
+ await withTaskGroup(of: Result.self) { group in
389
+ for item in items {
390
+ group.addTask { await process(item) }
391
+ }
392
+ }
393
+
394
+ // Use when:
395
+ // - Multiple independent operations
396
+ // - Time-to-first-result matters
397
+
398
+ > **Course Deep Dive**: This topic is covered in detail in [Lesson 10.4: How to choose between serialized, asynchronous, and parallel execution](https://www.swiftconcurrencycourse.com?utm_source=github&utm_medium=agent-skill&utm_campaign=lesson-reference)
399
+ // - Work scales with collection size
400
+ // - Proven beneficial by profiling
401
+ ```
402
+
403
+ ## Parallelism Costs
404
+
405
+ ### Tradeoffs
406
+
407
+ **Benefits**:
408
+ - Faster completion (if CPU-bound)
409
+ - Better resource utilization
410
+ - Improved responsiveness
411
+
412
+ **Costs**:
413
+ - Increased memory pressure
414
+ - CPU scheduling overhead
415
+ - System resource saturation
416
+ - Battery drain
417
+ - Thermal impact
418
+
419
+ ### When parallelism hurts
420
+
421
+ ```swift
422
+ // ❌ Over-parallelization
423
+ for i in 0..<1000 {
424
+ Task { await lightWork(i) }
425
+ }
426
+ // Creates 1000 tasks for trivial work
427
+ ```
428
+
429
+ **Better**: Batch work or use fewer tasks.
430
+
431
+ ## UX-Driven Decisions
432
+
433
+ ### Smooth animations > raw speed
434
+
435
+ ```swift
436
+ // 80ms on main thread, but animation stutters
437
+ @MainActor
438
+ func process() {
439
+ heavyWork() // Freezes UI for 1 frame
440
+ }
441
+
442
+ // 100ms total, but smooth UI
443
+ @MainActor
444
+ func process() async {
445
+ await backgroundWork() // UI stays responsive
446
+ }
447
+ ```
448
+
449
+ **Perception**: Smooth feels faster than raw speed.
450
+
451
+ ### Progress indication
452
+
453
+ ```swift
454
+ @MainActor
455
+ func loadItems() async {
456
+ isLoading = true
457
+
458
+ for i in 0..<100 {
459
+ let item = await fetchItem(i)
460
+ items.append(item)
461
+ progress = Double(i) / 100 // Incremental updates
462
+ }
463
+
464
+ isLoading = false
465
+ }
466
+ ```
467
+
468
+ Background work + progress = feels faster.
469
+
470
+ ## Optimization Checklist
471
+
472
+ Before optimizing, ask:
473
+
474
+ - [ ] Have I profiled with Instruments?
475
+ - [ ] Is main thread actually blocked?
476
+ - [ ] Can this be synchronous?
477
+ - [ ] Am I over-parallelizing?
478
+ - [ ] Is actor contention the issue?
479
+ - [ ] Are suspensions necessary?
480
+ - [ ] Does UX require background work?
481
+ - [ ] Will this scale with data?
482
+
483
+ ## Common Patterns
484
+
485
+ ### Move heavy work to background
486
+
487
+ ```swift
488
+ // Before
489
+ @MainActor
490
+ func generate() {
491
+ for _ in 0..<100 {
492
+ let item = heavyGeneration()
493
+ items.append(item)
494
+ }
495
+ }
496
+
497
+ // After
498
+ @MainActor
499
+ func generate() async {
500
+ for _ in 0..<100 {
501
+ let item = await backgroundGenerate()
502
+ items.append(item)
503
+ }
504
+ }
505
+
506
+ @concurrent
507
+ func backgroundGenerate() async -> Item {
508
+ // Heavy work off main thread
509
+ }
510
+ ```
511
+
512
+ ### Parallelize independent work
513
+
514
+ ```swift
515
+ // Before: Sequential
516
+ for url in urls {
517
+ let image = await download(url)
518
+ images.append(image)
519
+ }
520
+
521
+ // After: Parallel
522
+ await withTaskGroup(of: Image.self) { group in
523
+ for url in urls {
524
+ group.addTask { await download(url) }
525
+ }
526
+ for await image in group {
527
+ images.append(image)
528
+ }
529
+ }
530
+ ```
531
+
532
+ ### Reduce actor hops
533
+
534
+ ```swift
535
+ // Before: Multiple hops
536
+ actor Store {
537
+ func process() async {
538
+ let a = await fetch1() // Hop 1
539
+ let b = await fetch2() // Hop 2
540
+ let c = await fetch3() // Hop 3
541
+ combine(a, b, c)
542
+ }
543
+ }
544
+
545
+ // After: Batch fetches
546
+ actor Store {
547
+ func process() async {
548
+ async let a = fetch1()
549
+ async let b = fetch2()
550
+ async let c = fetch3()
551
+ combine(await a, await b, await c) // One hop
552
+ }
553
+ }
554
+ ```
555
+
556
+ ## Best Practices
557
+
558
+ 1. **Profile before optimizing** - measure baseline
559
+ 2. **Start synchronous** - add async only when needed
560
+ 3. **Use Instruments regularly** - catch issues early
561
+ 4. **Name tasks** - easier debugging in Instruments
562
+ 5. **Check suspension count** - reduce unnecessary awaits
563
+ 6. **Avoid premature parallelism** - has costs
564
+ 7. **Consider UX** - smooth > fast
565
+ 8. **Batch actor work** - reduce contention
566
+ 9. **Test on real devices** - simulators lie
567
+ 10. **Monitor in production** - real usage patterns differ
568
+
569
+ ## Debugging Performance
570
+
571
+ ### Instruments workflow
572
+
573
+ 1. Profile with Swift Concurrency template
574
+ 2. Identify main thread hangs
575
+ 3. Check task parallelism
576
+ 4. Analyze actor queue sizes
577
+ 5. Review suspension points
578
+ 6. Navigate to problematic code
579
+ 7. Apply optimizations
580
+ 8. Re-profile to verify
581
+
582
+ ### Red flags in Instruments
583
+
584
+ - Main thread blocked >16ms
585
+ - Actor queue size always 1
586
+ - High suspension count
587
+ - Tasks created but not running
588
+ - Excessive task creation (1000+)
589
+
590
+ ## Further Learning
591
+
592
+ For real-world optimization examples, profiling techniques, and advanced performance patterns, see [Swift Concurrency Course](https://www.swiftconcurrencycourse.com).
593
+