Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created June 5, 2025 17:25
Show Gist options
  • Select an option

  • Save mbrandonw/bd57dc852579c32a3f5b20105e85fd8f to your computer and use it in GitHub Desktop.

Select an option

Save mbrandonw/bd57dc852579c32a3f5b20105e85fd8f to your computer and use it in GitHub Desktop.
import Synchronization
import SwiftUI
final class Counter1: Sendable {
let count = Mutex(0)
func increment() {
count.withLock { $0 += 1 }
}
func increment10000() {
for _ in 1...10_000 {
increment()
}
}
}
final actor Counter2 {
var count = 0
func increment() {
count += 1
}
func increment10000() {
for _ in 1...10_000 {
increment()
}
}
}
struct ContentView: View {
var body: some View {
Form {
Button("Mutex") {
let start = Date()
defer { print("mutex", Date().timeIntervalSince(start).formatted()) }
let counter = Counter1()
counter.increment10000()
}
Button("Actor") {
Task {
let start = Date()
defer { print("actor", Date().timeIntervalSince(start).formatted()) }
let counter = Counter2()
await counter.increment10000()
}
}
}
}
}
@seanwoodward

Copy link
Copy Markdown

Have you tried putting the for loop in the Task? Big difference.

@mbrandonw

Copy link
Copy Markdown
Author

@seanwoodward that's not the point of this code snippet. This is showing that independently locking fields of a data type can lead to worse performance than using an actor.

@seanwoodward

Copy link
Copy Markdown

@mbrandonw Do you find that still is the case? When I ran the code (on Mac), performance was very close for each.

@mbrandonw

Copy link
Copy Markdown
Author

Yes it generally is true, and it only becomes more true as the logic in the lock becomes more complex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment