-
Notifications
You must be signed in to change notification settings - Fork 335
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
fbaf6df
93682a6
313bde0
3e1aeb4
51bc795
22873d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2722,7 +2722,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); | ||
} | ||
Err(()) | ||
} | ||
|
@@ -3032,6 +3032,7 @@ fn path_to_file_url_segments_windows( | |
any(unix, target_os = "redox", target_os = "wasi", target_os = "hermit") | ||
))] | ||
fn file_url_segments_to_pathbuf( | ||
estimated_capacity: usize, | ||
host: Option<&str>, | ||
segments: str::Split<'_, char>, | ||
) -> Result<PathBuf, ()> { | ||
|
@@ -3049,11 +3050,10 @@ fn file_url_segments_to_pathbuf( | |
return Err(()); | ||
} | ||
|
||
let mut bytes = if cfg!(target_os = "redox") { | ||
b"file:".to_vec() | ||
} else { | ||
Vec::new() | ||
}; | ||
let mut bytes = Vec::with_capacity(estimated_capacity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before 1,000 iterations within an iteration:
After:
|
||
if cfg!(target_os = "redox") { | ||
bytes.extend(b"file:"); | ||
} | ||
|
||
for segment in segments { | ||
bytes.push(b'/'); | ||
|
@@ -3085,22 +3085,26 @@ fn file_url_segments_to_pathbuf( | |
|
||
#[cfg(all(feature = "std", windows))] | ||
fn file_url_segments_to_pathbuf( | ||
estimated_capacity: usize, | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before 1000 iterations within an iteration:
After:
|
||
estimated_capacity: usize, | ||
host: Option<&str>, | ||
mut segments: str::Split<'_, char>, | ||
) -> Result<PathBuf, ()> { | ||
use percent_encoding::percent_decode; | ||
let mut string = if let Some(host) = host { | ||
r"\\".to_owned() + host | ||
use percent_encoding::percent_decode_str; | ||
let mut string = String::with_capacity(estimated_capacity); | ||
if let Some(host) = host { | ||
string.push('\\'); | ||
dsherret marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string.push_str(host); | ||
} else { | ||
let first = segments.next().ok_or(())?; | ||
|
||
|
@@ -3110,7 +3114,7 @@ fn file_url_segments_to_pathbuf_windows( | |
return Err(()); | ||
} | ||
|
||
first.to_owned() | ||
string.push_str(first); | ||
} | ||
|
||
4 => { | ||
|
@@ -3122,7 +3126,8 @@ fn file_url_segments_to_pathbuf_windows( | |
return Err(()); | ||
} | ||
|
||
first[0..1].to_owned() + ":" | ||
string.push_str(&first[0..1]); | ||
string.push(':'); | ||
} | ||
|
||
_ => return Err(()), | ||
|
@@ -3133,7 +3138,7 @@ fn file_url_segments_to_pathbuf_windows( | |
string.push('\\'); | ||
|
||
// Currently non-unicode windows paths cannot be represented | ||
match String::from_utf8(percent_decode(segment.as_bytes()).collect()) { | ||
match percent_decode_str(segment).decode_utf8() { | ||
Ok(s) => string.push_str(&s), | ||
Err(..) => return Err(()), | ||
} | ||
|
There was a problem hiding this comment.
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 hashostname
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.