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: url encode path segments in longer string slices #1026

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3041,7 +3041,6 @@ fn file_url_segments_to_pathbuf(
use std::os::hermit::ffi::OsStrExt;
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::prelude::OsStrExt;
use std::path::PathBuf;

if host.is_some() {
return Err(());
Expand Down
88 changes: 75 additions & 13 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,32 +1191,96 @@
path_start: usize,
mut input: Input<'i>,
) -> Input<'i> {
// it's much faster to call utf8_percent_encode in bulk
fn push_pending(
serialization: &mut String,
start_str: &str,
remaining_len: usize,
context: Context,
scheme_type: SchemeType,
) {
let text = &start_str[..start_str.len() - remaining_len];
if text.is_empty() {
return;

Check warning on line 1204 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1204

Added line #L1204 was not covered by tests
}
if context == Context::PathSegmentSetter {
if scheme_type.is_special() {
serialization.extend(utf8_percent_encode(text, SPECIAL_PATH_SEGMENT));
} else {
serialization.extend(utf8_percent_encode(text, PATH_SEGMENT));
}
} else {
serialization.extend(utf8_percent_encode(text, PATH));
}
}

// Relative path state
loop {
let mut segment_start = self.serialization.len();
let mut ends_with_slash = false;
let mut start_str = input.chars.as_str();
loop {
let input_before_c = input.clone();
let (c, utf8_c) = if let Some(x) = input.next_utf8() {
x
// bypass input.next() and manually handle ascii_tab_or_new_line
// in order to encode string slices in bulk
let c = if let Some(c) = input.chars.next() {
c

Check warning on line 1227 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1227

Added line #L1227 was not covered by tests
} else {
push_pending(
&mut self.serialization,

Check warning on line 1230 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1230

Added line #L1230 was not covered by tests
start_str,
0,
self.context,
scheme_type,

Check warning on line 1234 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1234

Added line #L1234 was not covered by tests
);
break;
};
match c {
ascii_tab_or_new_line_pattern!() => {
push_pending(
&mut self.serialization,

Check warning on line 1241 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1241

Added line #L1241 was not covered by tests
start_str,
input_before_c.chars.as_str().len(),
self.context,
scheme_type,

Check warning on line 1245 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1245

Added line #L1245 was not covered by tests
);
start_str = input.chars.as_str();
}
'/' if self.context != Context::PathSegmentSetter => {
push_pending(
&mut self.serialization,

Check warning on line 1251 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1251

Added line #L1251 was not covered by tests
start_str,
input_before_c.chars.as_str().len(),
self.context,
scheme_type,

Check warning on line 1255 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1255

Added line #L1255 was not covered by tests
);
self.serialization.push(c);
ends_with_slash = true;
break;
}
'\\' if self.context != Context::PathSegmentSetter
&& scheme_type.is_special() =>
{
push_pending(
&mut self.serialization,

Check warning on line 1265 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1265

Added line #L1265 was not covered by tests
start_str,
input_before_c.chars.as_str().len(),
self.context,
scheme_type,

Check warning on line 1269 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1269

Added line #L1269 was not covered by tests
);
self.log_violation(SyntaxViolation::Backslash);
self.serialization.push('/');
ends_with_slash = true;
break;
}
'?' | '#' if self.context == Context::UrlParser => {
push_pending(
&mut self.serialization,

Check warning on line 1278 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1278

Added line #L1278 was not covered by tests
start_str,
input_before_c.chars.as_str().len(),
self.context,
scheme_type,

Check warning on line 1282 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1282

Added line #L1282 was not covered by tests
);
input = input_before_c;
break;
}
Expand All @@ -1228,23 +1292,21 @@
&self.serialization[path_start + 1..],
)
{
push_pending(
&mut self.serialization,

Check warning on line 1296 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1296

Added line #L1296 was not covered by tests
start_str,
input_before_c.chars.as_str().len(),
self.context,
scheme_type,

Check warning on line 1300 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1300

Added line #L1300 was not covered by tests
);
start_str = input_before_c.chars.as_str();
self.serialization.push('/');
segment_start += 1;
}
if self.context == Context::PathSegmentSetter {
if scheme_type.is_special() {
self.serialization
.extend(utf8_percent_encode(utf8_c, SPECIAL_PATH_SEGMENT));
} else {
self.serialization
.extend(utf8_percent_encode(utf8_c, PATH_SEGMENT));
}
} else {
self.serialization.extend(utf8_percent_encode(utf8_c, PATH));
}
}
}
}

let segment_before_slash = if ends_with_slash {
&self.serialization[segment_start..self.serialization.len() - 1]
} else {
Expand Down
Loading