Skip to content

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

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2720,17 +2720,25 @@
_ => return Err(()),
};

let estimated_capacity = self.as_str().len()
- if cfg!(target_os = "redox") {
// remove only // because it still has file:
2
} else if cfg!(windows) {
// remove file: - has posssible \\ for hostname
5
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();

Check warning on line 2726 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2725-L2726

Added lines #L2725 - L2726 were not covered by tests
// 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)

(str_len + scheme_diff).saturating_sub(2)

Check warning on line 2730 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2728-L2730

Added lines #L2728 - L2730 were not covered by tests
} else {
// remove file://
7
};
let scheme_diff = scheme_len - file_scheme_len;
str_len.saturating_sub(scheme_diff + 2)

Check warning on line 2733 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2732-L2733

Added lines #L2732 - L2733 were not covered by tests
}
} else if cfg!(windows) {
// remove scheme: - has posssible \\ for hostname
str_len.saturating_sub(self.scheme().len() + 1)

Check warning on line 2737 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L2737

Added line #L2737 was not covered by tests
} else {
// remove scheme://
str_len.saturating_sub(self.scheme().len() + 3)
};
return file_url_segments_to_pathbuf(estimated_capacity, host, segments);
}
Err(())
Expand Down Expand Up @@ -3110,10 +3118,11 @@
mut segments: str::Split<'_, char>,
) -> Result<PathBuf, ()> {
use percent_encoding::percent_decode_str;
let mut string = String::with_capacity(estimated_capacity);
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);

Check warning on line 3125 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L3121-L3125

Added lines #L3121 - L3125 were not covered by tests
} else {
let first = segments.next().ok_or(())?;

Expand All @@ -3123,7 +3132,7 @@
return Err(());
}

string.push_str(first);

Check warning on line 3135 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L3135

Added line #L3135 was not covered by tests
}

4 => {
Expand All @@ -3135,8 +3144,8 @@
return Err(());
}

string.push_str(&first[0..1]);
string.push(':');

Check warning on line 3148 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L3147-L3148

Added lines #L3147 - L3148 were not covered by tests
}

_ => return Err(()),
Expand All @@ -3147,18 +3156,20 @@
string.push('\\');

// Currently non-unicode windows paths cannot be represented
match percent_decode_str(segment).decode_utf8() {

Check warning on line 3159 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L3159

Added line #L3159 was not covered by tests
Ok(s) => string.push_str(&s),
Err(..) => return Err(()),
}
}
// ensure our estimated capacity was good
debug_assert!(
string.len() <= estimated_capacity,
"len: {}, capacity: {}",
string.len(),
estimated_capacity
);
if cfg!(test) {
debug_assert!(

Check warning on line 3166 in url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

url/src/lib.rs#L3166

Added line #L3166 was not covered by tests
string.len() <= estimated_capacity,
"len: {}, capacity: {}",
string.len(),
estimated_capacity
);
}
let path = PathBuf::from(string);
debug_assert!(
path.is_absolute(),
Expand Down
Loading