Several optimizations

- Write to pipes only when the value changes.
- Sleep when not reading key event or messages.

Fixes: https://github.com/sayanarijit/xplr/issues/10
pull/25/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 65ddb0ee4b
commit 09abda29a3

@ -15,7 +15,7 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap();
c.bench_function("focus next item", |b| {
@ -26,7 +26,7 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::FocusNext),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
})
});
@ -39,7 +39,7 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::FocusPrevious),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
})
});
@ -52,7 +52,7 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::FocusFirst),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
})
});
@ -65,7 +65,7 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::FocusLast),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
})
});
@ -78,14 +78,14 @@ fn criterion_benchmark(c: &mut Criterion) {
app::MsgIn::External(app::ExternalMsg::Back),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
.enqueue(app::Task::new(
1,
app::MsgIn::External(app::ExternalMsg::Enter),
None,
))
.possibly_mutate()
.mutate_or_sleep()
.unwrap()
})
});

@ -1,6 +1,6 @@
with import <nixpkgs> { };
# Update the src url, version and sha256 when new version
# Run nix-build and update the src url, version and sha256 when new version
rustPlatform.buildRustPackage rec {
name = "xplr";
@ -9,6 +9,6 @@ rustPlatform.buildRustPackage rec {
("https://github.com/sayanarijit/xplr/archive/refs/tags/v0.3.2.tar.gz");
buildInputs = [ cargo ];
checkPhase = "";
cargoSha256 = "sha256-IyaYkHXmqXziXNK6uYU+XNNWA8a8S8cuMxkopps/9kk=";
cargoSha256 = "0000000000000000000000000000000000000000000000000000";
}

@ -11,12 +11,12 @@ use std::collections::VecDeque;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::time::Duration;
pub const VERSION: &str = "v0.3.2"; // Update Cargo.toml and default.nix
pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";
pub const UNSUPPORTED_STR: &str = "???";
pub const UPGRADE_GUIDE_LINK: &str = "github.com/sayanarijit/xplr/wiki/Upgrade-Guide";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipe {
@ -755,10 +755,11 @@ impl App {
"incompatible configuration version in {}
You config version is : {}
Required version is : {}
Visit https://github.com/sayanarijit/xplr/wiki/Upgrade-Guide",
Visit {}",
config_file.to_string_lossy().to_string(),
config.version,
VERSION,
UPGRADE_GUIDE_LINK,
)
};
@ -808,18 +809,25 @@ impl App {
self.directory_buffer().and_then(|d| d.focused_node())
}
pub fn focused_node_str(&self) -> String {
self.focused_node()
.map(|n| format!("{}\n", n.absolute_path.clone()))
.unwrap_or_default()
}
pub fn enqueue(mut self, task: Task) -> Self {
self.tasks.push(task);
self
}
pub fn possibly_mutate(mut self) -> Result<Self> {
pub fn mutate_or_sleep(mut self) -> Result<Self> {
if let Some(task) = self.tasks.pop() {
match task.msg {
MsgIn::Internal(msg) => self.handle_internal(msg),
MsgIn::External(msg) => self.handle_external(msg, task.key),
}
} else {
std::thread::sleep(Duration::from_millis(5));
Ok(self)
}
}
@ -1281,6 +1289,10 @@ impl App {
&self.mode
}
pub fn mode_str(&self) -> String {
format!("{}\n", &self.mode.name)
}
/// Get a reference to the app's directory buffers.
pub fn directory_buffers(&self) -> &HashMap<String, DirectoryBuffer> {
&self.directory_buffers
@ -1302,7 +1314,7 @@ impl App {
}
/// Get a reference to the app's runtime path.
pub fn session_path(&self) -> &String {
pub fn session_path(&self) -> &str {
&self.session_path
}

@ -4,6 +4,7 @@ use crate::input::Key;
use crossterm::event::{self, Event};
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
pub fn keep_reading(tx_msg_in: Sender<Task>, rx_event_reader: Receiver<bool>) {
thread::spawn(move || {
@ -36,6 +37,8 @@ pub fn keep_reading(tx_msg_in: Sender<Task>, rx_event_reader: Receiver<bool>) {
.unwrap();
}
}
} else {
thread::sleep(Duration::from_millis(1));
}
}
});

@ -40,6 +40,22 @@ fn main() -> Result<()> {
let mut app = app::App::create(pwd)?;
if app.version() != &app.config().version {
let msg = format!(
"version mismatch, to update your config file to {}, visit {}",
app.version(),
app::UPGRADE_GUIDE_LINK,
);
tx_msg_in.send(app::Task::new(
0,
app::MsgIn::External(app::ExternalMsg::LogInfo(msg)),
None,
))?;
};
fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;
explorer::explore(
app.explorer_config().clone(),
app.pwd().clone(),
@ -73,12 +89,12 @@ fn main() -> Result<()> {
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
// Threads
auto_refresher::start_auto_refreshing(tx_msg_in.clone());
pipe_reader::keep_reading(app.pipe().msg_in.clone(), tx_msg_in.clone());
event_reader::keep_reading(tx_msg_in.clone(), rx_event_reader);
let mut last_pwd = app.pwd().clone();
let mut last_app = app.clone();
'outer: while result.is_ok() {
while let Some(msg) = app.pop_msg_out() {
match msg {
@ -112,33 +128,41 @@ fn main() -> Result<()> {
app::MsgOut::Refresh => {
app = app.refresh_selection()?;
if app.pwd() != &last_pwd {
if app.pwd() != last_app.pwd() {
explorer::explore(
app.explorer_config().clone(),
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path.clone()),
tx_msg_in.clone(),
);
last_pwd = app.pwd().to_owned();
};
// UI
terminal.draw(|f| ui::draw(f, &app, &hb))?;
let focused = app
.focused_node()
.map(|n| format!("{}\n", n.absolute_path.clone()))
.unwrap_or_default();
if app.focused_node() != last_app.focused_node() {
fs::write(&app.pipe().focus_out, app.focused_node_str())?;
};
let mode = format!("{}\n", &app.mode().name);
if app.selection() != last_app.selection() {
fs::write(&app.pipe().selection_out, app.selection_str())?;
};
fs::write(&app.pipe().focus_out, focused)?;
fs::write(&app.pipe().selection_out, app.selection_str())?;
fs::write(&app.pipe().mode_out, mode)?;
fs::write(&app.pipe().directory_nodes_out, app.directory_nodes_str())?;
fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;
fs::write(&app.pipe().logs_out, app.logs_str())?;
fs::write(&app.pipe().result_out, app.result_str())?;
if app.mode_str() != last_app.mode_str() {
fs::write(&app.pipe().mode_out, app.mode_str())?;
};
if app.directory_buffer() != last_app.directory_buffer() {
fs::write(&app.pipe().directory_nodes_out, app.directory_nodes_str())?;
};
if app.logs() != last_app.logs() {
fs::write(&app.pipe().logs_out, app.logs_str())?;
};
if app.result() != last_app.result() {
fs::write(&app.pipe().result_out, app.result_str())?;
}
}
app::MsgOut::Call(cmd) => {
@ -148,14 +172,8 @@ fn main() -> Result<()> {
execute!(terminal.backend_mut(), term::LeaveAlternateScreen)?;
terminal.show_cursor()?;
let pid = std::process::id().to_string();
let input_buffer = app.input_buffer().unwrap_or_default();
let focus_path = app
.focused_node()
.map(|n| n.absolute_path.clone())
.unwrap_or_default();
let focus_index = app
.directory_buffer()
.map(|d| d.focus)
@ -176,9 +194,9 @@ fn main() -> Result<()> {
.current_dir(app.pwd())
.env("XPLR_APP_VERSION", app.version())
.env("XPLR_CONFIG_VERSION", &app.config().version)
.env("XPLR_PID", pid)
.env("XPLR_PID", &app.pid().to_string())
.env("XPLR_INPUT_BUFFER", input_buffer)
.env("XPLR_FOCUS_PATH", focus_path)
.env("XPLR_FOCUS_PATH", app.focused_node_str())
.env("XPLR_FOCUS_INDEX", focus_index)
.env("XPLR_SESSION_PATH", session_path)
.env("XPLR_PIPE_MSG_IN", pipe_msg_in)
@ -212,21 +230,20 @@ fn main() -> Result<()> {
terminal.draw(|f| ui::draw(f, &app, &hb))?;
}
};
last_app = app.clone();
}
for task in rx_msg_in.try_iter() {
app = app.enqueue(task);
}
let (new_app, new_result) = match app.clone().possibly_mutate() {
let (new_app, new_result) = match app.clone().mutate_or_sleep() {
Ok(a) => (a, Ok(())),
Err(e) => (app, Err(e)),
};
app = new_app;
result = new_result;
// thread::sleep(Duration::from_millis(10));
}
term::disable_raw_mode()?;

@ -27,7 +27,8 @@ pub fn keep_reading(pipe: String, tx: Sender<Task>) {
}
});
fs::write(&pipe, "").unwrap();
};
thread::sleep(Duration::from_millis(10));
} else {
thread::sleep(Duration::from_millis(50));
}
});
}

Loading…
Cancel
Save