Skip to content

Commit e18cdc0

Browse files
authored
use emplace_back and reserve for some vectors (#1724)
1 parent e195a83 commit e18cdc0

File tree

17 files changed

+43
-25
lines changed

17 files changed

+43
-25
lines changed

src/Ext/Building/Body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ const std::vector<CellStruct> BuildingExt::GetFoundationCells(BuildingClass* con
427427

428428
for (auto i = occupyHeight; i > 0; --i)
429429
{
430-
foundationCells.push_back(actualCell);
430+
foundationCells.emplace_back(actualCell);
431431
--actualCell.X;
432432
--actualCell.Y;
433433
}

src/Ext/Bullet/Trajectories/PhobosTrajectory.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
205205
std::vector<CellStruct> recCells;
206206
const auto cellNums = (std::abs(topEndCell.Y - bottomStaCell.Y) + 1) * (std::abs(rightMidCell.X - leftMidCell.X) + 1);
207207
recCells.reserve(cellNums);
208-
recCells.push_back(bottomStaCell);
208+
recCells.emplace_back(bottomStaCell);
209209

210210
if (bottomStaCell == leftMidCell || bottomStaCell == rightMidCell) // A straight line
211211
{
@@ -222,24 +222,24 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
222222
{
223223
mTheCurN -= middleThePace.X;
224224
middleCurCell.Y += middleTheUnit.Y;
225-
recCells.push_back(middleCurCell);
225+
recCells.emplace_back(middleCurCell);
226226
}
227227
else if (mTheCurN < 0)
228228
{
229229
mTheCurN += middleThePace.Y;
230230
middleCurCell.X += middleTheUnit.X;
231-
recCells.push_back(middleCurCell);
231+
recCells.emplace_back(middleCurCell);
232232
}
233233
else
234234
{
235235
mTheCurN += middleThePace.Y - middleThePace.X;
236236
middleCurCell.X += middleTheUnit.X;
237-
recCells.push_back(middleCurCell);
237+
recCells.emplace_back(middleCurCell);
238238
middleCurCell.X -= middleTheUnit.X;
239239
middleCurCell.Y += middleTheUnit.Y;
240-
recCells.push_back(middleCurCell);
240+
recCells.emplace_back(middleCurCell);
241241
middleCurCell.X += middleTheUnit.X;
242-
recCells.push_back(middleCurCell);
242+
recCells.emplace_back(middleCurCell);
243243
}
244244
}
245245
}
@@ -293,7 +293,7 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
293293
}
294294
else
295295
{
296-
recCells.push_back(leftCurCell);
296+
recCells.emplace_back(leftCurCell);
297297
break;
298298
}
299299
}
@@ -333,7 +333,7 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
333333
}
334334

335335
if (leftCurCell != rightCurCell) // Avoid double counting cells.
336-
recCells.push_back(leftCurCell);
336+
recCells.emplace_back(leftCurCell);
337337
}
338338

339339
while (rightCurCell != topEndCell) // Right
@@ -351,7 +351,7 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
351351
}
352352
else
353353
{
354-
recCells.push_back(rightCurCell);
354+
recCells.emplace_back(rightCurCell);
355355
break;
356356
}
357357
}
@@ -391,15 +391,15 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
391391
}
392392

393393
if (rightCurCell != leftCurCell) // Avoid double counting cells.
394-
recCells.push_back(rightCurCell);
394+
recCells.emplace_back(rightCurCell);
395395
}
396396

397397
middleCurCell = leftCurCell;
398398
middleCurCell.X += 1;
399399

400400
while (middleCurCell.X < rightCurCell.X) // Center
401401
{
402-
recCells.push_back(middleCurCell);
402+
recCells.emplace_back(middleCurCell);
403403
middleCurCell.X += 1;
404404
}
405405

@@ -408,15 +408,15 @@ std::vector<CellStruct> PhobosTrajectoryType::GetCellsInRectangle(CellStruct bot
408408
leftContinue = false;
409409
left2ndCurN -= left2ndPace.X;
410410
leftCurCell.Y += left2ndUnit.Y;
411-
recCells.push_back(leftCurCell);
411+
recCells.emplace_back(leftCurCell);
412412
}
413413

414414
if (rightContinue) // Continue Top Right Side
415415
{
416416
rightContinue = false;
417417
right2ndCurN -= right2ndPace.X;
418418
rightCurCell.Y += right2ndUnit.Y;
419-
recCells.push_back(rightCurCell);
419+
recCells.emplace_back(rightCurCell);
420420
}
421421
}
422422
}

src/Ext/Bullet/Trajectories/StraightTrajectory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
909909
this->TheCasualty.erase(ID);
910910

911911
std::vector<TechnoClass*> validTargets;
912+
validTargets.reserve(validTechnos.size());
912913

913914
// checking for duplicate
914915
for (const auto& pTechno : validTechnos)

src/Ext/House/Body.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ void HouseExt::ExtData::UpdateVehicleProduction()
3535
auto& bestChoicesNaval = HouseExt::AIProduction_BestChoicesNaval;
3636

3737
auto const count = static_cast<unsigned int>(UnitTypeClass::Array.Count);
38+
creationFrames.reserve(count);
3839
creationFrames.assign(count, 0x7FFFFFFF);
40+
values.reserve(count);
3941
values.assign(count, 0);
4042

