mercredi 28 janvier 2015

vector::clear in libc++ for trivially destructible types

Would vector<T, std::allocator<T>>::clear() be O(1) if T is trivially destructible?


gcc's implementation in bits/stl_vector.h calls std::_Destroy(bits/stl_construct.h). This implementation which optimizes for the case of T being trivially destructible through tag-dispatching on std::is_trivially_destructible<T>.


Looking through llvm's(3.5.0) implementation, vector::clear calls std::allocator<T>::destroy on every element, which in turn invokes the destructor.



_LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}


Would this end up getting optimized out making vector::clear() O(1) in libc++ as well?


Aucun commentaire:

Enregistrer un commentaire