QVarLengthArray: add rvalue overload of append/push_back

Improves performance when appending temporaries, esp. since
the aliasing fix in the lvalue overload in 0f730ef made that
overload correct, but a bit slower across reallocs.

The unit tests already also pass rvalues, so the function is
covered in the existing tests.

[ChangeLog][QtCore][QVarLengthArray] Added rvalue overloads
of append() and push_back().

Change-Id: If3a6970f03a160cba5b42d33d32d3d18948f6ce3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2017-02-17 00:58:11 +01:00
parent 63856135da
commit 1d31f23ce9
2 changed files with 39 additions and 0 deletions

View File

@ -165,6 +165,16 @@ public:
}
}
void append(T &&t) {
if (s == a)
realloc(s, s << 1);
const int idx = s++;
if (QTypeInfo<T>::isComplex)
new (ptr + idx) T(std::move(t));
else
ptr[idx] = std::move(t);
}
void append(const T *buf, int size);
inline QVarLengthArray<T, Prealloc> &operator<<(const T &t)
{ append(t); return *this; }
@ -218,6 +228,7 @@ public:
// STL compatibility:
inline bool empty() const { return isEmpty(); }
inline void push_back(const T &t) { append(t); }
void push_back(T &&t) { append(std::move(t)); }
inline void pop_back() { removeLast(); }
inline T &front() { return first(); }
inline const T &front() const { return first(); }

View File

@ -302,6 +302,34 @@
Provided for STL-compatibility.
*/
/*!
\fn void QVarLengthArray::append(T &&t)
\overload append
\since 5.9
\note Unlike the lvalue overload of append(), passing a reference to
an object that is already an element of \c *this leads to undefined
behavior:
\code
vla.append(std::move(vla[0])); // BUG: passing an object that is already in the container
\endcode
*/
/*!
\fn void QVarLengthArray::push_back(T &&t)
\overload push_back
\since 5.9
\note Unlike the lvalue overload of push_back(), passing a reference to
an object that is already an element of \c *this leads to undefined
behavior:
\code
vla.push_back(std::move(vla[0])); // BUG: passing an object that is already in the container
\endcode
*/
/*!
\fn inline void QVarLengthArray::removeLast()
\since 4.5