Skip to content

Commit

Permalink
perf: python_sys_path call once
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 14, 2024
1 parent f78e490 commit 9f78919
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 13 additions & 0 deletions crates/erg_common/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ fn _erg_pkgs_path() -> PathBuf {
fallback_erg_path().join("lib/pkgs")
})
}
fn _sys_path() -> impl Iterator<Item = PathBuf> {
get_sys_path(None).unwrap_or_default().into_iter().map(|p| {
p.canonicalize().unwrap_or_else(|_| {
// eprintln!("{RED}[ERR] {} not found {RESET}", p.display());
fallback_erg_path().join("lib/pkgs")
})
})
}
fn _python_site_packages() -> impl Iterator<Item = PathBuf> {
get_sys_path(None)
.unwrap_or_default()
Expand All @@ -91,6 +99,7 @@ pub static ERG_CORE_DECL_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_STD_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_PYSTD_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_PKGS_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static PYTHON_SYS_PATH: OnceLock<Vec<PathBuf>> = OnceLock::new();
pub static PYTHON_SITE_PACKAGES: OnceLock<Vec<PathBuf>> = OnceLock::new();

/// == `Path::new("~/.erg")` if ERG_PATH is not set
Expand Down Expand Up @@ -123,6 +132,10 @@ pub fn erg_pkgs_path() -> &'static PathBuf {
ERG_PKGS_PATH.get_or_init(|| normalize_path(_erg_pkgs_path()))
}

pub fn python_sys_path() -> &'static Vec<PathBuf> {
PYTHON_SYS_PATH.get_or_init(|| _sys_path().collect())
}

pub fn python_site_packages() -> &'static Vec<PathBuf> {
PYTHON_SITE_PACKAGES.get_or_init(|| _python_site_packages().collect())
}
Expand Down
13 changes: 5 additions & 8 deletions crates/erg_common/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use std::process::Stdio;

use crate::config::ErgConfig;
use crate::consts::{EXPERIMENTAL_MODE, PYTHON_MODE};
use crate::env::{erg_path, erg_pkgs_path, erg_pystd_path, erg_std_path, python_site_packages};
use crate::env::{
erg_path, erg_pkgs_path, erg_pystd_path, erg_std_path, python_site_packages, python_sys_path,
};
use crate::pathutil::{add_postfix_foreach, remove_postfix};
use crate::python_util::get_sys_path;
use crate::random::random;
use crate::stdin::GLOBAL_STDIN;
use crate::vfs::VFS;
Expand Down Expand Up @@ -374,10 +375,6 @@ impl Input {
}
}

pub fn sys_path(&self) -> Result<Vec<PathBuf>, std::io::Error> {
get_sys_path(self.path().parent())
}

/// resolution order:
/// 1. `{path/to}.er`
/// 2. `{path/to}/__init__.er`
Expand Down Expand Up @@ -471,8 +468,8 @@ impl Input {
if let Ok(path) = self.resolve_local_py(path) {
return Ok(path);
}
for sys_path in self.sys_path()? {
let mut dir = sys_path;
for sys_path in python_sys_path() {
let mut dir = sys_path.clone();
dir.push(path);
dir.set_extension("py");
if dir.exists() {
Expand Down

0 comments on commit 9f78919

Please sign in to comment.