From 38ee88e2b6e9e84f55e3bcd140d81b2505720f0f Mon Sep 17 00:00:00 2001 From: crowlbot <280062030+crowlbot@users.noreply.github.com> Date: Mon, 13 Jul 2026 10:57:05 +0000 Subject: [PATCH] perf: bulk-copy path segments in to_file_path (unix) 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%). --- url/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/url/src/lib.rs b/url/src/lib.rs index fa2803681..10acaa941 100644 --- a/url/src/lib.rs +++ b/url/src/lib.rs @@ -3086,7 +3086,11 @@ fn file_url_segments_to_pathbuf( for segment in segments { bytes.push(b'/'); - bytes.extend(percent_decode(segment.as_bytes())); + // `percent_decode(..).into()` yields a `Cow::Borrowed` when the segment + // has nothing to decode (no `%`), which is the common case; copying the + // slice in bulk is much faster than the byte-at-a-time decode iterator. + let decoded: Cow<'_, [u8]> = percent_decode(segment.as_bytes()).into(); + bytes.extend_from_slice(&decoded); } // A windows drive letter must end with a slash.