From f72c60a39a713ecd91034ba0004e7022f279ce0b Mon Sep 17 00:00:00 2001 From: The8472 Date: Thu, 27 May 2021 18:17:09 +0200 Subject: [PATCH] Revert "Auto merge of #83770 - the8472:tra-extend, r=Mark-Simulacrum" Due to a performance regression that didn't show up in the original perf run this reverts commit 9111b8ae9793f18179a1336417618fc07a9cac85, reversing changes made to 9a700d2947f2d7f97a2c0dfca3117a8dcc255bdd. --- library/alloc/src/vec/spec_extend.rs | 84 ++++++++------------------- library/core/src/iter/adapters/zip.rs | 4 -- 2 files changed, 25 insertions(+), 63 deletions(-) diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 5303fdb0c9561..c6f4f22a01f6e 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -1,5 +1,5 @@ use crate::alloc::Allocator; -use core::iter::{TrustedLen, TrustedRandomAccess}; +use core::iter::TrustedLen; use core::ptr::{self}; use core::slice::{self}; @@ -11,49 +11,6 @@ pub(super) trait SpecExtend { } impl SpecExtend for Vec -where - I: Iterator, -{ - default fn spec_extend(&mut self, iter: I) { - SpecExtendInner::spec_extend(self, iter); - } -} - -impl SpecExtend> for Vec { - fn spec_extend(&mut self, mut iterator: IntoIter) { - unsafe { - self.append_elements(iterator.as_slice() as _); - } - iterator.ptr = iterator.end; - } -} - -impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec -where - I: Iterator, - T: Clone, -{ - default fn spec_extend(&mut self, iterator: I) { - SpecExtend::spec_extend(self, iterator.cloned()) - } -} - -impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec -where - T: Copy, -{ - fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { - let slice = iterator.as_slice(); - unsafe { self.append_elements(slice) }; - } -} - -// Helper trait to disambiguate overlapping specializations -trait SpecExtendInner { - fn spec_extend(&mut self, iter: I); -} - -impl SpecExtendInner for Vec where I: Iterator, { @@ -62,7 +19,7 @@ where } } -impl SpecExtendInner for Vec +impl SpecExtend for Vec where I: TrustedLen, { @@ -98,22 +55,31 @@ where } } -impl SpecExtendInner for Vec +impl SpecExtend> for Vec { + fn spec_extend(&mut self, mut iterator: IntoIter) { + unsafe { + self.append_elements(iterator.as_slice() as _); + } + iterator.ptr = iterator.end; + } +} + +impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec where - I: TrustedLen + TrustedRandomAccess, + I: Iterator, + T: Clone, { - default fn spec_extend(&mut self, mut iterator: I) { - let size = iterator.size(); - self.reserve(size); + default fn spec_extend(&mut self, iterator: I) { + self.spec_extend(iterator.cloned()) + } +} - // SAFETY: reserve ensured that there is sufficient capacity for the additional items. - // The loop upholds the TRA requirements by accessing each element only once. - unsafe { - let sink = self.as_mut_ptr().add(self.len()); - for i in 0..size { - ptr::write(sink.add(i), iterator.__iterator_get_unchecked(i)); - self.set_len(self.len() + 1); - } - } +impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec +where + T: Copy, +{ + fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { + let slice = iterator.as_slice(); + unsafe { self.append_elements(slice) }; } } diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs index 4f6ef24be1ba4..2f8f504d8fcaa 100644 --- a/library/core/src/iter/adapters/zip.rs +++ b/library/core/src/iter/adapters/zip.rs @@ -416,10 +416,6 @@ impl ZipFmt