Skip to content

feat: take &Arc<Self> in fallible try_*_owned methods - #492

Open
wzxzhuxi wants to merge 1 commit into
apache:mainfrom
wzxzhuxi:try-owned-borrow-arc
Open

wzxzhuxi wants to merge 1 commit into
apache:mainfrom
wzxzhuxi:try-owned-borrow-arc

Conversation

@wzxzhuxi

@wzxzhuxi wzxzhuxi commented Nov 30, 2026 •

Copy link
Copy Markdown

Summary

Change Semaphore::try_acquire_owned, Mutex::try_lock_owned, and RwLock::{try_read_owned, try_write_owned} to take self: &Arc<Self> and clone the Arc only after a successful acquisition. Failed attempts no longer increment and decrement the reference count, which shares a cache line with the lock's state and caused cross-core contention under load shedding.

  • RwLock acquires the borrowed access token first and transfers permit ownership to the Arc only on success, keeping the failure path a plain atomic load.
  • Matches the receiver convention already used by pool::unbounded::Pool::try_get.
  • Doc examples and integration tests call the methods directly on an Arc; CHANGELOG.md records the change under ## Unreleased as a breaking change.

Benchmark

Failure paths on a saturated semaphore or held lock, comparing v0.8.1 against this branch with the same microbenchmark (--release with thin LTO, pinned CPUs 0-7, minimum of 3 runs; AMD Ryzen 5 5600H, x86_64 Linux):

Operation v0.8.1, 1 thread branch, 1 thread v0.8.1, 8 threads branch, 8 threads
Semaphore::try_acquire_owned 4.5-4.7 ns 1.6-1.7 ns 38.5 ns 0.33 ns
Mutex::try_lock_owned 4.5-4.6 ns 1.4 ns 35.7 ns 0.37-0.49 ns
RwLock::try_read_owned 4.7-4.8 ns 1.0-1.1 ns 32-33 ns 0.26-0.27 ns
RwLock::try_write_owned 4.7 ns 1.05-1.08 ns 36.7-36.8 ns 0.24 ns

Successful acquisition is unchanged in this microbenchmark (1-thread minimum: 10.0 ns on v0.8.1 vs 10.1 ns on this branch). Calling the new methods with the old arc.clone().try_*_owned() style reproduces the v0.8.1 numbers, confirming the difference comes from the removed reference-count operations rather than measurement noise.

Verification

  • cargo x build and cargo x test (workspace unit tests, integration tests, and doctests)
  • cargo +nightly x check (feature matrix with -Dwarnings)
  • cargo +nightly x lint (clippy, rustfmt, taplo, typos, hawkeye, and docsrs rustdoc)
  • cargo +nightly x miri with the CI MIRIFLAGS (-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-many-seeds=0..4)

Fixes #468

@wzxzhuxi
wzxzhuxi marked this pull request as ready for review November 30, 2026 07:39

This branch has not been deployed

No deployments
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.

Take &Arc<Self> in fallible try_*_owned acquisition methods before 1.0

1 participant