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 --- scripting/source/basprov/basmethnode.cxx | 2 +- scripting/source/provider/BrowseNodeFactoryImpl.cxx | 2 +- scripting/source/provider/ProviderCache.cxx | 2 +- scripting/source/provider/URIHelper.cxx | 2 +- scripting/source/vbaevents/eventhelper.cxx | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'scripting') diff --git a/scripting/source/basprov/basmethnode.cxx b/scripting/source/basprov/basmethnode.cxx index d053fa11b809..688b4ef2140f 100644 --- a/scripting/source/basprov/basmethnode.cxx +++ b/scripting/source/basprov/basmethnode.cxx @@ -203,7 +203,7 @@ namespace basprov sDocURL = xModel->getURL(); if ( sDocURL.isEmpty() ) { - Sequence < PropertyValue > aProps = xModel->getArgs(); + const Sequence < PropertyValue > aProps = xModel->getArgs(); // TODO: according to MBA the property 'Title' may change in future const PropertyValue* pProp = std::find_if(aProps.begin(), aProps.end(), [](const PropertyValue& rProp) { return rProp.Name == "Title"; }); diff --git a/scripting/source/provider/BrowseNodeFactoryImpl.cxx b/scripting/source/provider/BrowseNodeFactoryImpl.cxx index d3c1c1f0b394..1366f0d24f6b 100644 --- a/scripting/source/provider/BrowseNodeFactoryImpl.cxx +++ b/scripting/source/provider/BrowseNodeFactoryImpl.cxx @@ -102,7 +102,7 @@ public: Sequence< Reference < browse::XBrowseNode > > result( numChildren ); sal_Int32 index = 0; - for ( Sequence< Reference < browse::XBrowseNode > >& children : seqs ) + for ( const Sequence< Reference < browse::XBrowseNode > >& children : seqs ) { std::copy(children.begin(), children.end(), std::next(result.begin(), index)); index += children.getLength(); diff --git a/scripting/source/provider/ProviderCache.cxx b/scripting/source/provider/ProviderCache.cxx index f5bdfe1ac11b..be134725635e 100644 --- a/scripting/source/provider/ProviderCache.cxx +++ b/scripting/source/provider/ProviderCache.cxx @@ -144,7 +144,7 @@ ProviderCache::populateCache() Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW ); Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW ); - Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames(); + const Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames(); if ( serviceNames.hasElements() ) { diff --git a/scripting/source/provider/URIHelper.cxx b/scripting/source/provider/URIHelper.cxx index 9247f4b9fa98..3fa98db81355 100644 --- a/scripting/source/provider/URIHelper.cxx +++ b/scripting/source/provider/URIHelper.cxx @@ -142,7 +142,7 @@ ScriptingFrameworkURIHelper::initBaseURI() return false; } - uno::Sequence< OUString > children = + const uno::Sequence< OUString > children = m_xSimpleFileAccess->getFolderContents( uri, true ); auto pChild = std::find_if(children.begin(), children.end(), [&test](const OUString& child) { diff --git a/scripting/source/vbaevents/eventhelper.cxx b/scripting/source/vbaevents/eventhelper.cxx index b6cf579bd5de..9bc86023c2e8 100644 --- a/scripting/source/vbaevents/eventhelper.cxx +++ b/scripting/source/vbaevents/eventhelper.cxx @@ -393,7 +393,7 @@ ScriptEventHelper::getEventListeners() const for ( const Type& listType : aControlListeners ) { OUString sFullTypeName = listType.getTypeName(); - Sequence< OUString > sMeths = + const Sequence< OUString > sMeths = comphelper::getEventMethodsForType( listType ); std::transform(sMeths.begin(), sMeths.end(), std::back_inserter(eventMethods), [&sFullTypeName](const OUString& rMeth) -> OUString { return sFullTypeName + DELIM + rMeth; }); -- cgit