Skip to content

Commit 9fd8944

Browse files
committed
perf: remove the redundant node_modules/package/index cache value
closes #851
1 parent 642fd62 commit 9fd8944

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/cache/cached_path.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ impl CachedPath {
120120
})
121121
}
122122

123+
pub(crate) fn add_name_and_extension<Fs: FileSystem>(
124+
&self,
125+
name: &str,
126+
ext: &str,
127+
cache: &Cache<Fs>,
128+
) -> Self {
129+
SCRATCH_PATH.with_borrow_mut(|path| {
130+
path.clear();
131+
let s = path.as_mut_os_string();
132+
s.push(self.path.as_os_str());
133+
s.push(std::path::MAIN_SEPARATOR_STR);
134+
s.push(name);
135+
s.push(ext);
136+
cache.value(path)
137+
})
138+
}
139+
123140
pub(crate) fn replace_extension<Fs: FileSystem>(&self, ext: &str, cache: &Cache<Fs>) -> Self {
124141
SCRATCH_PATH.with_borrow_mut(|path| {
125142
path.clear();

src/lib.rs

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,13 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
656656
// 2. If X.js is a file, load X.js as JavaScript text. STOP
657657
// 3. If X.json is a file, parse X.json to a JavaScript Object. STOP
658658
// 4. If X.node is a file, load X.node as binary addon. STOP
659-
if let Some(path) =
660-
self.load_extensions(cached_path, &self.options.extensions, tsconfig, ctx)?
661-
{
662-
return Ok(Some(path));
659+
if !ctx.fully_specified {
660+
for extension in &self.options.extensions {
661+
let cached_path = cached_path.add_extension(extension, self.cache.as_ref());
662+
if let Some(path) = self.load_alias_or_file(&cached_path, tsconfig, ctx)? {
663+
return Ok(Some(path));
664+
}
665+
}
663666
}
664667
Ok(None)
665668
}
@@ -740,25 +743,6 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
740743
Ok(None)
741744
}
742745

743-
fn load_extensions(
744-
&self,
745-
path: &CachedPath,
746-
extensions: &[String],
747-
tsconfig: Option<&TsConfig>,
748-
ctx: &mut Ctx,
749-
) -> ResolveResult {
750-
if ctx.fully_specified {
751-
return Ok(None);
752-
}
753-
for extension in extensions {
754-
let cached_path = path.add_extension(extension, self.cache.as_ref());
755-
if let Some(path) = self.load_alias_or_file(&cached_path, tsconfig, ctx)? {
756-
return Ok(Some(path));
757-
}
758-
}
759-
Ok(None)
760-
}
761-
762746
fn load_realpath(&self, cached_path: &CachedPath) -> Result<PathBuf, ResolveError> {
763747
if self.options.symlinks {
764748
self.cache.canonicalize(cached_path)
@@ -801,21 +785,18 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
801785
tsconfig: Option<&TsConfig>,
802786
ctx: &mut Ctx,
803787
) -> ResolveResult {
804-
for main_file in &self.options.main_files {
805-
let cached_path = cached_path.normalize_with(main_file, self.cache.as_ref());
806-
if self.options.enforce_extension.is_disabled()
807-
&& let Some(path) = self.load_browser_field_or_alias(&cached_path, tsconfig, ctx)?
808-
&& self.check_restrictions(path.path())
809-
{
810-
return Ok(Some(path));
811-
}
812-
// 1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
813-
// 2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
814-
// 3. If X/index.node is a file, load X/index.node as binary addon. STOP
815-
if let Some(path) =
816-
self.load_extensions(&cached_path, &self.options.extensions, tsconfig, ctx)?
817-
{
818-
return Ok(Some(path));
788+
if !ctx.fully_specified {
789+
for main_file in &self.options.main_files {
790+
// 1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
791+
// 2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
792+
// 3. If X/index.node is a file, load X/index.node as binary addon. STOP
793+
for extension in &self.options.extensions {
794+
let cached_path =
795+
cached_path.add_name_and_extension(main_file, extension, &self.cache);
796+
if let Some(path) = self.load_alias_or_file(&cached_path, tsconfig, ctx)? {
797+
return Ok(Some(path));
798+
}
799+
}
819800
}
820801
}
821802
Ok(None)

0 commit comments

Comments
 (0)