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 --- vbahelper/source/msforms/vbalistbox.cxx | 2 +- vbahelper/source/msforms/vbalistcontrolhelper.cxx | 2 +- vbahelper/source/vbahelper/vbacommandbarhelper.cxx | 2 +- vbahelper/source/vbahelper/vbacommandbars.cxx | 2 +- vbahelper/source/vbahelper/vbahelper.cxx | 5 +++-- 5 files changed, 7 insertions(+), 6 deletions(-) (limited to 'vbahelper') diff --git a/vbahelper/source/msforms/vbalistbox.cxx b/vbahelper/source/msforms/vbalistbox.cxx index 9dcef8a77225..cfa9e54a6134 100644 --- a/vbahelper/source/msforms/vbalistbox.cxx +++ b/vbahelper/source/msforms/vbalistbox.cxx @@ -227,7 +227,7 @@ ScVbaListBox::getValueEvent() uno::Sequence< sal_Int16 > nList; m_xProps->getPropertyValue( "SelectedItems" ) >>= nList; sal_Int32 nIndex = m_nIndex; - bool bRet = std::find(nList.begin(), nList.end(), nIndex) != nList.end(); + bool bRet = std::find(std::cbegin(nList), std::cend(nList), nIndex) != std::cend(nList); return uno::makeAny( bRet ); } diff --git a/vbahelper/source/msforms/vbalistcontrolhelper.cxx b/vbahelper/source/msforms/vbalistcontrolhelper.cxx index 3fffbd5b1941..8f3ff6b45601 100644 --- a/vbahelper/source/msforms/vbalistcontrolhelper.cxx +++ b/vbahelper/source/msforms/vbalistcontrolhelper.cxx @@ -120,7 +120,7 @@ ListControlHelper::AddItem( const uno::Any& pvargItem, const uno::Any& pvargInde sVec.push_back( sString ); // point at first element to copy - sVec.insert( sVec.end(), std::next(sList.begin(), nIndex), sList.end() ); + sVec.insert( sVec.end(), std::next(std::cbegin(sList), nIndex), std::cend(sList) ); sList.realloc( sList.getLength() + 1 ); diff --git a/vbahelper/source/vbahelper/vbacommandbarhelper.cxx b/vbahelper/source/vbahelper/vbacommandbarhelper.cxx index 01de54cd8244..562205451839 100644 --- a/vbahelper/source/vbahelper/vbacommandbarhelper.cxx +++ b/vbahelper/source/vbahelper/vbacommandbarhelper.cxx @@ -182,7 +182,7 @@ OUString VbaCommandBarHelper::findToolbarByName( const css::uno::Reference< css: if( !sResourceUrl.isEmpty() ) return sResourceUrl; - uno::Sequence< OUString > allNames = xNameAccess->getElementNames(); + const uno::Sequence< OUString > allNames = xNameAccess->getElementNames(); auto pName = std::find_if(allNames.begin(), allNames.end(), [this, &sName](const OUString& rName) { return rName.startsWith( ITEM_TOOLBAR_URL ) diff --git a/vbahelper/source/vbahelper/vbacommandbars.cxx b/vbahelper/source/vbahelper/vbacommandbars.cxx index a864339fd38d..4cc3d28e0cf5 100644 --- a/vbahelper/source/vbahelper/vbacommandbars.cxx +++ b/vbahelper/source/vbahelper/vbacommandbars.cxx @@ -184,7 +184,7 @@ ScVbaCommandBars::getCount() { // Filter out all toolbars from the window collection sal_Int32 nCount = 1; // there is a Menubar in OOo - uno::Sequence< ::OUString > allNames = m_xNameAccess->getElementNames(); + const uno::Sequence< ::OUString > allNames = m_xNameAccess->getElementNames(); nCount += std::count_if(allNames.begin(), allNames.end(), [](const OUString& rName) { return rName.indexOf( "private:resource/toolbar/" ) != -1; }); return nCount; diff --git a/vbahelper/source/vbahelper/vbahelper.cxx b/vbahelper/source/vbahelper/vbahelper.cxx index 389a13352e6a..2eaa7e9f5a38 100644 --- a/vbahelper/source/vbahelper/vbahelper.cxx +++ b/vbahelper/source/vbahelper/vbahelper.cxx @@ -734,9 +734,10 @@ uno::Any getPropertyValue( const uno::Sequence< beans::PropertyValue >& aProp, c bool setPropertyValue( uno::Sequence< beans::PropertyValue >& aProp, const OUString& aName, const uno::Any& aValue ) { - auto pProp = std::find_if(aProp.begin(), aProp.end(), + auto [begin, end] = toNonConstRange(aProp); + auto pProp = std::find_if(begin, end, [&aName](const beans::PropertyValue& rProp) { return rProp.Name == aName; }); - if (pProp != aProp.end()) + if (pProp != end) { pProp->Value = aValue; return true; -- cgit