mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-01 07:13:24 +03:00
Optimize FromIterator<OsString> to reuse the first allocation
This commit is contained in:
@@ -1208,11 +1208,18 @@ fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T) {
|
||||
impl FromIterator<OsString> for OsString {
|
||||
#[inline]
|
||||
fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
|
||||
let mut buf = Self::new();
|
||||
for s in iter {
|
||||
buf.push(&s);
|
||||
let mut iterator = iter.into_iter();
|
||||
|
||||
// Because we're iterating over `OsString`s, we can avoid at least
|
||||
// one allocation by getting the first string from the iterator
|
||||
// and appending to it all the subsequent strings.
|
||||
match iterator.next() {
|
||||
None => OsString::new(),
|
||||
Some(mut buf) => {
|
||||
buf.extend(iterator);
|
||||
buf
|
||||
}
|
||||
}
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user