Skip to content

Reduce allocations for path conversions on Windows #96314

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

Merged
merged 2 commits into from
Apr 23, 2022
Merged
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
8 changes: 7 additions & 1 deletion library/std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down