Repository: bevyengine/bevy
File: crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs (Rust)

Code before:
            let used_mem = system.used_memory() as f64;
            let system_mem_usage = used_mem / total_mem * 100.0;

            let process_mem_usage = system
                .process(pid)
                .map(|p| p.memory() as f64 * BYTES_TO_GIB)
                .unwrap_or(0.0);

            let process_cpu_usage = system
                .process(pid)
                .map(|p| p.cpu_usage() as f64 / system.cpus().len() as f64)
                .unwrap_or(0.0);

            Self {
                system_cpu_usage,
                system_mem_usage,
                process_cpu_usage,
                process_mem_usage,
            }
        }
    }

    #[derive(Resource)]
    struct SysinfoTask {
        receiver: Mutex<Receiver<SysinfoRefreshData>>,
        waker: Arc<Mutex<Option<Waker>>>,
    }

    struct DiagnosticTask {
        system: System,
        last_refresh: Instant,
        sender: Sender<SysinfoRefreshData>,
        waker: Arc<Mutex<Option<Waker>>>,
    }

    impl DiagnosticTask {
        fn new(sender: Sender<SysinfoRefreshData>) -> Self {
            Self {
                system: System::new_with_specifics(
                    RefreshKind::nothing()
                        .with_cpu(CpuRefreshKind::nothing().with_cpu_usage())
                        .with_memory(MemoryRefreshKind::everything()),
                ),
                last_refresh: Instant::now() - sysinfo::MINIMUM_CPU_UPDATE_INTERVAL,
                sender,
                waker: Arc::default(),
            }
        }

        fn update_waker(&mut self, cx: &mut Context<'_>) {
            let mut waker = self.waker.lock().unwrap();

Review comment (suggestion):
```suggestion
        receiver: SyncCell<Receiver<SysinfoRefreshData>>,
```

This does not need to be accessed from more than one location at a time. We can skip the overhead of Mutex locking.

Code after:
        fn new(system: &mut System) -> Self {
            let pid = sysinfo::get_current_pid().expect("Failed to get current process ID");
            system.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true);

            system.refresh_cpu_specifics(CpuRefreshKind::nothing().with_cpu_usage());
            system.refresh_memory();

            let system_cpu_usage = system.global_cpu_usage().into();
            let total_mem = system.total_memory() as f64;
            let used_mem = system.used_memory() as f64;
            let system_mem_usage = used_mem / total_mem * 100.0;

            let process_mem_usage = system
                .process(pid)
                .map(|p| p.memory() as f64 * BYTES_TO_GIB)
                .unwrap_or(0.0);

            let process_cpu_usage = system
                .process(pid)
                .map(|p| p.cpu_usage() as f64 / system.cpus().len() as f64)
                .unwrap_or(0.0);

            Self {
                system_cpu_usage,
                system_mem_usage,
                process_cpu_usage,
                process_mem_usage,
            }
        }
    }

    #[derive(Resource)]
    struct SysinfoTask {
        _task: Task<()>,
        receiver: SyncCell<Receiver<SysinfoRefreshData>>,
        waker: Arc<AtomicWaker>,
    }

    struct DiagnosticTask {
        system: System,
        last_refresh: Instant,
        sender: Sender<SysinfoRefreshData>,
        waker: Arc<AtomicWaker>,
    }

    impl DiagnosticTask {
        fn new(sender: Sender<SysinfoRefreshData>) -> Self {
            Self {
                system: System::new_with_specifics(
                    RefreshKind::nothing()
                        .with_cpu(CpuRefreshKind::nothing().with_cpu_usage())