From 4a5129c700a76bf5c4b7f2aa9c362496fb6d8bd6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 15 Dec 2009 08:34:01 +0000 Subject: [PATCH] a few improvements: 1. Use std::equal instead of reinventing it. 2. don't run dtors in destroy_range if element is pod-like. 3. Use isPodLike to decide between memcpy/uninitialized_copy instead of is_class. isPodLike is more generous in some cases. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91427 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/SmallVector.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/llvm/ADT/SmallVector.h b/include/llvm/ADT/SmallVector.h index 69c25d0d962..b16649e23c9 100644 --- a/include/llvm/ADT/SmallVector.h +++ b/include/llvm/ADT/SmallVector.h @@ -402,11 +402,7 @@ public: bool operator==(const SmallVectorImpl &RHS) const { if (size() != RHS.size()) return false; - for (const T *This = begin(), *That = RHS.begin(), *E = end(); - This != E; ++This, ++That) - if (*This != *That) - return false; - return true; + return std::equal(begin(), end(), RHS.begin()); } bool operator!=(const SmallVectorImpl &RHS) const { return !(*this == RHS); } @@ -440,7 +436,9 @@ private: } void destroy_range(T *S, T *E) { - // TODO: POD + // No need to do a destroy loop for POD's. + if (isPodLike::value) return; + while (S != E) { --E; E->~T(); @@ -459,11 +457,11 @@ void SmallVectorImpl::grow(size_t MinSize) { T *NewElts = static_cast(operator new(NewCapacity*sizeof(T))); // Copy the elements over. - if (is_class::value) - std::uninitialized_copy(begin(), end(), NewElts); - else - // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove). + if (isPodLike::value) + // Use memcpy for PODs: std::uninitialized_copy optimizes to memmove. memcpy(NewElts, begin(), CurSize * sizeof(T)); + else + std::uninitialized_copy(begin(), end(), NewElts); // Destroy the original elements. destroy_range(begin(), end());