Optimize performance

```
Benchmarking focus next item: Collecting 100 samples in estimated 5.1972 s (126k itera                                                                                      focus next item         time:   [41.216 us 41.346 us 41.494 us]
                        change: [-28.669% -28.110% -27.551%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  4 (4.00%) high mild
  5 (5.00%) high severe

Benchmarking focus previous item: Collecting 100 samples in estimated 5.0576 s (116k i                                                                                      focus previous item     time:   [43.589 us 43.754 us 43.927 us]
                        change: [-29.506% -28.748% -28.039%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

Benchmarking focus first item: Collecting 100 samples in estimated 5.1765 s (116k iter                                                                                      focus first item        time:   [44.071 us 44.340 us 44.634 us]
                        change: [-26.739% -26.314% -25.885%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  8 (8.00%) high mild
  4 (4.00%) high severe

Benchmarking focus last item: Collecting 100 samples in estimated 5.1522 s (116k itera                                                                                      focus last item         time:   [43.950 us 44.214 us 44.541 us]
                        change: [-27.571% -26.953% -26.337%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
  5 (5.00%) high mild
  6 (6.00%) high severe

Benchmarking leave and enter directory: Collecting 100 samples in estimated 5.4863 s (                                                                                      leave and enter directory
                        time:   [96.645 us 96.915 us 97.234 us]
                        change: [-28.720% -27.224% -25.666%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  6 (6.00%) high mild
  3 (3.00%) high severe
```
pull/139/head
Arijit Basu 3 years ago committed by Arijit Basu
parent e9680d9abd
commit cd5bf81646

@ -1,6 +1,5 @@
use crate::config::Config;
use crate::config::Mode;
use crate::explorer;
use crate::input::Key;
use crate::ui::Layout;
use anyhow::{bail, Result};
@ -1298,6 +1297,7 @@ impl Command {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum MsgOut {
ExplorePwd,
ExplorePwdAsync,
ExploreParentsAsync,
Refresh,
@ -1700,13 +1700,9 @@ impl App {
Ok(self)
}
fn explore_pwd(self) -> Result<Self> {
let dirbuf = explorer::explore_sync(
self.explorer_config().clone(),
self.pwd().clone(),
self.focused_node().map(|n| n.absolute_path().clone()),
)?;
self.add_directory(dirbuf.parent().clone(), dirbuf)
fn explore_pwd(mut self) -> Result<Self> {
self.msg_out.push_back(MsgOut::ExplorePwd);
Ok(self)
}
fn explore_pwd_async(mut self) -> Result<Self> {
@ -1805,18 +1801,13 @@ impl App {
}
fn change_directory(mut self, dir: &str) -> Result<Self> {
if PathBuf::from(dir).is_dir() {
match env::set_current_dir(dir) {
Ok(()) => {
self.pwd = dir.to_owned();
self.history = self.history.push(self.pwd.clone());
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}
Err(e) => self.log_error(e.to_string()),
match env::set_current_dir(dir) {
Ok(()) => {
self.pwd = dir.to_owned();
self.history = self.history.push(self.pwd.clone());
self.explore_pwd_async()
}
} else {
Ok(self)
Err(e) => self.log_error(e.to_string()),
}
}
@ -1845,7 +1836,7 @@ impl App {
.map(|p| p.to_owned())
.unwrap_or(self.pwd);
env::set_current_dir(&self.pwd)?;
self.refresh()
self.explore_pwd_async()
}
fn next_visited_path(mut self) -> Result<Self> {
@ -1856,7 +1847,7 @@ impl App {
.map(|p| p.to_owned())
.unwrap_or(self.pwd);
env::set_current_dir(&self.pwd)?;
self.refresh()
self.explore_pwd_async()
}
fn buffer_input(mut self, input: &str) -> Result<Self> {

@ -46,23 +46,29 @@ pub fn explore_async(
config: ExplorerConfig,
parent: String,
focused_path: Option<String>,
tx: Sender<Task>,
tx_msg_in: Sender<Task>,
tx_pwd_watcher: Sender<String>,
) {
thread::spawn(move || {
explore_sync(config, parent.clone(), focused_path)
.map(|dirbuf| {
tx.send(Task::new(
MsgIn::Internal(InternalMsg::AddDirectory(parent, dirbuf)),
None,
))
.unwrap_or_default();
.map(|buf| {
tx_pwd_watcher
.send(buf.parent().clone())
.unwrap_or_default();
tx_msg_in
.send(Task::new(
MsgIn::Internal(InternalMsg::AddDirectory(parent.clone(), buf)),
None,
))
.unwrap_or_default();
})
.unwrap_or_else(|e| {
tx.send(Task::new(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap_or_default();
tx_msg_in
.send(Task::new(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap_or_default();
})
});
}
@ -71,16 +77,24 @@ pub fn explore_recursive_async(
config: ExplorerConfig,
parent: String,
focused_path: Option<String>,
tx: Sender<Task>,
tx_msg_in: Sender<Task>,
tx_pwd_watcher: Sender<String>,
) {
let path = PathBuf::from(&parent);
explore_async(config.clone(), parent, focused_path, tx.clone());
explore_async(
config.clone(),
parent,
focused_path,
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
if let Some(grand_parent) = path.parent() {
explore_recursive_async(
config,
grand_parent.to_string_lossy().to_string(),
path.file_name().map(|f| f.to_string_lossy().to_string()),
tx,
tx_msg_in,
tx_pwd_watcher,
);
}
}

@ -17,6 +17,10 @@ pub fn keep_watching(
thread::spawn(move || loop {
if let Ok(new_pwd) = rx_pwd_watcher.try_recv() {
pwd = PathBuf::from(new_pwd);
last_modified = pwd
.metadata()
.and_then(|m| m.modified())
.unwrap_or(last_modified);
} else {
pwd.metadata()
.and_then(|m| m.modified())
@ -26,13 +30,13 @@ pub fn keep_watching(
tx_msg_in.send(Task::new(msg, None)).unwrap_or_default();
last_modified = modified;
} else {
thread::sleep(Duration::from_millis(1000));
thread::sleep(Duration::from_secs(1));
};
})
.unwrap_or_else(|e| {
let msg = MsgIn::External(ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(Task::new(msg, None)).unwrap_or_default();
thread::sleep(Duration::from_millis(1000));
thread::sleep(Duration::from_secs(1));
})
}
});

@ -75,6 +75,7 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.pwd().clone(),
focused_path,
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
let mut hb = Handlebars::new();
@ -156,12 +157,35 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
terminal.clear()?;
}
app::MsgOut::ExplorePwd => {
match explorer::explore_sync(
app.explorer_config().clone(),
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
) {
Ok(buf) => {
tx_pwd_watcher.send(buf.parent().clone())?;
let msg = app::MsgIn::Internal(app::InternalMsg::AddDirectory(
buf.parent().clone(),
buf,
));
tx_msg_in.send(app::Task::new(msg, None))?;
}
Err(e) => {
let msg =
app::MsgIn::External(app::ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(app::Task::new(msg, None))?;
}
}
}
app::MsgOut::ExplorePwdAsync => {
explorer::explore_async(
app.explorer_config().clone(),
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
}
@ -171,20 +195,12 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
tx_pwd_watcher.send(app.pwd().clone())?;
}
app::MsgOut::Refresh => {
if app.pwd() != last_app.pwd() {
tx_pwd_watcher.send(app.pwd().clone())?;
explorer::explore_recursive_async(
app.explorer_config().clone(),
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
tx_msg_in.clone(),
);
};
// UI
terminal.draw(|f| ui::draw(f, &app, &hb))?;
}

Loading…
Cancel
Save