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: case Node::KIND_LOCALIZED_PROPERTY:
{ {
NodeMap & members = node_->getMembers(); NodeMap & members = node_->getMembers();
NodeMap::iterator i(members.find(localizedName_)); auto [i, bInserted] = members.insert(NodeMap::value_type(localizedName_, nullptr));
LocalizedValueNode *pLVNode; LocalizedValueNode *pLVNode;
if (i == members.end()) { if (bInserted) {
pLVNode = new LocalizedValueNode(layer_); pLVNode = new LocalizedValueNode(layer_);
members.insert( i->second = pLVNode;
NodeMap::value_type(localizedName_, pLVNode ));
} else { } else {
pLVNode = static_cast< LocalizedValueNode * >(i->second.get()); pLVNode = static_cast< LocalizedValueNode * >(i->second.get());
} }

View File

@@ -3345,10 +3345,8 @@ void ChartExport::_exportAxis(
} }
// only export each axis only once non-deleted // only export each axis only once non-deleted
bool bDeleted = maExportedAxis.find(rAxisIdPair.nAxisType) != maExportedAxis.end(); auto aItInsertedPair = maExportedAxis.insert(rAxisIdPair.nAxisType);
bool bDeleted = !aItInsertedPair.second;
if (!bDeleted)
maExportedAxis.insert(rAxisIdPair.nAxisType);
pFS->singleElement(FSNS(XML_c, XML_delete), XML_val, !bDeleted && bVisible ? "0" : "1"); 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; return 0;
if( stringIdsMap == nullptr ) if( stringIdsMap == nullptr )
stringIdsMap = new std::unordered_map<const rtl_uString*, int>; 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; int newId = stringIdsMap->size() + 1;
stringIdsMap->insert( std::pair( string, newId )); auto aItInsertedPair = stringIdsMap->insert( std::pair( string, newId ));
return newId; return aItInsertedPair.first->second;
} }
void DynamicKernelArgument::ClearStringIds() void DynamicKernelArgument::ClearStringIds()

View File

@@ -768,8 +768,7 @@ void ScHTMLLayoutParser::SetWidths()
for (auto it = pLocalColOffset->begin(); it != pLocalColOffset->end(); ++it) for (auto it = pLocalColOffset->begin(); it != pLocalColOffset->end(); ++it)
{ {
// Only exact offsets, do not use MakeColNoRef(). // 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()) if (it != pImpl->m_Items.end())
{ {
// Replace Item // Replace Item
pImpl->m_Items.erase( it ); it->second = std::unique_ptr<SfxPoolItem>(pItem);
pImpl->m_Items.insert(std::make_pair(nWhich, std::unique_ptr<SfxPoolItem>(pItem)));
// if active, notify Bindings // if active, notify Bindings
SfxDispatcher *pDispat = GetDispatcher(); SfxDispatcher *pDispat = GetDispatcher();

View File

@@ -1379,10 +1379,10 @@ drawinglayer::primitive2d::Primitive2DContainer Array::CreateB2DPrimitiveRange(
GetMergedRange(nColLeft, nRowTop, nColRight, nRowBottom, nCol, nRow); GetMergedRange(nColLeft, nRowTop, nColRight, nRowBottom, nCol, nRow);
const sal_Int32 nIndexOfMergedCell(mxImpl->GetIndex(nColLeft, nRowTop)); 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 // not found, so not yet handled.
aMergedCells.insert(nIndexOfMergedCell);
// Get and check if diagonal styles are used // Get and check if diagonal styles are used
// Note: For GetCellStyleBLTR below I tried to use nRowBottom // Note: For GetCellStyleBLTR below I tried to use nRowBottom

View File

@@ -925,11 +925,9 @@ void SvxCSS1Parser::InsertMapEntry( const OUString& rKey,
const SvxCSS1PropertyInfo& rProp, const SvxCSS1PropertyInfo& rProp,
CSS1Map& rMap ) CSS1Map& rMap )
{ {
CSS1Map::iterator itr = rMap.find(rKey); auto [itr,inserted] = rMap.insert(std::make_pair(rKey, nullptr));
if (itr == rMap.end()) if (inserted)
{ itr->second = std::make_unique<SvxCSS1MapEntry>(rItemSet, rProp);
rMap.insert(std::make_pair(rKey, std::make_unique<SvxCSS1MapEntry>(rItemSet, rProp)));
}
else else
{ {
SvxCSS1MapEntry *const p = itr->second.get(); 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) 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(); const sal_uInt16 nId = m_aRedlineTable.size();
m_aRedlineTable.insert(std::pair<OUString, sal_uInt16>(rAuthor, nId)); // insert if we don't already have one
return nId; 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) const OUString* RtfExport::GetRedline(sal_uInt16 nId)