perf: python_sys_path call once

This commit is contained in:
Shunsuke Shibayama 2024-09-12 14:40:50 +09:00
parent f78e4909de
commit 9f789199b9
2 changed files with 18 additions and 8 deletions

View file

@ -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()
@ -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
@ -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())
}

View file

@ -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;
@ -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`
@ -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() {