From ed2db809900d1de4b0bc11e131b23dec5203e9a4 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 20 Apr 2024 02:46:39 +0100 Subject: [PATCH] Codechange: Use map.emplace() instead of map.insert(std::pair). This avoids a copy of the pair into the map. --- src/cargomonitor.cpp | 3 +-- src/cargotype.cpp | 2 +- src/network/network_content.cpp | 2 +- src/newgrf_gui.cpp | 3 +-- src/saveload/cargomonitor_sl.cpp | 6 ++---- src/saveload/station_sl.cpp | 2 +- src/station_cmd.cpp | 4 ++-- 7 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/cargomonitor.cpp b/src/cargomonitor.cpp index dc5ab7643f..ab8dc4a06f 100644 --- a/src/cargomonitor.cpp +++ b/src/cargomonitor.cpp @@ -71,8 +71,7 @@ static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, b CargoMonitorMap::iterator iter = monitor_map.find(monitor); if (iter == monitor_map.end()) { if (keep_monitoring) { - std::pair p(monitor, 0); - monitor_map.insert(p); + monitor_map.emplace(monitor, 0); } return 0; } else { diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 98d53a9d1c..870369dbbd 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -99,7 +99,7 @@ void BuildCargoLabelMap() /* Label already exists, don't addd again. */ if (CargoSpec::label_map.count(cs.label) != 0) continue; - CargoSpec::label_map.insert(std::make_pair(cs.label, cs.Index())); + CargoSpec::label_map.emplace(cs.label, cs.Index()); } } diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index 6db744dffe..2395180a27 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -72,7 +72,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet &p) for (uint i = 0; i < dependency_count; i++) { ContentID dependency_cid = (ContentID)p.Recv_uint32(); ci->dependencies.push_back(dependency_cid); - this->reverse_dependency_map.insert({ dependency_cid, ci->id }); + this->reverse_dependency_map.emplace(dependency_cid, ci->id); } uint tag_count = p.Recv_uint8(); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index ef8950a5ce..146f890189 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -591,8 +591,7 @@ typedef std::map GrfIdMap; ///< Map of grfid to the static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map) { while (c != nullptr) { - std::pair p(c->ident.grfid, c); - grfid_map->insert(p); + grfid_map->emplace(c->ident.grfid, c); c = c->next; } } diff --git a/src/saveload/cargomonitor_sl.cpp b/src/saveload/cargomonitor_sl.cpp index 61f6e0eec4..c932ea18d7 100644 --- a/src/saveload/cargomonitor_sl.cpp +++ b/src/saveload/cargomonitor_sl.cpp @@ -81,8 +81,7 @@ struct CMDLChunkHandler : ChunkHandler { if (fix) storage.number = FixupCargoMonitor(storage.number); - std::pair p(storage.number, storage.amount); - _cargo_deliveries.insert(p); + _cargo_deliveries.emplace(storage.number, storage.amount); } } }; @@ -125,8 +124,7 @@ struct CMPUChunkHandler : ChunkHandler { if (fix) storage.number = FixupCargoMonitor(storage.number); - std::pair p(storage.number, storage.amount); - _cargo_pickups.insert(p); + _cargo_pickups.emplace(storage.number, storage.amount); } } }; diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 8c5b962ead..0db9509e53 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -318,7 +318,7 @@ public: for (uint32_t j = 0; j < num_flows; ++j) { SlObject(&flow, this->GetLoadDescription()); if (fs == nullptr || prev_source != flow.source) { - fs = &(ge->flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second); + fs = &(ge->flows.emplace(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second; } else { fs->AppendShare(flow.via, flow.share, flow.restricted); } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index aece206dfc..e807e8d9b1 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -4810,7 +4810,7 @@ void FlowStatMap::AddFlow(StationID origin, StationID via, uint flow) { FlowStatMap::iterator origin_it = this->find(origin); if (origin_it == this->end()) { - this->insert(std::make_pair(origin, FlowStat(via, flow))); + this->emplace(origin, FlowStat(via, flow)); } else { origin_it->second.ChangeShare(via, flow); assert(!origin_it->second.GetShares()->empty()); @@ -4831,7 +4831,7 @@ void FlowStatMap::PassOnFlow(StationID origin, StationID via, uint flow) if (prev_it == this->end()) { FlowStat fs(via, flow); fs.AppendShare(INVALID_STATION, flow); - this->insert(std::make_pair(origin, fs)); + this->emplace(origin, fs); } else { prev_it->second.ChangeShare(via, flow); prev_it->second.ChangeShare(INVALID_STATION, flow);