Skip to content

Use Mutex directly instead of ThreadSafeBox for class/actor stored properties#2695

Open
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:mutex-replace-threadsafebox
Open

Use Mutex directly instead of ThreadSafeBox for class/actor stored properties#2695
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:mutex-replace-threadsafebox

Conversation

@rintaro

@rintaro rintaro commented Jun 18, 2026

Copy link
Copy Markdown
Member

ThreadSafeBox is a class wrapping a Mutex<Value>. When stored as a property of another class or actor, the outer reference type already provides identity, so the extra indirection is just a redundant heap allocation. Replace ThreadSafeBox<T> with Mutex<T> at those sites.

Port SourceKitD's fileprivate computeIfNil extension from ThreadSafeBox to Mutex (now borrowing).

ThreadSafeBox is a `class` wrapping a `Mutex<Value>`. When stored as
a property of another class or actor, the outer reference type already
provides identity, so the extra ThreadSafeBox class indirection is just a
redundant heap allocation. Replace `ThreadSafeBox<T>` with `Mutex<T>` at
those sites.

Function-local ThreadSafeBox uses are unchanged since they need class
identity to be capturable in `@Sendable` closures.

CustomBuildServerTestProject.buildServerBox is also unchanged: it is
value-captured into a closure stored before `super.init`, which a
~Copyable Mutex can't support without a class wrapper.

Port SourceKitD's fileprivate `computeIfNil` extension from ThreadSafeBox
to Mutex (now `borrowing`).
@rintaro

rintaro commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

swiftlang/swift-tools-protocols#66
@swift-ci Please test

@rintaro

rintaro commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

swiftlang/swift-tools-protocols#66
@swift-ci Please test macOS

@rintaro

rintaro commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

swiftlang/swift-tools-protocols#66
@swift-ci Please test Linux

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants