the directories crate spawns a lot of children! stop using it

The `directories` crate unconditionally and eagerly tries to spawn
`xdg-user-dir` for all possible directories that it might be asked
about.  This is relatively expensive and undesirable.

Shift to the `dirs` crate instead, which doesn't seem to need
to run `xdg-user-dir` at all.

Maintain these probed dirs as singletons via lazy_static.
This commit is contained in:
Wez Furlong committed 2019-03-22 20:41:50 -07:00
1 parent bd1b66e758
commit 6575f50ffc
2 files changed
+29 -25

No files matched your search

+2 -1
View File
@@ -16,7 +16,7 @@ serde = "~1.0"
serde_derive = "~1.0"
toml = "~0.4"
unicode-width = "~0.1"
directories = "~1.0"
dirs = "~1.0"
font-loader = "0.8"
rusttype = "0.7"
clipboard = "0.5"
@@ -32,6 +32,7 @@ promise = { path = "promise" }
base91 = { path = "base91" }
varu64 = "0.6"
bincode = "1.1"
lazy_static = "1.3"
[target.'cfg(unix)'.dependencies]
harfbuzz-sys = { git = "https://github.com/wez/rust-harfbuzz", branch="coretext" }
+27 -24
View File
@@ -3,12 +3,13 @@
use crate::font::FontSystemSelection;
use crate::frontend::FrontEndSelection;
use crate::{get_shell, Command};
use directories::{ProjectDirs, UserDirs};
use failure::{err_msg, Error};
use lazy_static::lazy_static;
use std;
use std::ffi::OsStr;
use std::fs;
use std::io::prelude::*;
use std::path::PathBuf;
use term;
use term::color::RgbColor;
use termwiz::hyperlink;
@@ -288,25 +289,32 @@ pub struct StyleRule {
pub font: TextStyle,
}
fn project_dirs() -> Result<ProjectDirs, Error> {
ProjectDirs::from("org", "wez", "wezterm").ok_or_else(|| err_msg("can't find project dirs"))
fn compute_runtime_dir() -> Result<PathBuf, Error> {
if let Some(runtime) = dirs::runtime_dir() {
return Ok(runtime.join("wezterm"));
}
let home = dirs::home_dir().ok_or_else(|| err_msg("can't find home dir"))?;
Ok(home.join(".local/share/wezterm"))
}
lazy_static! {
static ref HOME_DIR: PathBuf = dirs::home_dir().expect("can't find HOME dir");
static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap();
}
impl Config {
pub fn load() -> Result<Self, Error> {
let project_dirs = project_dirs()?;
let dirs = UserDirs::new().ok_or_else(|| err_msg("can't find home dir"))?;
let home = dirs.home_dir();
// Note that the directories crate has methods for locating project
// specific config directories, but only returns one of them, not
// multiple, so we use it for one of the paths, and fill in the fallbacks
// manually here. This will likely result in trying the same path
// a couple of times, but that's harmless.
// multiple. In addition, it spawns a lot of subprocesses,
// so we do this bit "by-hand"
let paths = [
project_dirs.config_dir().join("wezterm.toml"),
home.join(".config").join("wezterm").join("wezterm.toml"),
home.join(".wezterm.toml"),
HOME_DIR
.join(".config")
.join("wezterm")
.join("wezterm.toml"),
HOME_DIR.join(".wezterm.toml"),
];
for p in &paths {
@@ -323,29 +331,24 @@ impl Config {
let cfg: Self = toml::from_str(&s)
.map_err(|e| format_err!("Error parsing TOML from {}: {:?}", p.display(), e))?;
return Ok(cfg.compute_extra_defaults(project_dirs));
return Ok(cfg.compute_extra_defaults());
}
Ok(Self::default().compute_extra_defaults(project_dirs))
Ok(Self::default().compute_extra_defaults())
}
pub fn default_config() -> Self {
let project_dirs = project_dirs().unwrap();
Self::default().compute_extra_defaults(project_dirs)
Self::default().compute_extra_defaults()
}
/// In some cases we need to compute expanded values based
/// on those provided by the user. This is where we do that.
fn compute_extra_defaults(&self, project_dirs: ProjectDirs) -> Self {
fn compute_extra_defaults(&self) -> Self {
let mut cfg = self.clone();
if cfg.mux_server_unix_domain_socket_path.is_none() {
cfg.mux_server_unix_domain_socket_path = match project_dirs.runtime_dir() {
Some(path) => path.join("sock"),
None => project_dirs.data_local_dir().join("sock"),
}
.to_str()
.map(str::to_owned);
cfg.mux_server_unix_domain_socket_path =
RUNTIME_DIR.join("sock").to_str().map(str::to_owned);
}
if cfg.font_rules.is_empty() {