-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement multithreading in UI for better performance
Now all UI frontends follow an update loop and input as logging is sent through mpsc messaging. Replacing the slow io write waiting in between commands with way faster mpsc send waiting. Additionally refactored the way frontends work by removing the trait Frontend all together. As now they don't require to follow that architecture and can just receive mpsc messages through composition rather than trait "inheritance". TLDR, this change will prevent IO bottlenecking when logging to stdout/performing TUI rendering.
- Loading branch information
1 parent
4611c54
commit 44b9334
Showing
10 changed files
with
354 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::Action; | ||
|
||
use super::MessageColor; | ||
|
||
pub struct UIWriteHandle { | ||
messages: Mutex<UnboundedSender<(String, MessageColor)>>, | ||
actions: Mutex<UnboundedSender<Action>>, | ||
progressbar: Mutex<UnboundedSender<f32>>, | ||
exit: Mutex<UnboundedSender<()>>, | ||
|
||
/// The frontend will need to send to this receiver through [UIReadHandle::exit_finish] | ||
/// once the [Self::exit] procedure is finished. | ||
pub exit_finish: Mutex<UnboundedReceiver<()>>, | ||
} | ||
|
||
pub struct UIReadHandle { | ||
pub messages: UnboundedReceiver<(String, MessageColor)>, | ||
pub actions: UnboundedReceiver<Action>, | ||
pub progressbar: UnboundedReceiver<f32>, | ||
pub exit: UnboundedReceiver<()>, | ||
pub exit_finish: Mutex<UnboundedSender<()>>, | ||
} | ||
|
||
pub fn generate_message_pair() -> (UIWriteHandle, UIReadHandle) { | ||
let (mw, mr) = mpsc::unbounded_channel(); | ||
let (aw, ar) = mpsc::unbounded_channel(); | ||
let (pw, pr) = mpsc::unbounded_channel(); | ||
let (ew, er) = mpsc::unbounded_channel(); | ||
let (efw, efr) = mpsc::unbounded_channel(); | ||
|
||
( | ||
UIWriteHandle { | ||
messages: mw.into(), | ||
actions: aw.into(), | ||
progressbar: pw.into(), | ||
exit: ew.into(), | ||
exit_finish: efr.into(), | ||
}, | ||
UIReadHandle { | ||
messages: mr, | ||
actions: ar, | ||
progressbar: pr, | ||
exit: er, | ||
exit_finish: efw.into(), | ||
}, | ||
) | ||
} | ||
|
||
impl UIWriteHandle { | ||
pub async fn display_message(&self, message: String, color: &MessageColor) { | ||
self.messages | ||
.lock() | ||
.await | ||
.send((message, color.clone())) | ||
.unwrap(); | ||
} | ||
|
||
pub async fn display_action(&self, action: &Action) { | ||
self.actions.lock().await.send(action.clone()).unwrap(); | ||
} | ||
|
||
pub async fn set_progressbar(&self, percentage: f32) { | ||
self.progressbar.lock().await.send(percentage).unwrap(); | ||
} | ||
|
||
pub async fn exit(&self) { | ||
self.exit.lock().await.send(()).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.