summaryrefslogtreecommitdiffstats
path: root/include/com
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-10-13 09:02:48 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2021-10-14 06:00:49 +0200
commit8a017d25a62e878fdd32f189f0663b05d2ffb9cf (patch)
treec91ee53b5d9276ae30df785b52579a1b77a057df /include/com
parentsw: remove unused document (diff)
downloadcore-8a017d25a62e878fdd32f189f0663b05d2ffb9cf.tar.gz
core-8a017d25a62e878fdd32f189f0663b05d2ffb9cf.zip
Avoid COW overhead using css::uno::Sequence
The scenarios are: 1. Calling sequence's begin() and end() in pairs to pass to algorithms (both calls use getArray(), which does the COW checks) 2. In addition to #1, calling end() again when checking result of find algorithms, and/or begin() to calculate result's distance 3. Using non-const sequences in range-based for loops, which internally do #1 4. Assigning sequence to another sequence variable, and then modifying one of them In many cases, the sequences could be made const, or treated as const for the purposes of the algorithms (using std::as_const, std::cbegin, and std::cend). Where algorithm modifies the sequence, it was changed to only call getArray() once. For that, css::uno::toNonConstRange was introduced, which returns a struct (sublclass of std::pair) with two iterators [begin, end], that are calculated using one call to begin() and one call to getLength(). To handle #4, css::uno::Sequence::swap was introduced, that swaps the internal pointer to uno_Sequence. So when a local Sequence variable should be assigned to another variable, and the latter will be modified further, it's now possible to use swap instead, so the two sequences are kept independent. The modified places were found by temporarily removing non-const end(). Change-Id: I8fe2787f200eecb70744e8b77fbdf7a49653f628 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123542 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/com')
-rw-r--r--include/com/sun/star/uno/Sequence.h10
-rw-r--r--include/com/sun/star/uno/Sequence.hxx24
2 files changed, 34 insertions, 0 deletions
diff --git a/include/com/sun/star/uno/Sequence.h b/include/com/sun/star/uno/Sequence.h
index 6b19af68d8a1..39caf8ff768f 100644
--- a/include/com/sun/star/uno/Sequence.h
+++ b/include/com/sun/star/uno/Sequence.h
@@ -265,6 +265,16 @@ public:
*/
uno_Sequence * SAL_CALL get() const
{ return _pSequence; }
+
+#if defined LIBO_INTERNAL_ONLY
+ /** Swaps sequences efficiently exchanging their underlying representations.
+
+ @param other another sequence of same type
+
+ @since LibreOffice 7.3
+ */
+ inline void swap(Sequence& other);
+#endif
};
// Find uses of illegal Sequence<bool> (instead of Sequence<sal_Bool>) during
diff --git a/include/com/sun/star/uno/Sequence.hxx b/include/com/sun/star/uno/Sequence.hxx
index 26a51350815b..08a74d73789f 100644
--- a/include/com/sun/star/uno/Sequence.hxx
+++ b/include/com/sun/star/uno/Sequence.hxx
@@ -30,6 +30,7 @@
#if defined LIBO_INTERNAL_ONLY
# include <type_traits>
# include <ostream>
+# include <utility>
#endif
#include "osl/interlck.h"
@@ -203,6 +204,13 @@ inline void Sequence< E >::realloc( sal_Int32 nSize )
throw ::std::bad_alloc();
}
+#if defined LIBO_INTERNAL_ONLY
+template <class E> inline void Sequence<E>::swap(Sequence& other)
+{
+ std::swap(_pSequence, other._pSequence);
+}
+#endif
+
inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
const ::rtl::ByteSequence & rByteSequence )
{
@@ -271,6 +279,22 @@ inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, t
return os;
}
+template <class E> inline auto toNonConstRange(css::uno::Sequence<E>& s)
+{
+ // Two iterators [begin, end] representing the non-const range of the Sequence.
+ // It only calls Sequence::getArray once, to avoid the second COW overhead when
+ // Sequence::begin() and Sequence::end() are called in pairs.
+ // Inheriting from pair allows to use std::tie to unpack the two iterators.
+ struct SequenceRange : public std::pair<E*, E*>
+ {
+ SequenceRange(E* ptr, sal_Int32 len) : std::pair<E*, E*>(ptr, ptr + len) {}
+ // These allow to pass it as range-expression to range-based for loops
+ E* begin() { return std::pair<E*, E*>::first; }
+ E* end() { return std::pair<E*, E*>::second; }
+ };
+ return SequenceRange(s.begin(), s.getLength());
+};
+
/// @endcond
#endif