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

Switch back to std::sync::Mutex? #4623

Closed
SUPERCILEX opened this issue Apr 15, 2022 · 17 comments · Fixed by #5885
Closed

Switch back to std::sync::Mutex? #4623

SUPERCILEX opened this issue Apr 15, 2022 · 17 comments · Fixed by #5885
Labels
A-tokio Area: The main tokio crate

Comments

@SUPERCILEX
Copy link
Contributor

rust-lang/rust#95035

std is using futex now and seems to beat parking lot in most cases. Are there contention benchmarks that could validate this for tokio?

@hawkw
Copy link
Member

hawkw commented Apr 15, 2022

Tokio's use of parking_lot is an off-by-default optional dependency, so Tokio actually uses std::sync::Mutex by default, unless the parking_lot feature flag is enabled.

However, I agree that it would be interesting to measure performance with std's new mutex implementation; we might stop recommending the use of parking_lot if performance is consistently better with std now...

@SUPERCILEX
Copy link
Contributor Author

Oops, I thought parking_lot was the default. Either way, if std faster I think parking_lot should just be removed entirely.

@Darksonn Darksonn added the A-tokio Area: The main tokio crate label Apr 15, 2022
@Darksonn
Copy link
Contributor

The parking_lot mutex has a const new method. We use that in some places, e.g. here.

@SUPERCILEX
Copy link
Contributor Author

Dang, though that stuff should be able to be deprecated as std is moving towards removing the allocation that prevents things from being const: rust-lang/rust#93740

@hawkw
Copy link
Member

hawkw commented Apr 15, 2022

Oops, I thought parking_lot was the default. Either way, if std faster I think parking_lot should just be removed entirely.

Removing the feature flag is a breaking change. It could be made into a no-op, where enabling it doesn't actually do anything and Tokio unconditionally would use the stdmutex, but this seems...questionable.

@SUPERCILEX
Copy link
Contributor Author

How does deprecation work? Would the idea be to say the flag is deprecated (AFAIK there's no way to mark a flag as deprecated) and then remove it in a major release?

I think it's perfectly reasonable to make it a noop (I would even argue removal is fine without a major release) because parking_lot is specifically documented as an "internal feature." Unless there's a policy about internal features I'm missing?

@jdahlstrom
Copy link

Futexes are a Linux-only thing, remember.

@hawkw
Copy link
Member

hawkw commented Apr 18, 2022

How does deprecation work? Would the idea be to say the flag is deprecated (AFAIK there's no way to mark a flag as deprecated) and then remove it in a major release?

There isn't any way to mark a feature flag itself as deprecated, but it could be made to emit a deprecation warning by enabling code that emits a deprecation warning. An otherwise unused type can be added and marked as deprecated to emit a deprecation warning when the feature is enabled.

Alternatively, a build script could check if the "deprecated" feature is enabled, and emit a warning using println!("cargo:warning=...);".

Both of these solutions are kind of hacky though.

@Noah-Kennedy
Copy link
Contributor

Personally I see no reason to get rid of this feature.

@hawkw
Copy link
Member

hawkw commented Apr 19, 2022

Yeah, IMO, the only compelling reason to remove the feature is to actually get rid of the need to test another feature combination, which we can't do without a breaking change anyway, so it's not going to happen any time soon. Users can always just choose not to enable parking_lot...which is what they get by default anyway.

@memoryruins
Copy link

The parking_lot mutex has a const new method. We use that in some places, e.g. here.

rust-lang/rust#97791 should make std's {Mutex, Condvar, RwLock}::new() const in rustc 1.63.0.

@Darksonn
Copy link
Contributor

That's nice to know. Increasing our MSRV to 1.63.0 isn't going to happen anytime soon, but it probably will eventually.

@eaufavor
Copy link

Meanwhile, does it make sense to move parking_lot out of the full feature group? Other features don't change how tokio works the same way parking_lot does.

@Noah-Kennedy
Copy link
Contributor

So for now that would be a breaking change due to const and MSRV. Once that change happens, I'd still say its a breaking change.

@nashley
Copy link

nashley commented Sep 28, 2022

Increasing our MSRV to 1.63.0 isn't going to happen anytime soon, but it probably will eventually.

Once this happens, can tokio's Mutex::new() become const as well, or are there other reasons that preclude it from becoming const?

@hawkw
Copy link
Member

hawkw commented Sep 28, 2022

Increasing our MSRV to 1.63.0 isn't going to happen anytime soon, but it probably will eventually.

Once this happens, can tokio's Mutex::new() become const as well, or are there other reasons that preclude it from becoming const?

Mutex::new could certainly become const fn once std::sync::Mutex::new is a const fn on Tokio's MSRV — note that when parking_lot is enabled, there's currently a separate const_new constructor. Once Tokio's MSRV is Rust 1.63.0 or newer, we could probably deprecate that in favor of making Mutex::new just be a const fn.

@NobodyXu
Copy link
Contributor

Increasing our MSRV to 1.63.0 isn't going to happen anytime soon, but it probably will eventually.

Once this happens, can tokio's Mutex::new() become const as well, or are there other reasons that preclude it from becoming const?

Mutex::new could certainly become const fn once std::sync::Mutex::new is a const fn on Tokio's MSRV — note that when parking_lot is enabled, there's currently a separate const_new constructor. Once Tokio's MSRV is Rust 1.63.0 or newer, we could probably deprecate that in favor of making Mutex::new just be a const fn.

Unfortunately they can't since with cfg(all(tokio_unstable, feature = "tracing")) Mutex::new will log using tracing::trace! and it also constructs a span using tracing::trace_span!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants