perf: reduce IO

This commit is contained in:
Shunsuke Shibayama 2024-11-10 13:32:13 +09:00
parent a7dbdb7c8d
commit 9e782bef2e
5 changed files with 51 additions and 21 deletions

View file

@ -1,6 +1,6 @@
//! provides utilities for parser, compiler, and vm crate.
use std::fmt;
use std::path::PathBuf;
use std::path::{Component, Path, PathBuf};
pub mod cache;
pub mod config;
@ -192,6 +192,33 @@ pub fn normalize_path(path: PathBuf) -> PathBuf {
PathBuf::from(lower)
}
pub fn cheap_canonicalize_path(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}
/// ```
/// use erg_common::trim_eliminate_top_indent;
/// let code = r#"