Guest User

Untitled

a guest
May 21st, 2026
82
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. ### Language Version
  2.  
  3. Swift 6.2 with strict concurrency, `@MainActor` isolation by default, and the following enabled features:
  4.  
  5. - **NonisolatedNonsendingByDefault** (SE-0461): Nonisolated async functions execute on the calling actor's executor by default, not the global concurrent pool.
  6. - **Named tasks** (SE-0469): `Task("Get Users") { ... }` — use descriptive names for debuggability.
  7. - **Caller isolation inheritance** (SE-0420): `func poll(isolation: isolated (any Actor)? = #isolation) async -> [Item]` — async functions can explicitly inherit the caller's isolation.
  8.  
  9. ### Concurrency Patterns
  10.  
  11. **Scheduling hierarchy** (cheapest to most expensive, confirmed by reading the Swift runtime source — see `~/Developer/Research/swift-concurrency-scheduling.md`):
  12.  
  13. 1. **`MainActor.assumeIsolated { }`** — near-zero cost. Synchronous assertion + direct call. Use for callbacks known to be on main (e.g., NotificationCenter with `queue: .main`, AppKit delegates).
  14. 2. **`await MainActor.run { }`** — no allocation, reuses existing task. Use when already in async context.
  15. 3. **`DispatchQueue.main.async { }`** — ~64B block allocation + direct GCD dispatch. Use for fire-and-forget from background or `@Sendable` contexts (e.g., `onTermination` handlers).
  16. 4. **`Task.immediate { @MainActor in }`** (SE-0472) — full Task allocation (~300-500B) but **no dispatch hop** if already on the right executor. Falls back to normal enqueue otherwise. Use when Task semantics are needed (cancellation, task-locals, priority) with predictable execution ordering.
  17. 5. **`Task { @MainActor in }`** — full Task allocation + always enqueues via dispatch_async. Only use when you specifically need deferred execution with Task semantics.
  18.  
  19. **Rules:**
  20. - **Never use `Task { @MainActor in }` for synchronous code** — anti-pattern that introduces unnecessary thread hops and ~5x more overhead than `DispatchQueue.main.async` for the same fundamental `dispatch_async_f` call.
  21. - **Use `.detached` to actually run off MainActor** — given implicit MainActor isolation, a plain `Task { }` inherits the actor context.
  22. - **Task.immediate preserves execution order** — unlike regular Task which always enqueues, `Task.immediate` runs synchronously when isolation is compatible, guaranteeing that mutations are visible immediately after the call returns.
  23.  
  24. **Isolated deinit** (SE-0471): Actor-isolated types can declare `isolated deinit` to safely access isolated state during deinitialization without resorting to unstructured concurrency.
  25.  
  26. ```swift
  27. @MainActor
  28. final class ResourceOwner {
  29. let connection: NonSendableConnection
  30.  
  31. isolated deinit {
  32. connection.close() // safe — runs on MainActor
  33. }
  34. }
  35. ```
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment