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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
ComponentInner, ComponentsInner, CpuInner, NetworkDataInner, NetworksInner, ProcessInner,
SystemInner, UserInner,
utils::into_iter_mut, ComponentInner, ComponentsInner, CpuInner, NetworkDataInner,
NetworksInner, ProcessInner, SystemInner, UserInner,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -3822,9 +3822,13 @@ impl Components {
/// components.refresh();
/// ```
pub fn refresh(&mut self) {
for component in self.list_mut() {
component.refresh();
}
#[cfg(all(
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
use rayon::iter::ParallelIterator;
into_iter_mut(self.list_mut()).for_each(|component| component.refresh());
}

/// The component list will be emptied then completely recomputed.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod common;
mod debug;
#[cfg(feature = "serde")]
mod serde;
mod utils;
pub(crate) mod utils;

/// This function is only used on Linux targets, on the other platforms it does nothing and returns
/// `false`.
Expand Down
56 changes: 43 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
// Take a look at the license at the top of the repository in the LICENSE file.

/// Converts the value into a parallel iterator (if the multi-thread feature is enabled).
/// Converts the value into a parallel iterator if the `multithread` feature is enabled.
/// Uses the `rayon::iter::IntoParallelIterator` trait.
#[cfg(all(
all(
any(target_os = "macos", target_os = "windows", target_os = "freebsd",),
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!

not(all(target_os = "macos", feature = "apple-sandbox")),
not(feature = "unknown-ci")
))]
#[allow(dead_code)]
pub(crate) fn into_iter<T>(val: T) -> T::Iter
where
T: rayon::iter::IntoParallelIterator,
{
val.into_par_iter()
}

/// Converts the value into a sequential iterator (if the multithread feature is disabled).
/// Converts the value into a sequential iterator if the `multithread` feature is disabled.
/// Uses the `std::iter::IntoIterator` trait.
#[cfg(any(
not(feature = "multithread"),
feature = "unknown-ci",
all(target_os = "macos", feature = "apple-sandbox")
))]
#[allow(dead_code)]
pub(crate) fn into_iter<T>(val: T) -> T::IntoIter
where
T: IntoIterator,
{
val.into_iter()
}

/// Converts the value into a parallel mutable iterator if the `multithread` feature is enabled.
/// Uses the `rayon::iter::IntoParallelRefMutIterator` trait.
#[cfg(all(
all(
any(target_os = "macos", target_os = "windows", target_os = "freebsd",),
not(feature = "multithread")
),
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox"))
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
pub(crate) fn into_iter<T>(val: T) -> T::IntoIter
pub(crate) fn into_iter_mut<'a, T>(
val: &'a mut T,
) -> <T as rayon::iter::IntoParallelRefMutIterator<'a>>::Iter
where
T: rayon::iter::IntoParallelRefMutIterator<'a> + ?Sized,
{
val.par_iter_mut()
}

// 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!


/// Converts the value into a sequential mutable iterator if the `multithread` feature is disabled.
/// Uses the `std::iter::IntoIterator` trait.
#[cfg(any(
not(feature = "multithread"),
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.

where
T: IntoIterator,
{
Expand Down