optimise find/insert calls to maps

Instead of doing two lookups, we can just call insert and
then update the mapped-to value if the insert actually succeeded.

Change-Id: I3df3b98f0debe6bf74c739dd5850e7714d9c2789
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151030
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2023-04-26 08:41:23 +02:00 committed by Noel Grandin
parent 2eb3922750
commit a33922e38b
8 changed files with 19 additions and 31 deletions

View File

@ -352,12 +352,11 @@ bool ValueParser::endElement() {
case Node::KIND_LOCALIZED_PROPERTY:
{
NodeMap & members = node_->getMembers();
NodeMap::iterator i(members.find(localizedName_));
auto [i, bInserted] = members.insert(NodeMap::value_type(localizedName_, nullptr));
LocalizedValueNode *pLVNode;
if (i == members.end()) {
if (bInserted) {
pLVNode = new LocalizedValueNode(layer_);
members.insert(
NodeMap::value_type(localizedName_, pLVNode ));
i->second = pLVNode;
} else {
pLVNode = static_cast< LocalizedValueNode * >(i->second.get());
}

View File

@ -3345,10 +3345,8 @@ void ChartExport::_exportAxis(
}
// only export each axis only once non-deleted
bool bDeleted = maExportedAxis.find(rAxisIdPair.nAxisType) != maExportedAxis.end();
if (!bDeleted)
maExportedAxis.insert(rAxisIdPair.nAxisType);
auto aItInsertedPair = maExportedAxis.insert(rAxisIdPair.nAxisType);
bool bDeleted = !aItInsertedPair.second;
pFS->singleElement(FSNS(XML_c, XML_delete), XML_val, !bDeleted && bVisible ? "0" : "1");

View File

@ -114,12 +114,9 @@ int DynamicKernelArgument::GetStringId( const rtl_uString* string )
return 0;
if( stringIdsMap == nullptr )
stringIdsMap = new std::unordered_map<const rtl_uString*, int>;
std::unordered_map<const rtl_uString*, int>::iterator it = stringIdsMap->find( string );
if( it != stringIdsMap->end())
return it->second;
int newId = stringIdsMap->size() + 1;
stringIdsMap->insert( std::pair( string, newId ));
return newId;
auto aItInsertedPair = stringIdsMap->insert( std::pair( string, newId ));
return aItInsertedPair.first->second;
}
void DynamicKernelArgument::ClearStringIds()

View File

@ -768,8 +768,7 @@ void ScHTMLLayoutParser::SetWidths()
for (auto it = pLocalColOffset->begin(); it != pLocalColOffset->end(); ++it)
{
// Only exact offsets, do not use MakeColNoRef().
if (maColOffset.find(*it) == maColOffset.end())
maColOffset.insert(*it);
maColOffset.insert(*it);
}
}
}

View File

@ -170,8 +170,7 @@ void SfxShell::PutItem
if (it != pImpl->m_Items.end())
{
// Replace Item
pImpl->m_Items.erase( it );
pImpl->m_Items.insert(std::make_pair(nWhich, std::unique_ptr<SfxPoolItem>(pItem)));
it->second = std::unique_ptr<SfxPoolItem>(pItem);
// if active, notify Bindings
SfxDispatcher *pDispat = GetDispatcher();

View File

@ -1379,10 +1379,10 @@ drawinglayer::primitive2d::Primitive2DContainer Array::CreateB2DPrimitiveRange(
GetMergedRange(nColLeft, nRowTop, nColRight, nRowBottom, nCol, nRow);
const sal_Int32 nIndexOfMergedCell(mxImpl->GetIndex(nColLeft, nRowTop));
if(aMergedCells.end() == aMergedCells.find(nIndexOfMergedCell))
auto aItInsertedPair = aMergedCells.insert(nIndexOfMergedCell);
if(aItInsertedPair.second)
{
// not found, so not yet handled. Add now to mark as handled
aMergedCells.insert(nIndexOfMergedCell);
// not found, so not yet handled.
// Get and check if diagonal styles are used
// Note: For GetCellStyleBLTR below I tried to use nRowBottom

View File

@ -925,11 +925,9 @@ void SvxCSS1Parser::InsertMapEntry( const OUString& rKey,
const SvxCSS1PropertyInfo& rProp,
CSS1Map& rMap )
{
CSS1Map::iterator itr = rMap.find(rKey);
if (itr == rMap.end())
{
rMap.insert(std::make_pair(rKey, std::make_unique<SvxCSS1MapEntry>(rItemSet, rProp)));
}
auto [itr,inserted] = rMap.insert(std::make_pair(rKey, nullptr));
if (inserted)
itr->second = std::make_unique<SvxCSS1MapEntry>(rItemSet, rProp);
else
{
SvxCSS1MapEntry *const p = itr->second.get();

View File

@ -1432,13 +1432,11 @@ OString* RtfExport::GetStyle(sal_uInt16 nId)
sal_uInt16 RtfExport::GetRedline(const OUString& rAuthor)
{
auto it = m_aRedlineTable.find(rAuthor);
if (it != m_aRedlineTable.end())
return it->second;
const sal_uInt16 nId = m_aRedlineTable.size();
m_aRedlineTable.insert(std::pair<OUString, sal_uInt16>(rAuthor, nId));
return nId;
// insert if we don't already have one
auto[it, inserted] = m_aRedlineTable.insert(std::pair<OUString, sal_uInt16>(rAuthor, nId));
(void)inserted;
return it->second;
}
const OUString* RtfExport::GetRedline(sal_uInt16 nId)