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

Multithreaded component refresh #1232

Merged
merged 6 commits into from Mar 20, 2024

Conversation

softdevca
Copy link
Contributor

@softdevca softdevca commented Mar 19, 2024

This PR causes the bench_refresh_components benchmark to go from 1,517,282 ns/iter to 142,299 ns/iter when the "multithread" feature is enabled, which it is on by default. This massive improvement in speed is because reading from the /sys/class/hwmon/ files in Linux can be slow. In particular reading the CPU and chipset temperatures were taking about 60 ms each on my system.

src/common.rs Outdated
@@ -3822,9 +3822,19 @@ impl Components {
/// components.refresh();
/// ```
pub fn refresh(&mut self) {
for component in self.list_mut() {
component.refresh();
#[cfg(feature = "multithread")]
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of doing it like this, could you instead create an iter_mut() function into utils.rs (which would be the same as into_iter except it would call par_iter_mut/iter_mut) please? The idea is to prevent the duplication of .for_each(|component| component.refresh()). It might seem super small but it's easy to lose track of such things quite easily.

@GuillaumeGomez
Copy link
Owner

Apart from my suggestion, this is a great improvement, thanks!

@softdevca
Copy link
Contributor Author

Thank you for the suggestion. I have made the change.

While implementing into_iter_mut I noticed the #[cfg] conditional on the existing into_iter function did not enable parallelization for Linux. Simplifying the conditional to support Linux resulted in additional speed improvements in the benchmark.

feature = "unknown-ci",
all(target_os = "macos", feature = "apple-sandbox")
))]
pub(crate) fn into_iter_mut<T>(val: T) -> T::IntoIter
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't it be:

Suggested change
pub(crate) fn into_iter_mut<T>(val: T) -> T::IntoIter
pub(crate) fn into_iter_mut<T>(val: &mut T) -> T::IntoIter

to enforce that val is mutably borrowed? It would also make it more coherent with the rayon function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is slightly confusing but in the multithreaded case the &mut is saying the parallel iterator is mutable. In the non-multithreaded case the &mut is already part of T and specifying it will result in &mut &mut T.

Copy link
Owner

Choose a reason for hiding this comment

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

Can you add a code comment to explain that please? Like that future me will not wonder about it. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Future me will also appreciate the clarity.

feature = "multithread"
),
feature = "multithread",
not(feature = "unknown-ci"),
Copy link
Owner

Choose a reason for hiding this comment

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

Wow, great catch!


// In the multithreaded version of `into_iter_mut` above, the `&mut` on the argument is indicating
// the parallel iterator is an exclusive reference. In the non-multithreaded case, the `&mut` is
// already part of `T` and specifying it will result in the argument being `&mut &mut T`.
Copy link
Owner

Choose a reason for hiding this comment

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

Awesome, thanks!

@GuillaumeGomez GuillaumeGomez merged commit b7d7f31 into GuillaumeGomez:master Mar 20, 2024
66 of 67 checks passed
GuillaumeGomez pushed a commit that referenced this pull request Apr 2, 2024
* Multithreaded component refresh
* Create utils::into_iter_mut to complement utils::into_iter.
* Fix invalid parallelization `cfg`, preventing multi-threading on 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.

None yet

2 participants