Skip to content

Commit e85ba2c

Browse files
committed
Cache file resolver calls
Signed-off-by: Jonatan Waern <jonatan.waern@intel.com>
1 parent b3338a0 commit e85ba2c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# Change Log
66

77
# 0.9.15
8-
- Slight optimization in how the server resolves file paths, should reduce
9-
time-to-ready for the server when first starting by about 30%.
8+
- Optimizations in how the server resolves file paths, should reduce
9+
time-to-ready for the server when first starting by about 50%, depending
10+
on the complexity of the include tree.
1011

1112
## 0.9.14
1213
- Slight optimization to the memory usage of device-level analysis which

src/file_management.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::fs;
88

99
use std::path::{Path, PathBuf};
1010
use std::ops::Deref;
11+
use std::cell::RefCell;
1112

1213
// A path which we know to be canonical in the filesystem
1314
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
@@ -52,12 +53,14 @@ impl CanonPath {
5253
}
5354

5455
/// This is how we resolve relative paths to in-workspace full paths
55-
#[derive(Eq, PartialEq, Debug, Clone)]
56+
#[derive(Debug, Clone)]
5657
pub struct PathResolver {
5758
// Root is provided by context, who will pass this struct to threads for
5859
// import resolution
5960
roots: Vec<PathBuf>,
6061
include_paths: HashMap<CanonPath, Vec<PathBuf>>,
62+
#[allow(clippy::type_complexity)]
63+
cache: RefCell<HashMap<(PathBuf, Option<CanonPath>), Option<CanonPath>>>,
6164
}
6265

6366
impl From<Option<PathBuf>> for PathResolver {
@@ -69,6 +72,7 @@ impl From<Option<PathBuf>> for PathResolver {
6972
PathResolver {
7073
roots,
7174
include_paths: HashMap::default(),
75+
cache: RefCell::default(),
7276
}
7377
}
7478
}
@@ -117,6 +121,16 @@ impl PathResolver {
117121
path: &Path,
118122
context: Option<&CanonPath>)
119123
-> Option<CanonPath> {
124+
self.cache.borrow_mut().entry((path.to_path_buf(), context.cloned()))
125+
.or_insert_with(
126+
||self.resolve_with_maybe_context_impl(path, context))
127+
.clone()
128+
}
129+
130+
fn resolve_with_maybe_context_impl(&self,
131+
path: &Path,
132+
context: Option<&CanonPath>)
133+
-> Option<CanonPath> {
120134
// Given some relative info, find a canonical file path
121135
// NOTE: Right now the relative info is a pathbuf, but this might
122136
// change later

0 commit comments

Comments
 (0)