Skip to content

chore: Elide mutex variable from Lock class when it is unused#3483

Merged
Lukasa merged 1 commit intoapple:mainfrom
PassiveLogic:chore/elideUnusedVariablesInLock
Jan 22, 2026
Merged

chore: Elide mutex variable from Lock class when it is unused#3483
Lukasa merged 1 commit intoapple:mainfrom
PassiveLogic:chore/elideUnusedVariablesInLock

Conversation

@scottmarchant
Copy link
Copy Markdown
Contributor

@scottmarchant scottmarchant commented Jan 19, 2026

Motivation:

In reviewing a copy of this file in swift-log, @czechboy0 noticed that the mutex variable is essentially unused and unnecessarily allocated for certain conditions. It is possible the compiler elided usage. But given the unconditional variable reference in the deinit, the compiler may not be able to.

This change ensures the mutex property is completely elided except in the conditions where it is used.

Modifications:

  • Adjusted compiler directive pattern to completely elide the mutex property for conditions where it would be unused.
  • Adjusted deinit to avoid referencing mutex unless it is compiled into the Lock class

Result:

The mutex property is no longer allocated or even compiled into the Lock class for unsupported configurations. All unit tests and checks pass.

Research:

The deinit used to have a blanket call to mutex.deallocate(). This has been moved into the platform-specific checks within the deinit. This allows complete elision of the mutex property altogether. The _runtime(_multithreaded) condition appears to be rooted to this implementation. Diving deeper, all operating systems in this list except for UnknownOS and WASI result in _runtime(_multithreaded) returning true. That means that OpenBSD (and many other operating systems besides windows) were almost certainly using the #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) condition in the deinit. In conclusion, moving mutex.deallocate() up into the conditional clauses inside deinit should be identical to the previous implementation for all operating systems except llvm::Triple::UnknownOS. And llvm::Triple::UnknownOS is likely not to have every compiled anyways due to the check on line 32.

So in summary, it is expected that this clean up will compile identical to the previous implementation for all platforms except WASI platforms that don't have pthread support. For non-pthread WASI platforms, the unused mutex is properly elided from compilation altogether.

Testing Done:

  • Confirmed that unit tests pass locally using Xcode
  • Verified PR checks pass

@scottmarchant scottmarchant changed the title Temporary PR for lock elide change to run checks ahead of upstreaming chore: Elide mutex variable from Lock class when it is unused Jan 19, 2026
@scottmarchant scottmarchant marked this pull request as ready for review January 20, 2026 00:03
@scottmarchant scottmarchant force-pushed the chore/elideUnusedVariablesInLock branch from 633c201 to 7b50463 Compare January 20, 2026 00:13
Copy link
Copy Markdown
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change, thanks!

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jan 22, 2026
@Lukasa Lukasa enabled auto-merge (squash) January 22, 2026 10:25
@Lukasa Lukasa disabled auto-merge January 22, 2026 16:18
… handled compiler conditions conditions. Previously, the allocation would essentially be an unused allocation.
@scottmarchant scottmarchant force-pushed the chore/elideUnusedVariablesInLock branch from 7b50463 to fb6db90 Compare January 22, 2026 17:27
@scottmarchant
Copy link
Copy Markdown
Contributor Author

@Lukasa Thanks for reviewing this! I rebased this on the latest main

@Lukasa Lukasa enabled auto-merge (squash) January 22, 2026 17:34
@Lukasa Lukasa merged commit c337170 into apple:main Jan 22, 2026
54 checks passed
@scottmarchant scottmarchant deleted the chore/elideUnusedVariablesInLock branch January 22, 2026 18:03
kukushechkin pushed a commit to apple/swift-log that referenced this pull request Feb 12, 2026
…408)

Ports changes to `Lock` from swift-nio to swift-log.

### Motivation:

The copy-paste of `Lock` in swift-log should be updated with changes
from swift-nio.

### Modifications:

- Rolls back changed [made previously for
FreeBSD](https://github.com/apple/swift-log/pull/387/changes#diff-7b1bd45403dd1a7418287bd60682bf05ba2e4bf75dca1c0ca55a86d0477c9af5L73)
in favor of a consolidated implementation in swift-nio that will be in
place once apple/swift-nio#3494 merges. @kkebo
Please confirm this works. After both of our PR's merge, swift-log and
swift-nio will have the same implementation as far as FreeBSD is
concerned.
- Brought the following recent changes from swift-nio over to swift-log:

  - apple/swift-nio#3482
  - apple/swift-nio#3480
  - apple/swift-nio#3483

- Removed stale `// SRWLOCK does not need to be free'd` comment.

### Result:

`Lock` implementation will have the same implementation as swift-nio
(assuming apple/swift-nio#3494 merges).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants