[llvm] r223201 - ADT: Add SmallVector<>::emplace_back()

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Dec 2 21:36:24 PST 2014


> On 2014 Dec 2, at 21:00, David Blaikie <dblaikie at gmail.com> wrote:
> 
> 
> 
> On Tue, Dec 2, 2014 at 8:45 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com> wrote:
> Author: dexonsmith
> Date: Tue Dec  2 22:45:09 2014
> New Revision: 223201
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=223201&view=rev
> Log:
> ADT: Add SmallVector<>::emplace_back()
> 
> Modified:
>     llvm/trunk/include/llvm/ADT/SmallVector.h
>     llvm/trunk/unittests/ADT/SmallVectorTest.cpp
> 
> Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=223201&r1=223200&r2=223201&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
> +++ llvm/trunk/include/llvm/ADT/SmallVector.h Tue Dec  2 22:45:09 2014
> @@ -236,6 +236,51 @@ public:
>      this->setEnd(this->end()-1);
>      this->end()->~T();
>    }
> +
> +#if LLVM_HAS_VARIADIC_TEMPLATES
> +  template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
> +    if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
> +      this->grow();
> +    ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
> +    this->setEnd(this->end() + 1);
> +  }
> +#else
> +private:
> +  template <typename Constructor> emplace_back_impl(Constructor emplace) {
> 
> wow, calling that functor 'emplace' confused me for quite a while (I was wondering why we had an emplace function that took a void* and didn't update the end function) - at some point we might grow SmallVector::emplace to match std::vector::emplace (emplace at a specified location, rather than the back) - so it might be best not to use that name in this context.

Heh, good point.  Totally missed that.  How do you feel about `construct`?

>  
> +    if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
> +      this->grow();
> 
> This can be tricky - I seem to recall seeing some previous changes being careful about allowing, I think it was, insertion of a value referenced within the container being inserted into. That has to carefully split the grow operation - allocating the new buffer, constructing the new element, then moving/copying the old elements over.
> 
> I don't know if the C++ standard guarantees that, say, v.emplace_back(v[0]), is required to be valid, or if we should bother making that valid, but it's a thing to think about.

I'm not sure either (and I hadn't thought of it), but I replied to the
other thread that you bumped.

>  
> +    emplace((void *)this->end());
> +    this->setEnd(this->end() + 1);
> +  }
> +
> +public:
> +  void emplace_back() {
> +    emplace_back_impl([](void *Mem) { ::new (Mem) T(); });
> +  }
> +  template <typename T1> void emplace_back(T1 &&A1) {
> +    emplace_back_impl([&](void *Mem) { ::new (Mem) T(std::forward<T1>(A1)); });
> +  }
> +  template <typename T1, typename T2> void emplace_back(T1 &&A1, T2 &&A2) {
> +    emplace_back_impl([&](void *Mem) {
> +      ::new (Mem) T(std::forward<T1>(A1), std::forward<T2>(A2));
> +    });
> +  }
> +  template <typename T1, typename T2, typename T3>
> +  void emplace_back(T1 &&A1, T2 &&A2, T3 &&A3) {
> +    T(std::forward<T1>(A1), std::forward<T2>(A2), std::forward<T3>(A3));
> +    emplace_back_impl([&](void *Mem) {
> +      ::new (Mem)
> +          T(std::forward<T1>(A1), std::forward<T2>(A2), std::forward<T3>(A3));
> +    });
> +  }
> +  template <typename T1, typename T2, typename T3, typename T4>
> +  void emplace_back(T1 &&A1, T2 &&A2, T3 &&A3, T4 &&A4) {
> +    emplace_back_impl([&](void *Mem) {
> +      ::new (Mem) T(std::forward<T1>(A1), std::forward<T2>(A2),
> +                    std::forward<T3>(A3), std::forward<T4>(A4));
> +    });
> +  }
> +#endif // LLVM_HAS_VARIADIC_TEMPLATES
>  };
> 
>  // Define this out-of-line to dissuade the C++ compiler from inlining it.
> 
> Modified: llvm/trunk/unittests/ADT/SmallVectorTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=223201&r1=223200&r2=223201&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ADT/SmallVectorTest.cpp (original)
> +++ llvm/trunk/unittests/ADT/SmallVectorTest.cpp Tue Dec  2 22:45:09 2014
> @@ -699,4 +699,135 @@ TEST(SmallVectorTest, MidInsert) {
>      EXPECT_TRUE(m.hasValue);
>  }
> 
> +enum EmplaceableArgState {
> +  EAS_Defaulted,
> +  EAS_Arg,
> +  EAS_LValue,
> +  EAS_RValue,
> +  EAS_Failure
> +};
> +template <int I> struct EmplaceableArg {
> +  EmplaceableArgState State;
> +  EmplaceableArg() : State(EAS_Defaulted) {}
> +  EmplaceableArg(EmplaceableArg &&X)
> +      : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
> +  EmplaceableArg(EmplaceableArg &X)
> +      : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
> +
> +  explicit EmplaceableArg(bool) : State(EAS_Arg) {}
> +
> +private:
> +  EmplaceableArg(const EmplaceableArg &X) LLVM_DELETED_FUNCTION;
> +  EmplaceableArg &operator=(EmplaceableArg &&) LLVM_DELETED_FUNCTION;
> +  EmplaceableArg &operator=(const EmplaceableArg &) LLVM_DELETED_FUNCTION;
> +};
> +
> +enum EmplaceableState { ES_Emplaced, ES_Moved };
> +struct Emplaceable {
> +  EmplaceableArg<0> A0;
> +  EmplaceableArg<1> A1;
> +  EmplaceableArg<2> A2;
> +  EmplaceableArg<3> A3;
> +  EmplaceableState State;
> +
> +  Emplaceable() : State(ES_Emplaced) {}
> +
> +  template <class A0Ty>
> +  explicit Emplaceable(A0Ty &&A0)
> +      : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
> +
> +  template <class A0Ty, class A1Ty>
> +  Emplaceable(A0Ty &&A0, A1Ty &&A1)
> +      : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
> +        State(ES_Emplaced) {}
> +
> +  template <class A0Ty, class A1Ty, class A2Ty>
> +  Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
> +      : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
> +        A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
> +
> +  template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
> +  Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
> +      : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
> +        A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
> +        State(ES_Emplaced) {}
> +
> +  Emplaceable(Emplaceable &&) : State(ES_Moved) {}
> +  Emplaceable &operator=(Emplaceable &&) {
> +    State = ES_Moved;
> +    return *this;
> +  }
> +
> +private:
> +  Emplaceable(const Emplaceable &) LLVM_DELETED_FUNCTION;
> +  Emplaceable &operator=(const Emplaceable &) LLVM_DELETED_FUNCTION;
> +};
> +
> +TEST(SmallVectorTest, EmplaceBack) {
> +  EmplaceableArg<0> A0(true);
> +  EmplaceableArg<1> A1(true);
> +  EmplaceableArg<2> A2(true);
> +  EmplaceableArg<3> A3(true);
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back();
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
> +  }
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back(std::move(A0));
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_RValue);
> +    EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
> +  }
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back(A0);
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_LValue);
> +    EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
> +  }
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back(A0, A1);
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_LValue);
> +    EXPECT_TRUE(V.back().A1.State == EAS_LValue);
> +    EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
> +  }
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back(std::move(A0), std::move(A1));
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_RValue);
> +    EXPECT_TRUE(V.back().A1.State == EAS_RValue);
> +    EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
> +    EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
> +  }
> +  {
> +    SmallVector<Emplaceable, 3> V;
> +    V.emplace_back(std::move(A0), A1, std::move(A2), A3);
> +    EXPECT_TRUE(V.size() == 1);
> +    EXPECT_TRUE(V.back().State == ES_Emplaced);
> +    EXPECT_TRUE(V.back().A0.State == EAS_RValue);
> +    EXPECT_TRUE(V.back().A1.State == EAS_LValue);
> +    EXPECT_TRUE(V.back().A2.State == EAS_RValue);
> +    EXPECT_TRUE(V.back().A3.State == EAS_LValue);
> +  }
>  }
> +
> +} // end namespace
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list