diff options
| author | bors <bors@rust-lang.org> | 2022-04-23 04:17:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-04-23 04:17:50 +0000 |
| commit | 64c5deb0e3a22b93ed1fe5e2a7f2e8d91eea63b9 (patch) | |
| tree | 22d324d56bfe8cfc7f9471e8a3beb527b196ecd3 /library/std | |
| parent | 8834629b861cd182be6b914d4e6bc5958160debc (diff) | |
| parent | 6cfdeaf1a198a63e65817f12e93b5a29fdddfda8 (diff) | |
| download | rust-64c5deb0e3a22b93ed1fe5e2a7f2e8d91eea63b9.tar.gz rust-64c5deb0e3a22b93ed1fe5e2a7f2e8d91eea63b9.zip | |
Auto merge of #96314 - AronParker:issue-96297-fix, r=thomcc
Reduce allocations for path conversions on Windows Previously, UTF-8 to UTF-16 Path conversions on Windows unnecessarily allocate twice, as described in #96297. This commit fixes that issue.
Diffstat (limited to 'library/std')
| -rw-r--r-- | library/std/src/sys/windows/mod.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 2c832aa75fd..4e9d408291d 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -156,7 +156,13 @@ pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option<usize> { pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> { fn inner(s: &OsStr) -> crate::io::Result<Vec<u16>> { - let mut maybe_result: Vec<u16> = s.encode_wide().collect(); + // Most paths are ASCII, so reserve capacity for as much as there are bytes + // in the OsStr plus one for the null-terminating character. We are not + // wasting bytes here as paths created by this function are primarily used + // in an ephemeral fashion. + let mut maybe_result = Vec::with_capacity(s.len() + 1); + maybe_result.extend(s.encode_wide()); + if unrolled_find_u16s(0, &maybe_result).is_some() { return Err(crate::io::const_io_error!( ErrorKind::InvalidInput, |
