summaryrefslogtreecommitdiff
path: root/comphelper
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 /comphelper
parent17d3cacfb9675268e709cfc95771ad4ce8bde75a (diff)
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 'comphelper')
-rw-r--r--comphelper/qa/unit/base64_test.cxx12
-rw-r--r--comphelper/source/property/opropertybag.cxx21
-rw-r--r--comphelper/source/property/propertycontainerhelper.cxx10
3 files changed, 22 insertions, 21 deletions
diff --git a/comphelper/qa/unit/base64_test.cxx b/comphelper/qa/unit/base64_test.cxx
index dd89951e6430..31e865a370a6 100644
--- a/comphelper/qa/unit/base64_test.cxx
+++ b/comphelper/qa/unit/base64_test.cxx
@@ -72,18 +72,18 @@ void Base64Test::testBase64Decode()
uno::Sequence<sal_Int8> expectedSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
comphelper::Base64::decode(decodedSequence, "AAAAAAABAgM=");
- CPPUNIT_ASSERT(
- std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
+ CPPUNIT_ASSERT(std::equal(std::cbegin(expectedSequence), std::cend(expectedSequence),
+ std::cbegin(decodedSequence)));
expectedSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
comphelper::Base64::decode(decodedSequence, "BQIDAAABAgM=");
- CPPUNIT_ASSERT(
- std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
+ CPPUNIT_ASSERT(std::equal(std::cbegin(expectedSequence), std::cend(expectedSequence),
+ std::cbegin(decodedSequence)));
expectedSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
comphelper::Base64::decode(decodedSequence, "yB9NbwABAgM=");
- CPPUNIT_ASSERT(
- std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
+ CPPUNIT_ASSERT(std::equal(std::cbegin(expectedSequence), std::cend(expectedSequence),
+ std::cbegin(decodedSequence)));
}
void Base64Test::testBase64EncodeForOStringBuffer()
diff --git a/comphelper/source/property/opropertybag.cxx b/comphelper/source/property/opropertybag.cxx
index 62dd474c962f..f84d509847eb 100644
--- a/comphelper/source/property/opropertybag.cxx
+++ b/comphelper/source/property/opropertybag.cxx
@@ -83,14 +83,14 @@ namespace comphelper
&& (_rArguments[1] >>= AllowEmptyPropertyName)
&& (_rArguments[2] >>= AutomaticAddition))
{
- m_aAllowedTypes.insert(aTypes.begin(), aTypes.end());
+ m_aAllowedTypes.insert(std::cbegin(aTypes), std::cend(aTypes));
m_bAutoAddProperties = AutomaticAddition;
} else {
::comphelper::NamedValueCollection aArguments( _rArguments );
if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
- m_aAllowedTypes.insert( aTypes.begin(), aTypes.end());
+ m_aAllowedTypes.insert(std::cbegin(aTypes), std::cend(aTypes));
aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
aArguments.get_ensureType( "AllowEmptyPropertyName",
@@ -377,8 +377,8 @@ namespace comphelper
// their names
Sequence< OUString > aNames( aProperties.getLength() );
std::transform(
- aProperties.begin(),
- aProperties.end(),
+ std::cbegin(aProperties),
+ std::cend(aProperties),
aNames.getArray(),
TransformPropertyToName< Property >()
);
@@ -425,17 +425,18 @@ namespace comphelper
{
// sort (the XMultiPropertySet interface requires this)
Sequence< PropertyValue > aProperties( _rProps );
+ auto [begin, end] = toNonConstRange(aProperties);
std::sort(
- aProperties.begin(),
- aProperties.end(),
+ begin,
+ end,
ComparePropertyValueByName()
);
// a sequence of names
Sequence< OUString > aNames( aProperties.getLength() );
std::transform(
- aProperties.begin(),
- aProperties.end(),
+ std::cbegin(aProperties),
+ std::cend(aProperties),
aNames.getArray(),
TransformPropertyToName< PropertyValue >()
);
@@ -479,8 +480,8 @@ namespace comphelper
// a sequence of values
Sequence< Any > aValues( aProperties.getLength() );
std::transform(
- aProperties.begin(),
- aProperties.end(),
+ std::cbegin(aProperties),
+ std::cend(aProperties),
aValues.getArray(),
ExtractPropertyValue()
);
diff --git a/comphelper/source/property/propertycontainerhelper.cxx b/comphelper/source/property/propertycontainerhelper.cxx
index 1ba31ec8258f..73dcbd2e862b 100644
--- a/comphelper/source/property/propertycontainerhelper.cxx
+++ b/comphelper/source/property/propertycontainerhelper.cxx
@@ -469,15 +469,15 @@ void OPropertyContainerHelper::describeProperties(Sequence< Property >& _rProps)
}
// as our property vector is sorted by handles, not by name, we have to sort aOwnProps
- std::sort(aOwnProps.begin(), aOwnProps.end(), PropertyCompareByName());
+ auto [begin, end] = toNonConstRange(aOwnProps);
+ std::sort(begin, end, PropertyCompareByName());
// unfortunately the STL merge function does not allow the output range to overlap one of the input ranges,
// so we need an extra sequence
- Sequence< Property > aOutput;
- aOutput.realloc(_rProps.getLength() + aOwnProps.getLength());
+ Sequence< Property > aOutput(_rProps.getLength() + aOwnProps.getLength());
// do the merge
- std::merge( _rProps.begin(), _rProps.end(), // input 1
- aOwnProps.begin(), aOwnProps.end(), // input 2
+ std::merge( std::cbegin(_rProps), std::cend(_rProps), // input 1
+ std::cbegin(aOwnProps), std::cend(aOwnProps), // input 2
aOutput.getArray(), // output
PropertyCompareByName() // compare operator
);