diff options
| author | Dan Gohman <dev@sunfishcode.online> | 2022-05-07 09:34:57 -0700 |
|---|---|---|
| committer | Dan Gohman <dev@sunfishcode.online> | 2022-06-23 13:10:47 -0700 |
| commit | caf8bcceff301b4fa3414e3f21813581a8d758a3 (patch) | |
| tree | a9111e4c5d5b49b70878a7290737cae316c8532b /library/std/src/sys/windows | |
| parent | 10f4ce324baf7cfb7ce2b2096662b82b79204944 (diff) | |
| download | rust-caf8bcceff301b4fa3414e3f21813581a8d758a3.tar.gz rust-caf8bcceff301b4fa3414e3f21813581a8d758a3.zip | |
Optimize `Wtf8Buf::into_string` for the case where it contains UTF-8.
Add a `is_known_utf8` flag to `Wtf8Buf`, which tracks whether the string is known to contain UTF-8. This is efficiently computed in many common situations, such as when a `Wtf8Buf` is constructed from a `String` or `&str`, or with `Wtf8Buf::from_wide` which is already doing UTF-16 decoding and already checking for surrogates. This makes `OsString::into_string` O(1) rather than O(N) on Windows in common cases. And, it eliminates the need to scan through the string for surrogates in `Args::next` and `Vars::next`, because the strings are already being translated with `Wtf8Buf::from_wide`. Many things on Windows construct `OsString`s with `Wtf8Buf::from_wide`, such as `DirEntry::file_name` and `fs::read_link`, so with this patch, users of those functions can subsequently call `.into_string()` without paying for an extra scan through the string for surrogates.
Diffstat (limited to 'library/std/src/sys/windows')
| -rw-r--r-- | library/std/src/sys/windows/os_str.rs | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/library/std/src/sys/windows/os_str.rs b/library/std/src/sys/windows/os_str.rs index 11883f15022..4bdd8c505ff 100644 --- a/library/std/src/sys/windows/os_str.rs +++ b/library/std/src/sys/windows/os_str.rs @@ -164,9 +164,7 @@ impl Slice { } pub fn to_owned(&self) -> Buf { - let mut buf = Wtf8Buf::with_capacity(self.inner.len()); - buf.push_wtf8(&self.inner); - Buf { inner: buf } + Buf { inner: self.inner.to_owned() } } pub fn clone_into(&self, buf: &mut Buf) { |