4143
for (auto currentTeam : TeamClass::Array)

src/Ext/Rules/Body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void RulesExt::ExtData::LoadBeforeTypeData(RulesClass* pThis, CCINIClass* pINI)
276276
this->AnimCraterDestroyTiberium.Read(exINI, GameStrings::General, "AnimCraterDestroyTiberium");
277277

278278
this->BerzerkTargeting.Read(exINI, GameStrings::CombatDamage, "BerzerkTargeting");
279-
279+
280280
// Section AITargetTypes
281281
int itemsCount = pINI->GetKeyCount("AITargetTypes");
282282
for (int i = 0; i < itemsCount; ++i)

src/Ext/Rules/Body.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class RulesExt
234234
int TintColorIronCurtain;
235235
int TintColorForceShield;
236236
int TintColorBerserk;
237-
237+
238238
ExtData(RulesClass* OwnerObject) : Extension<RulesClass>(OwnerObject)
239239
, Storage_TiberiumIndex { -1 }
240240
, HarvesterDumpAmount { 0.0f }
@@ -408,7 +408,7 @@ class RulesExt
408408
, HarvesterScanAfterUnload { false }
409409

410410
, AnimCraterDestroyTiberium { true }
411-
411+
412412
, BerzerkTargeting { AffectedHouse::All }
413413

414414
, TintColorIronCurtain { 0 }

src/Ext/SWType/Body.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void SWTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
170170
if (this->LimboDelivery_RandomWeightsData.size() > i)
171171
this->LimboDelivery_RandomWeightsData[i] = std::move(weights);
172172
else
173-
this->LimboDelivery_RandomWeightsData.push_back(std::move(weights));
173+
this->LimboDelivery_RandomWeightsData.emplace_back(std::move(weights));
174174
}
175175

176176
ValueableVector<int> weights;
@@ -180,7 +180,7 @@ void SWTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
180180
if (this->LimboDelivery_RandomWeightsData.size())
181181
this->LimboDelivery_RandomWeightsData[0] = std::move(weights);
182182
else
183-
this->LimboDelivery_RandomWeightsData.push_back(std::move(weights));
183+
this->LimboDelivery_RandomWeightsData.emplace_back(std::move(weights));
184184
}
185185

186186
// SW.Next.RandomWeights
@@ -196,7 +196,7 @@ void SWTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
196196
if (this->SW_Next_RandomWeightsData.size() > i)
197197
this->SW_Next_RandomWeightsData[i] = std::move(weights2);
198198
else
199-
this->SW_Next_RandomWeightsData.push_back(std::move(weights2));
199+
this->SW_Next_RandomWeightsData.emplace_back(std::move(weights2));
200200
}
201201

202202
ValueableVector<int> weights2;
@@ -206,7 +206,7 @@ void SWTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
206206
if (this->SW_Next_RandomWeightsData.size())
207207
this->SW_Next_RandomWeightsData[0] = std::move(weights2);
208208
else
209-
this->SW_Next_RandomWeightsData.push_back(std::move(weights2));
209+
this->SW_Next_RandomWeightsData.emplace_back(std::move(weights2));
210210
}
211211

212212
this->Detonate_Warhead.Read<true>(exINI, pSection, "Detonate.Warhead");

src/Ext/SWType/SWHelpers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ std::vector<int> SWTypeExt::ExtData::WeightedRollsHandler(ValueableVector<float>
1717
rollOnce = true;
1818
}
1919

20+
indices.reserve(rollsSize);
21+
2022
for (size_t i = 0; i < rollsSize; i++)
2123
{
2224
this->RandomBuffer = ScenarioClass::Instance->Random.RandomDouble();
@@ -32,6 +34,7 @@ std::vector<int> SWTypeExt::ExtData::WeightedRollsHandler(ValueableVector<float>
3234
if (size_t(index) < size)
3335
indices.push_back(index);
3436
}
37+
3538
return indices;
3639
}
3740

src/Ext/Script/Mission.Attack.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,7 @@ void ScriptExt::Mission_Attack_List1Random(TeamClass* pTeam, int calcThreatMode,
13791379
if (idxSelectedObject < 0 && objectsList.size() > 0 && !selected)
13801380
{
13811381
const auto pFirstUnit = pTeam->FirstUnit;
1382+
validIndexes.reserve(TechnoClass::Array.Count * objectsList.size());
13821383

13831384
// Finding the objects from the list that actually exists in the map
13841385
for (int i = 0; i < TechnoClass::Array.Count; i++)

src/Ext/Script/Mission.Move.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ void ScriptExt::Mission_Move_List1Random(TeamClass* pTeam, int calcThreatMode, b
436436
if (idxSelectedObject < 0 && objectsList.size() > 0 && !selected)
437437
{
438438
const auto pFirstUnit = pTeam->FirstUnit;
439+
validIndexes.reserve(TechnoClass::Array.Count * objectsList.size());
439440

440441
// Finding the objects from the list that actually exists in the map
441442
for (int i = 0; i < TechnoClass::Array.Count; i++)

0 commit comments

Comments
 (0)