Skip to content

Commit

Permalink
perf: eager load some data to improve performance (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Oct 26, 2024
1 parent b475f02 commit 7717232
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 38 deletions.
25 changes: 14 additions & 11 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl Display for BackendType {
static TOOLS: Mutex<Option<BackendMap>> = Mutex::new(None);

fn load_tools() -> BackendMap {
if let Some(backends) = TOOLS.lock().unwrap().as_ref() {
let mut memo_tools = TOOLS.lock().unwrap();
if let Some(backends) = &*memo_tools {
return backends.clone();
}
time!("load_tools: start");
Expand Down Expand Up @@ -109,7 +110,7 @@ fn load_tools() -> BackendMap {
.into_iter()
.map(|plugin| (plugin.id().to_string(), plugin))
.collect();
*TOOLS.lock().unwrap() = Some(tools.clone());
*memo_tools = Some(tools.clone());
time!("load_tools done");
tools
}
Expand Down Expand Up @@ -351,19 +352,21 @@ pub trait Backend: Debug + Send + Sync {
None
}
fn ensure_dependencies_installed(&self) -> eyre::Result<()> {
trace!("Ensuring dependencies installed for {}", self.id());
let deps = self
.get_all_dependencies(&ToolRequest::System(self.id().into()))?
.into_iter()
.collect::<HashSet<_>>();
let config = Config::get();
let ts = config.get_tool_request_set()?.filter_by_tool(&deps);
if !ts.missing_tools().is_empty() {
bail!(
"Dependency {} not installed for {}",
deps.iter().map(|d| d.to_string()).join(", "),
self.id()
);
if !deps.is_empty() {
trace!("Ensuring dependencies installed for {}", self.id());
let config = Config::get();
let ts = config.get_tool_request_set()?.filter_by_tool(&deps);
if !ts.missing_tools().is_empty() {
bail!(
"Dependency {} not installed for {}",
deps.iter().map(|d| d.to_string()).join(", "),
self.id()
);
}
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ impl Commands {
pub static CLI: Lazy<clap::Command> = Lazy::new(|| {
Commands::augment_subcommands(
clap::Command::new("mise")
.version(version::VERSION.to_string())
.about(env!("CARGO_PKG_DESCRIPTION"))
.author("Jeff Dickey <@jdx>")
.long_about(LONG_ABOUT)
Expand All @@ -196,7 +195,8 @@ pub static CLI: Lazy<clap::Command> = Lazy::new(|| {
.arg(args::QUIET_ARG.clone())
.arg(args::TRACE_ARG.clone())
.arg(args::VERBOSE_ARG.clone())
.arg(args::YES_ARG.clone()),
.arg(args::YES_ARG.clone())
.version(version::VERSION.to_string()),
)
});

Expand Down
6 changes: 5 additions & 1 deletion src/config/config_file/legacy_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::cli::args::BackendArg;
use crate::config::config_file::ConfigFile;
use crate::toolset::{ToolRequest, ToolRequestSet, ToolSource};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct LegacyVersionFile {
path: PathBuf,
tools: ToolRequestSet,
Expand Down Expand Up @@ -74,4 +74,8 @@ impl ConfigFile for LegacyVersionFile {
fn to_tool_request_set(&self) -> Result<ToolRequestSet> {
Ok(self.tools.clone())
}

fn clone_box(&self) -> Box<dyn ConfigFile> {
Box::new(self.clone())
}
}
4 changes: 4 additions & 0 deletions src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ impl ConfigFile for MiseToml {
fn task_config(&self) -> &TaskConfig {
&self.task_config
}

fn clone_box(&self) -> Box<dyn ConfigFile> {
Box::new(self.clone())
}
}

impl Debug for MiseToml {
Expand Down
1 change: 1 addition & 0 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub trait ConfigFile: Debug + Send + Sync {
static DEFAULT_TASK_CONFIG: Lazy<TaskConfig> = Lazy::new(TaskConfig::default);
&DEFAULT_TASK_CONFIG
}
fn clone_box(&self) -> Box<dyn ConfigFile>;
}

impl dyn ConfigFile {
Expand Down
8 changes: 6 additions & 2 deletions src/config/config_file/tool_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::toolset::{ToolRequest, ToolRequestSet, ToolSource};
// shfmt 3.6.0

/// represents asdf's .tool-versions file
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ToolVersions {
context: Context,
path: PathBuf,
Expand All @@ -29,7 +29,7 @@ pub struct ToolVersions {
tools: ToolRequestSet,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
struct ToolVersionPlugin {
orig_name: String,
versions: Vec<String>,
Expand Down Expand Up @@ -201,6 +201,10 @@ impl ConfigFile for ToolVersions {
fn to_tool_request_set(&self) -> eyre::Result<ToolRequestSet> {
Ok(self.tools.clone())
}

fn clone_box(&self) -> Box<dyn ConfigFile> {
Box::new(self.clone())
}
}

#[cfg(test)]
Expand Down
5 changes: 4 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,10 @@ pub fn load_config_paths(config_filenames: &[String]) -> Vec<PathBuf> {
config_files.extend(global_config_files());
config_files.extend(system_config_files());

config_files.into_iter().unique().collect()
config_files
.into_iter()
.unique_by(|p| file::desymlink_path(p))
.collect()
}

pub fn is_global_config(path: &Path) -> bool {
Expand Down
9 changes: 8 additions & 1 deletion src/config/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::{system_config_files, DEFAULT_CONFIG_FILENAMES};
use crate::file::FindUp;
use crate::{config, dirs, env, file};
use crate::{config, dirs, eager, env, file};
#[allow(unused_imports)]
use confique::env::parse::{list_by_colon, list_by_comma};
use confique::{Config, Partial};
Expand Down Expand Up @@ -213,6 +213,9 @@ impl Settings {
if let Some(cd) = m.get_one::<PathBuf>("cd") {
s.cd = Some(cd.clone());
}
if let Some(profile) = m.get_one::<String>("profile") {
s.profile = Some(profile.clone());
}
if let Some(true) = m.get_one::<bool>("yes") {
s.yes = Some(true);
}
Expand Down Expand Up @@ -265,6 +268,10 @@ impl Settings {
fn parse_settings_file(path: &PathBuf) -> Result<SettingsPartial> {
let raw = file::read_to_string(path)?;
let settings_file: SettingsFile = toml::from_str(&raw)?;

// eagerly parse the file as a config file in the background for later
eager::CONFIG_FILES.lock().unwrap().push(path.clone());

Ok(settings_file.settings)
}

Expand Down
26 changes: 13 additions & 13 deletions src/eager.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use xx::regex;
use crate::cli::version::VERSION;
use crate::plugins::VERSION_REGEX;
use once_cell::sync::Lazy;
use std::path::PathBuf;
use std::sync::Mutex;

/// initializes slow parts of mise eagerly in the background
pub fn early_init() {
rayon::spawn(|| {
regex!("");
}); // initialize regex library
let _ = &*VERSION_REGEX;
});
rayon::spawn(|| {
let _ = &*VERSION;
})
}

pub static CONFIG_FILES: Lazy<Mutex<Vec<PathBuf>>> = Lazy::new(|| Mutex::new(Vec::new()));

/// run after SETTING has been loaded
pub fn post_settings() {
// if std::env::var("EAGER").is_err() {
// return;
// }
// rayon::spawn(|s| {
// s.spawn(|_| {
// // let _ = load_toolset();
// });
// });
}
pub fn post_settings() {}
17 changes: 10 additions & 7 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,17 @@ pub fn split_file_name(path: &Path) -> (String, String) {
}

pub fn same_file(a: &Path, b: &Path) -> bool {
let canonicalize = |p: &Path| p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
if canonicalize(a) == canonicalize(b) {
return true;
}
if let (Ok(a), Ok(b)) = (fs::read_link(a), fs::read_link(b)) {
return canonicalize(&a) == canonicalize(&b);
desymlink_path(a) == desymlink_path(b)
}

pub fn desymlink_path(p: &Path) -> PathBuf {
if let Ok(target) = fs::read_link(p) {
target
.canonicalize()
.unwrap_or_else(|_| target.to_path_buf())
} else {
p.canonicalize().unwrap_or_else(|_| p.to_path_buf())
}
false
}

#[cfg(test)]
Expand Down

0 comments on commit 7717232

Please sign in to comment.