Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nit: unnecessary synchronization in Lazy::force_mut() #226

Closed
danielhenrymantilla opened this issue Apr 2, 2023 · 1 comment · Fixed by #231
Closed

Nit: unnecessary synchronization in Lazy::force_mut() #226

danielhenrymantilla opened this issue Apr 2, 2023 · 1 comment · Fixed by #231

Comments

@danielhenrymantilla
Copy link
Contributor

Self::force() has to synchronize the access to the raw value and/or its needed initialization, given its &-based API, but force_mut() does not, thanks to the guarantees of &mut access (e.g., https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#method.get_mut).

But the current implementation of force_mut() just defers to force():

once_cell/src/lib.rs

Lines 767 to 770 in 3514863

pub fn force_mut(this: &mut Lazy<T, F>) -> &mut T {
Self::force(this);
Self::get_mut(this).unwrap_or_else(|| unreachable!())
}

@matklad
Copy link
Owner

matklad commented Apr 2, 2023

Yup, this indeed should be improved!

bors bot added a commit that referenced this issue May 29, 2023
231: Avoid unnecessary synchronization in `{force,deref}_mut` r=matklad a=danielhenrymantilla

  - `DerefMut` was not delegating to `force_mut()` (contary to the by-ref APIs);
  - `Lazy::force_mut` was not taking advantage of exclusive-mutability access, and instead paying shared-mutability access. Mainly, in the `sync` case, it involved atomic operations which are now skipped.

Fixes #226

Co-authored-by: Daniel Henry-Mantilla <[email protected]>
@bors bors bot closed this as completed in 8f2d2f5 May 29, 2023
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 a pull request may close this issue.

2 participants