From 8a017d25a62e878fdd32f189f0663b05d2ffb9cf Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 13 Oct 2021 09:02:48 +0300 Subject: 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 --- chart2/qa/extras/chart2import.cxx | 2 +- chart2/qa/extras/charttest.hxx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'chart2/qa') diff --git a/chart2/qa/extras/chart2import.cxx b/chart2/qa/extras/chart2import.cxx index 682f24febca0..e908cfc57a71 100644 --- a/chart2/qa/extras/chart2import.cxx +++ b/chart2/qa/extras/chart2import.cxx @@ -2001,7 +2001,7 @@ void Chart2ImportTest::testTdf116163() CPPUNIT_ASSERT(xTextualDataSequence.is()); std::vector aCategories; - Sequence aTextData(xTextualDataSequence->getTextualData()); + const Sequence aTextData(xTextualDataSequence->getTextualData()); ::std::copy(aTextData.begin(), aTextData.end(), ::std::back_inserter(aCategories)); diff --git a/chart2/qa/extras/charttest.hxx b/chart2/qa/extras/charttest.hxx index 9094ab308afb..2aec79cfccb0 100644 --- a/chart2/qa/extras/charttest.hxx +++ b/chart2/qa/extras/charttest.hxx @@ -102,8 +102,8 @@ public: OUString findChartFile(const OUString& rDir, uno::Reference< container::XNameAccess > const & xNames ) { - uno::Sequence aNames = xNames->getElementNames(); - OUString* pElement = std::find_if(aNames.begin(), aNames.end(), CheckForChartName(rDir)); + const uno::Sequence aNames = xNames->getElementNames(); + const OUString* pElement = std::find_if(aNames.begin(), aNames.end(), CheckForChartName(rDir)); CPPUNIT_ASSERT(pElement != aNames.end()); return *pElement; -- cgit