perf: bulk-copy path segments in to_file_path (unix)#2
Open
crowlbot wants to merge 1 commit into
Open
Conversation
file_url_segments_to_pathbuf decoded each path segment with the byte-at-a-time percent_decode iterator even when the segment contained no '%'. Use the Cow<[u8]> bulk path instead: it borrows the segment unchanged in the common no-escape case, so segments are copied with a single extend_from_slice rather than pushed byte by byte. Bench (parse_url/url_to_file_path): 135 -> 105 ns/iter (-22%).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Url::to_file_path()on Unix built thePathBufby decoding each path segment with the byte-at-a-timepercent_decodeiterator (bytes.extend(percent_decode(..))), even for the overwhelmingly common case where a segment contains no%and nothing needs decoding.This switches to the
Cow<[u8]>bulk path:percent_decode(..).into()borrows the segment unchanged when there is nothing to decode, so each segment is appended with a singleextend_from_slice(a memcpy) instead of oneVec::pushper byte. Behavior is identical (the outer buffer is still pre-reserved withtry_reserve).Only the Unix builder is touched; the Windows builder already decodes in bulk via
decode_utf8+push_str.Correctness
All existing
urltests pass (unit+wpt), including theto_file_pathround-trip tests that cover percent-encoded and non-UTF-8 segments.Benchmark
cargo bench -p url --bench parse_url(2 runs each, same machine):