From a5c8ca3796b0d44ca277292d3309705cf6d1397a Mon Sep 17 00:00:00 2001 From: vyfor Date: Wed, 1 Jan 2025 11:53:43 +0500 Subject: [PATCH] perf: handle logic responsible for broadcasting the ready event more efficiently --- src/ipc/discord/client.rs | 2 +- src/ipc/discord/platform/unix.rs | 17 ++++++++++++----- src/ipc/discord/platform/windows.rs | 12 ++++++++++-- src/messages/events/server/ready.rs | 13 +------------ 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ipc/discord/client.rs b/src/ipc/discord/client.rs index 42c45fb1..14eff78d 100644 --- a/src/ipc/discord/client.rs +++ b/src/ipc/discord/client.rs @@ -24,7 +24,7 @@ pub struct RichClient { #[cfg(not(target_os = "windows"))] pub write_pipe: Option, pub pid: u32, - pub is_ready: AtomicBool, + pub is_ready: Arc, pub thread_handle: Option>, pub is_reconnecting: Arc, } diff --git a/src/ipc/discord/platform/unix.rs b/src/ipc/discord/platform/unix.rs index 0ab4a5ab..b2eb2402 100644 --- a/src/ipc/discord/platform/unix.rs +++ b/src/ipc/discord/platform/unix.rs @@ -2,6 +2,7 @@ use std::env::var; use std::io::{self, Read, Write}; use std::net::Shutdown; use std::os::unix::net::UnixStream; +use std::sync::atomic::Ordering; use std::sync::mpsc::Sender; use std::sync::Arc; @@ -51,7 +52,7 @@ impl Connection for RichClient { read_pipe: Some(read_pipe), write_pipe: Some(pipe), pid: std::process::id(), - is_ready: false.into(), + is_ready: Arc::new(false.into()), thread_handle: None, is_reconnecting: Arc::new(false.into()), }); @@ -82,6 +83,7 @@ impl Connection for RichClient { fn start_read_thread(&mut self, tx: Sender) -> crate::Result<()> { if let Some(mut read_pipe) = self.read_pipe.take() { let client_id = self.client_id; + let is_ready = self.is_ready.clone(); let handle = std::thread::spawn(move || { let mut buf = [0u8; 8192]; @@ -128,10 +130,15 @@ impl Connection for RichClient { .ok(); break; } - tx.send(server_event!( - 0, Ready - )) - .ok(); + if !is_ready.swap( + true, + Ordering::SeqCst, + ) { + tx.send(server_event!( + 0, Ready + )) + .ok(); + } } Opcode::Close => { tx.send(local_event!( diff --git a/src/ipc/discord/platform/windows.rs b/src/ipc/discord/platform/windows.rs index bb434861..7381fe78 100644 --- a/src/ipc/discord/platform/windows.rs +++ b/src/ipc/discord/platform/windows.rs @@ -2,6 +2,7 @@ use std::ffi::OsStr; use std::fs::File; use std::os::windows::ffi::OsStrExt; use std::os::windows::io::{AsRawHandle, FromRawHandle}; +use std::sync::atomic::Ordering; use std::sync::mpsc::Sender; use std::sync::Arc; use std::{io, ptr}; @@ -53,7 +54,7 @@ impl Connection for RichClient { client_id, pipe: Some(Arc::new(pipe)), pid: std::process::id(), - is_ready: false.into(), + is_ready: Arc::new(false.into()), thread_handle: None, is_reconnecting: Arc::new(false.into()), }; @@ -76,6 +77,7 @@ impl Connection for RichClient { if let Some(pipe) = self.pipe.as_ref() { let pipe = pipe.clone(); let client_id = self.client_id; + let is_ready = self.is_ready.clone(); let handle = std::thread::spawn(move || { let mut buf = [0u8; 8192]; @@ -167,8 +169,14 @@ impl Connection for RichClient { .ok(); break; } - tx.send(server_event!(0, Ready)) + if !is_ready + .swap(true, Ordering::SeqCst) + { + tx.send(server_event!( + 0, Ready + )) .ok(); + } } Opcode::Close => { tx.send(local_event!( diff --git a/src/messages/events/server/ready.rs b/src/messages/events/server/ready.rs index c8390637..504ac85e 100644 --- a/src/messages/events/server/ready.rs +++ b/src/messages/events/server/ready.rs @@ -1,5 +1,3 @@ -use std::sync::atomic::Ordering; - use crate::ipc::pipe::PipeServerImpl; use crate::messages::events::event::{EventContext, OnEvent}; use crate::protocol::msgpack::serialize::Serialize; @@ -11,16 +9,7 @@ pub struct ReadyEvent; impl OnEvent for ReadyEvent { fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { - if !ctx - .cord - .rich_client - .read() - .unwrap() - .is_ready - .swap(true, Ordering::SeqCst) - { - ctx.cord.pipe.broadcast(&MsgPack::serialize(&self)?)?; - } + ctx.cord.pipe.broadcast(&MsgPack::serialize(&self)?)?; Ok(()) }