perf(node_resolver): reduce url to/from path conversions (#27839)

Extracted out of https://github.com/denoland/deno/pull/27838/files

Reduces some allocations by accepting either a pathbuf or url for the
referrer for resolution and returning either a pathbuf or url at the
end, which the caller can then convert into to their preferred state.

This is about 4% faster when still converting the final result to a url
and 6% faster when keeping the result as a path in a benchmark I ran.
This commit is contained in:
David Sherret 2025-01-27 15:23:20 -05:00 committed by GitHub
parent 92dce12af7
commit 679902a108
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 679 additions and 417 deletions

View file

@ -709,6 +709,9 @@ pub enum ResolveError {
)]
ModuleResolution(#[from] deno_core::ModuleResolutionError),
#[class(inherit)]
#[error(transparent)]
FilePathToUrl(#[from] deno_path_util::PathToUrlError),
#[class(inherit)]
#[error("{0}")]
PackageSubpathResolve(PackageSubpathResolveError),
#[class(inherit)]
@ -943,7 +946,7 @@ fn resolve_graph_specifier_types(
NodeResolutionKind::Types,
);
let maybe_url = match res_result {
Ok(url) => Some(url),
Ok(path_or_url) => Some(path_or_url.into_url()?),
Err(err) => match err.code() {
NodeJsErrorCode::ERR_TYPES_NOT_FOUND
| NodeJsErrorCode::ERR_MODULE_NOT_FOUND => None,
@ -971,6 +974,9 @@ fn resolve_graph_specifier_types(
#[derive(Debug, Error, deno_error::JsError)]
pub enum ResolveNonGraphSpecifierTypesError {
#[class(inherit)]
#[error(transparent)]
FilePathToUrl(#[from] deno_path_util::PathToUrlError),
#[class(inherit)]
#[error(transparent)]
ResolvePkgFolderFromDenoReq(#[from] ResolvePkgFolderFromDenoReqError),
@ -1003,8 +1009,8 @@ fn resolve_non_graph_specifier_types(
resolution_mode,
NodeResolutionKind::Types,
)
.ok()
.map(|res| res.into_url()),
.and_then(|res| res.into_url())
.ok(),
)))
} else if let Ok(npm_req_ref) =
NpmPackageReqReference::from_str(raw_specifier)
@ -1025,7 +1031,7 @@ fn resolve_non_graph_specifier_types(
NodeResolutionKind::Types,
);
let maybe_url = match res_result {
Ok(url) => Some(url),
Ok(url_or_path) => Some(url_or_path.into_url()?),
Err(err) => match err.code() {
NodeJsErrorCode::ERR_TYPES_NOT_FOUND
| NodeJsErrorCode::ERR_MODULE_NOT_FOUND => None,