From e9680d9abd2d91b0c3e3aaa0dabe94c97f1a6c71 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Tue, 11 May 2021 02:06:15 +0530 Subject: [PATCH] Optimize release binary size Also improve the $PWD watcher logic. --- Cargo.toml | 6 ++++++ src/pwd_watcher.rs | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cef54d4..6021047 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,9 @@ criterion = "0.3" [[bench]] name = "criterion" harness = false + +# https://github.com/johnthagen/min-sized-rust +[profile.release] +lto = true +codegen-units = 1 +panic = 'abort' diff --git a/src/pwd_watcher.rs b/src/pwd_watcher.rs index 86c2ea6..1d6c8a5 100644 --- a/src/pwd_watcher.rs +++ b/src/pwd_watcher.rs @@ -5,7 +5,6 @@ use std::path::PathBuf; use std::sync::mpsc::{Receiver, Sender}; use std::thread; use std::time::Duration; -use std::time::SystemTime; pub fn keep_watching( pwd: &str, @@ -13,27 +12,28 @@ pub fn keep_watching( rx_pwd_watcher: Receiver, ) -> Result<()> { let mut pwd = PathBuf::from(pwd); - let mut last_modified = pwd - .metadata() - .and_then(|m| m.modified()) - .unwrap_or_else(|_| SystemTime::now()); + let mut last_modified = pwd.metadata().and_then(|m| m.modified())?; thread::spawn(move || loop { if let Ok(new_pwd) = rx_pwd_watcher.try_recv() { pwd = PathBuf::from(new_pwd); } else { - let modified = pwd - .metadata() + pwd.metadata() .and_then(|m| m.modified()) - .unwrap_or_else(|_| SystemTime::now()); - - if modified != last_modified { - let msg = MsgIn::External(ExternalMsg::ExplorePwdAsync); - tx_msg_in.send(Task::new(msg, None)).unwrap_or_default(); - last_modified = modified; - } else { - thread::sleep(Duration::from_millis(1000)); - }; + .map(|modified| { + if modified != last_modified { + let msg = MsgIn::External(ExternalMsg::ExplorePwdAsync); + tx_msg_in.send(Task::new(msg, None)).unwrap_or_default(); + last_modified = modified; + } else { + thread::sleep(Duration::from_millis(1000)); + }; + }) + .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)); + }) } }); Ok(())