Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve to_file_path() #1018

Merged
merged 6 commits into from
Feb 8, 2025

Conversation

dsherret
Copy link
Contributor

@dsherret dsherret commented Jan 27, 2025

Reduces some heap allocations and pre-allocates the vector for building the string.

url/src/lib.rs Outdated
} else {
Vec::new()
};
let mut bytes = Vec::with_capacity(estimated_capacity);
Copy link
Contributor Author

@dsherret dsherret Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before 1,000 iterations within an iteration:

test url_to_file_path ... bench: 202,125 ns/iter (+/- 12,207)

After:

test url_to_file_path ... bench: 127,257 ns/iter (+/- 4,782)

host: Option<&str>,
segments: str::Split<char>,
) -> Result<PathBuf, ()> {
file_url_segments_to_pathbuf_windows(host, segments)
file_url_segments_to_pathbuf_windows(estimated_capacity, host, segments)
}

// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
#[cfg(feature = "std")]
#[cfg_attr(not(windows), allow(dead_code))]
fn file_url_segments_to_pathbuf_windows(
Copy link
Contributor Author

@dsherret dsherret Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before 1000 iterations within an iteration:

test url_to_file_path ... bench: 437,555 ns/iter (+/- 11,519)

After:

test url_to_file_path ... bench: 119,461 ns/iter (+/- 5,927)

url/src/lib.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 27, 2025

Codecov Report

Attention: Patch coverage is 21.73913% with 18 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@ffca1ef). Learn more about missing BASE report.

Files with missing lines Patch % Lines
url/src/lib.rs 21.73% 18 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1018   +/-   ##
=======================================
  Coverage        ?   79.97%           
=======================================
  Files           ?       24           
  Lines           ?     4274           
  Branches        ?        0           
=======================================
  Hits            ?     3418           
  Misses          ?      856           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dsherret

This comment was marked as outdated.

Copy link
Collaborator

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

url/src/lib.rs Outdated
@@ -2720,7 +2720,7 @@ impl Url {
_ => return Err(()),
};

return file_url_segments_to_pathbuf(host, segments);
return file_url_segments_to_pathbuf(self.as_str().len(), host, segments);
Copy link
Collaborator

@lucacasonato lucacasonato Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split the estimated_len into its own temp variable so it's cleaner to read what this is.

I think this over-alloc 7 bytes in 99% of cases on unix (file://). On redox, just 2 (//). On Windows, even 8 (file:///). Windows also has hostname support though, so to ensure no-realloc in most cases, you should probably do -5 there too (file: - the extra // is for the leading \\ in hostname cases).

The cost of doing this calculation may be too much perf cost though - so please check if this minor memory improvement wouldn't regress perf too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds a few nanoseconds. Worth it to use less memory IMO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

url/src/lib.rs Outdated
@@ -2720,7 +2720,18 @@ impl Url {
_ => return Err(()),
};

return file_url_segments_to_pathbuf(host, segments);
let estimated_capacity = self.as_str().len()
- if cfg!(target_os = "redox") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a clamped subtract so that it doesn't wrap around in release when someone calls this with something other than file scheme URLs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I think the logic needs to be a bit smarter here because urls could be like a:/path? I updated the code to at least be a fast path for file://-like urls and maybe in some weird cases like a:/path it might under allocate. Probalby not worth the effort to make it smarter than it is now.

let file_scheme_len = "file".len();
// remove only // because it still has file:
if scheme_len < file_scheme_len {
let scheme_diff = file_scheme_len - scheme_len;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still wraps around when scheme is longer than 4.

Copy link
Contributor Author

@dsherret dsherret Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a check above for this:

if scheme_len < file_scheme_len { // if 5 < 4 {

(or maybe I'm misreading)

@dsherret dsherret requested a review from lucacasonato February 7, 2025 19:22
Copy link
Collaborator

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, very nice improvement.

@lucacasonato lucacasonato added this pull request to the merge queue Feb 8, 2025
Merged via the queue into servo:main with commit 267afa5 Feb 8, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants