-
Notifications
You must be signed in to change notification settings - Fork 348
perf: improve to_file_path() #1018
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
Changes from 5 commits
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 |
---|---|---|
|
@@ -2720,7 +2720,26 @@ | |
_ => return Err(()), | ||
}; | ||
|
||
return file_url_segments_to_pathbuf(host, segments); | ||
let str_len = self.as_str().len(); | ||
let estimated_capacity = if cfg!(target_os = "redox") { | ||
let scheme_len = self.scheme().len(); | ||
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; | ||
(str_len + scheme_diff).saturating_sub(2) | ||
} else { | ||
let scheme_diff = scheme_len - file_scheme_len; | ||
str_len.saturating_sub(scheme_diff + 2) | ||
} | ||
} else if cfg!(windows) { | ||
// remove scheme: - has posssible \\ for hostname | ||
str_len.saturating_sub(self.scheme().len() + 1) | ||
} else { | ||
// remove scheme:// | ||
str_len.saturating_sub(self.scheme().len() + 3) | ||
}; | ||
return file_url_segments_to_pathbuf(estimated_capacity, host, segments); | ||
} | ||
Err(()) | ||
} | ||
|
@@ -3030,6 +3049,7 @@ | |
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, ()> { | ||
|
@@ -3047,11 +3067,10 @@ | |
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'/'); | ||
|
@@ -3083,22 +3102,27 @@ | |
|
||
#[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::new(); | ||
string.try_reserve(estimated_capacity).map_err(|_| ())?; | ||
if let Some(host) = host { | ||
string.push_str(r"\\"); | ||
string.push_str(host); | ||
} else { | ||
let first = segments.next().ok_or(())?; | ||
|
||
|
@@ -3108,7 +3132,7 @@ | |
return Err(()); | ||
} | ||
|
||
first.to_owned() | ||
string.push_str(first); | ||
} | ||
|
||
4 => { | ||
|
@@ -3120,7 +3144,8 @@ | |
return Err(()); | ||
} | ||
|
||
first[0..1].to_owned() + ":" | ||
string.push_str(&first[0..1]); | ||
string.push(':'); | ||
} | ||
|
||
_ => return Err(()), | ||
|
@@ -3131,11 +3156,20 @@ | |
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(()), | ||
} | ||
} | ||
// ensure our estimated capacity was good | ||
if cfg!(test) { | ||
debug_assert!( | ||
string.len() <= estimated_capacity, | ||
"len: {}, capacity: {}", | ||
string.len(), | ||
estimated_capacity | ||
); | ||
} | ||
let path = PathBuf::from(string); | ||
debug_assert!( | ||
path.is_absolute(), | ||
|
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.
This still wraps around when
scheme
is longer than 4.Uh oh!
There was an error while loading. Please reload this page.
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.
There's a check above for this:
(or maybe I'm misreading)