Skip to content

perf: bulk-copy path segments in to_file_path (unix)#2

Open
crowlbot wants to merge 1 commit into
mainfrom
perf/to-file-path-bulk-decode
Open

perf: bulk-copy path segments in to_file_path (unix)#2
crowlbot wants to merge 1 commit into
mainfrom
perf/to-file-path-bulk-decode

Conversation

@crowlbot

Copy link
Copy Markdown
Owner

Summary

Url::to_file_path() on Unix built the PathBuf by decoding each path segment with the byte-at-a-time percent_decode iterator (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 single extend_from_slice (a memcpy) instead of one Vec::push per byte. Behavior is identical (the outer buffer is still pre-reserved with try_reserve).

Only the Unix builder is touched; the Windows builder already decodes in bulk via decode_utf8 + push_str.

Correctness

All existing url tests pass (unit + wpt), including the to_file_path round-trip tests that cover percent-encoded and non-UTF-8 segments.

Benchmark

cargo bench -p url --bench parse_url (2 runs each, same machine):

bench before after change
url_to_file_path 135–136 ns 105–106 ns −22%

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%).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant