Performance concerns

This commit is contained in:
Sam Hedin 2020-06-14 14:56:11 +02:00
parent 97bad0e4de
commit b5923d0cd3

View File

@ -84,7 +84,7 @@ impl DirectorySpecificEnvironment {
while let Some(wdir) = working_dir {
if self.allowed_directories.contains(wdir) {
let toml_doc = std::fs::read_to_string(wdir.join(".nu-env").as_path())
.unwrap_or_else(|_| r#"[env]"#.to_string())
.unwrap_or_else(|_| "[env]".to_string())
.parse::<toml::Value>()?;
toml_doc
@ -101,12 +101,13 @@ impl DirectorySpecificEnvironment {
if let Some(val) = std::env::var_os(k) {
self.overwritten_env_values
.entry(wdir.to_path_buf())
.or_insert(IndexMap::new())
.or_insert_with(|| IndexMap::new())
.insert(k.clone(), val);
} else { //Otherwise, we just track that we added it here
} else {
//Otherwise, we just track that we added it here
self.added_env_vars
.entry(wdir.to_path_buf())
.or_insert(IndexSet::new())
.or_insert_with(|| IndexSet::new())
.insert(k.clone());
}
}
@ -126,29 +127,26 @@ impl DirectorySpecificEnvironment {
pub fn env_vars_to_delete(&mut self) -> Result<Vec<String>> {
let current_dir = std::env::current_dir()?;
let mut new_added_env_vars: IndexMap<PathBuf, IndexSet<String>> = IndexMap::new();
//Gather up all environment variables that should be deleted.
//If we are not in a directory or one of its subdirectories, mark the env_vals it maps to for removal.
let vars_to_delete = self.added_env_vars.iter().fold(
Vec::new(),
|mut vars_to_delete, (directory, env_vars)| {
let mut working_dir = Some(current_dir.as_path());
let mut new_added_env_vars = IndexMap::new();
let mut working_dir = Some(current_dir.as_path());
while let Some(wdir) = working_dir {
if wdir == directory {
if let Some(vars_added_by_this_directory) = self.added_env_vars.get(wdir) { //If we are still in a directory, we should continue to track the vars it added.
new_added_env_vars.insert(wdir.to_path_buf(), vars_added_by_this_directory.clone());
}
return vars_to_delete;
} else {
working_dir = working_dir.expect("Root directory has no parent").parent();
}
while let Some(wdir) = working_dir {
if let Some(vars_added_by_this_directory) = self.added_env_vars.get(wdir) {
//If we are still in a directory, we should continue to track the vars it added.
new_added_env_vars.insert(wdir.to_path_buf(), vars_added_by_this_directory.clone());
}
working_dir = working_dir.expect("Root directory has no parent").parent();
}
//Gather up all environment variables that should be deleted.
let mut vars_to_delete = vec![];
self.added_env_vars.iter().for_each(|(dir, added_keys)| {
if !new_added_env_vars.contains_key(dir) {
for k in added_keys {
vars_to_delete.push(k.clone());
}
//only delete vars from directories we are not in
vars_to_delete.extend(env_vars.clone());
vars_to_delete
},
);
}
});
self.added_env_vars = new_added_env_vars;
Ok(vars_to_delete)
}