forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#95904 - paolobarbolini:vecdeque-specextend, r…
…=the8472 Add VecDeque::extend from vec::IntoIter and slice::Iter specializations Inspired from the [`Vec` `SpecExtend` implementation](https://github.com/rust-lang/rust/blob/027a232755fa9728e9699337267f6675dfd0a8ba/library/alloc/src/vec/spec_extend.rs), but without the specialization for `TrustedLen` which I'll look into in the future. Should help rust-lang#95632 and KillingSpark/zstd-rs#17 ## Benchmarks Before ``` test vec_deque::bench_extend_bytes ... bench: 862 ns/iter (+/- 10) test vec_deque::bench_extend_vec ... bench: 883 ns/iter (+/- 19) ``` After ``` test vec_deque::bench_extend_bytes ... bench: 8 ns/iter (+/- 0) test vec_deque::bench_extend_vec ... bench: 24 ns/iter (+/- 1) ```
- Loading branch information
Showing
5 changed files
with
109 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::alloc::Allocator; | ||
use crate::vec; | ||
use core::slice; | ||
|
||
use super::VecDeque; | ||
|
||
// Specialization trait used for VecDeque::extend | ||
pub(super) trait SpecExtend<T, I> { | ||
fn spec_extend(&mut self, iter: I); | ||
} | ||
|
||
impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> | ||
where | ||
I: Iterator<Item = T>, | ||
{ | ||
default fn spec_extend(&mut self, mut iter: I) { | ||
// This function should be the moral equivalent of: | ||
// | ||
// for item in iter { | ||
// self.push_back(item); | ||
// } | ||
while let Some(element) = iter.next() { | ||
if self.len() == self.capacity() { | ||
let (lower, _) = iter.size_hint(); | ||
self.reserve(lower.saturating_add(1)); | ||
} | ||
|
||
let head = self.head; | ||
self.head = self.wrap_add(self.head, 1); | ||
unsafe { | ||
self.buffer_write(head, element); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> { | ||
fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { | ||
let slice = iterator.as_slice(); | ||
self.reserve(slice.len()); | ||
|
||
unsafe { | ||
self.copy_slice(self.head, slice); | ||
self.head = self.wrap_add(self.head, slice.len()); | ||
} | ||
iterator.forget_remaining_elements(); | ||
} | ||
} | ||
|
||
impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for VecDeque<T, A> | ||
where | ||
I: Iterator<Item = &'a T>, | ||
T: Copy, | ||
{ | ||
default fn spec_extend(&mut self, iterator: I) { | ||
self.spec_extend(iterator.copied()) | ||
} | ||
} | ||
|
||
impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A> | ||
where | ||
T: Copy, | ||
{ | ||
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { | ||
let slice = iterator.as_slice(); | ||
self.reserve(slice.len()); | ||
|
||
unsafe { | ||
self.copy_slice(self.head, slice); | ||
self.head = self.wrap_add(self.head, slice.len()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters