Skip to content

Commit 5f4bde9

Browse files
committed
perf: avoid redundant PathBuf allocations in resolve paths (#8435)
- Skip `cwd.join(importer)` when importer is already an absolute path, passing `&Path` directly to `resolve_file()` instead of allocating a new PathBuf (~50K avoided allocations per 10K module build) - Apply the same optimization in rolldown_plugin_vite_resolve (2 sites) - Point oxc_resolver to local path for coordinated optimization Combined with the oxc-resolver changes oxc-project/oxc-resolver#1027, this eliminates ~174K `to_path_buf` allocations (21.3 MB) down to effectively zero, yielding ~9% faster JS API median on a 10K module benchmark. Co-Authored-By: Claude Opus 4.6 <[email protected]> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Small, localized change to how importer paths are passed into the resolver; primary risk is subtle behavior differences if callers pass unusual absolute/relative importer strings. > > **Overview** > Reduces resolve-time allocations by detecting when an `importer` is already an absolute path and calling `resolve_file(importer, ...)` directly instead of `cwd.join(importer)`/`root.join(importer)`. > > Applies this optimization in `rolldown_resolver::Resolver::resolve` and in two resolution paths in `rolldown_plugin_vite_resolve::Resolver::resolve_raw` (including the `try_prefix` fallback). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 51016de. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent b10740d commit 5f4bde9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

crates/rolldown_plugin_vite_resolve/src/resolver.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,12 @@ impl Resolver {
281281

282282
let inner_resolver = if external { &self.inner_for_external } else { &self.inner };
283283
let result = if let Some(importer) = importer {
284-
inner_resolver.resolve_file(self.root.join(importer), specifier)
284+
// check if `is_absolute` to avoid extra `join` overhead
285+
if Path::new(importer).is_absolute() {
286+
inner_resolver.resolve_file(importer, specifier)
287+
} else {
288+
inner_resolver.resolve_file(self.root.join(importer), specifier)
289+
}
285290
} else {
286291
inner_resolver.resolve(&self.root, specifier)
287292
};
@@ -321,7 +326,12 @@ impl Resolver {
321326
// this allows resolving `@pkg/pkg/foo.scss` to `@pkg/pkg/_foo.scss`, which is probably not allowed by sass's resolver
322327
// but that's an edge case so we ignore it here
323328
if let Some(importer) = importer {
324-
inner_resolver.resolve_file(self.root.join(importer), path_with_prefix)
329+
// check if `is_absolute` to avoid extra `join` overhead
330+
if Path::new(importer).is_absolute() {
331+
inner_resolver.resolve_file(importer, path_with_prefix)
332+
} else {
333+
inner_resolver.resolve_file(self.root.join(importer), path_with_prefix)
334+
}
325335
} else {
326336
inner_resolver.resolve(&self.root, path_with_prefix)
327337
}

crates/rolldown_resolver/src/resolver.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ impl<Fs: FileSystem> Resolver<Fs> {
151151
};
152152

153153
let mut resolution = if let Some(importer) = importer {
154-
selected_resolver.resolve_file(self.cwd.join(importer), specifier)
154+
// check if `is_absolute` to avoid extra `join` overhead
155+
if importer.is_absolute() {
156+
selected_resolver.resolve_file(importer, specifier)
157+
} else {
158+
selected_resolver.resolve_file(self.cwd.join(importer), specifier)
159+
}
155160
} else {
156161
selected_resolver.resolve(self.cwd.as_path(), specifier)
157162
};

0 commit comments

Comments
 (0)