Skip to content

Commit c259918

Browse files
JohnCoconutminggo
authored andcommitted
Optimize cases where loop variable is unecessarily copied in a range-for loop. (#19637) (#19640)
Pass by const reference when appropriate.
1 parent d89f1d0 commit c259918

File tree

15 files changed

+24
-23
lines changed

15 files changed

+24
-23
lines changed

cocos/2d/CCSpriteFrameCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ bool SpriteFrameCache::PlistFramesCache::erasePlistIndex(const std::string &plis
773773
if (it == _indexPlist2Frames.end()) return false;
774774

775775
auto &frames = it->second;
776-
for (auto f : frames)
776+
for (const auto& f : frames)
777777
{
778778
// !!do not!! call `_spriteFrames.erase(f);` to erase SpriteFrame
779779
// only erase index here

cocos/2d/CCTMXXMLParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char *name)
746746
}
747747

748748
uint32_t* bufferPtr = reinterpret_cast<uint32_t*>(buffer);
749-
for(auto gidToken : gidTokens) {
749+
for(const auto& gidToken : gidTokens) {
750750
auto tileGid = (uint32_t)strtoul(gidToken.c_str(), nullptr, 10);
751751
*bufferPtr = tileGid;
752752
bufferPtr++;

cocos/3d/CCAnimate3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void Animate3D::update(float t)
392392
float prekeyTime = lastTime * getDuration() * _frameRate;
393393
float keyTime = t * getDuration() * _frameRate;
394394
std::vector<Animate3DDisplayedEventInfo*> eventInfos;
395-
for (auto keyFrame : _keyFrameUserInfos)
395+
for (const auto& keyFrame : _keyFrameUserInfos)
396396
{
397397
if ((!_playReverse && keyFrame.first >= prekeyTime && keyFrame.first < keyTime)
398398
|| (_playReverse && keyFrame.first >= keyTime && keyFrame.first < prekeyTime))

cocos/3d/CCAnimation3D.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ Animation3D::Animation3D()
8787

8888
Animation3D::~Animation3D()
8989
{
90-
for (auto itor : _boneCurves) {
91-
CC_SAFE_DELETE(itor.second);
90+
for (const auto& itor : _boneCurves) {
91+
Curve* curve = itor.second;
92+
CC_SAFE_DELETE(curve);
9293
}
9394
}
9495

cocos/3d/CCBundle3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ std::vector<Vec3> Bundle3D::getTrianglesList(const std::string& path)
22922292
Bundle3D::destroyBundle(bundle);
22932293
for (auto iter : meshs.meshDatas){
22942294
int preVertexSize = iter->getPerVertexSize() / sizeof(float);
2295-
for (auto indexArray : iter->subMeshIndices){
2295+
for (const auto& indexArray : iter->subMeshIndices){
22962296
for (auto i : indexArray){
22972297
trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1], iter->vertex[i * preVertexSize + 2]));
22982298
}

cocos/3d/CCSkeleton3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void Bone3D::updateLocalMat()
202202
Quaternion quat(Quaternion::ZERO);
203203

204204
float total = 0.f;
205-
for (auto it: _blendStates) {
205+
for (const auto& it: _blendStates) {
206206
total += it.weight;
207207
}
208208
if (total)

cocos/3d/CCTerrain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ bool Terrain::Chunk::getIntersectPointWithRay(const Ray& ray, Vec3& intersectPoi
13551355

13561356
float minDist = FLT_MAX;
13571357
bool isFind = false;
1358-
for (auto triangle : _trianglesList)
1358+
for (const auto& triangle : _trianglesList)
13591359
{
13601360
Vec3 p;
13611361
if (triangle.getIntersectPoint(ray, p))

cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ ActionTimeline* ActionTimeline::clone() const
176176
newAction->setDuration(_duration);
177177
newAction->setTimeSpeed(_timeSpeed);
178178

179-
for (auto timelines : _timelineMap)
179+
for (const auto& timelines : _timelineMap)
180180
{
181181
for(auto timeline : timelines.second)
182182
{
@@ -185,7 +185,7 @@ ActionTimeline* ActionTimeline::clone() const
185185
}
186186
}
187187

188-
for( auto info : _animationInfos)
188+
for(const auto& info : _animationInfos)
189189
{
190190
newAction->addAnimationInfo(info.second);
191191
}
@@ -416,7 +416,7 @@ void ActionTimeline::emitFrameEndCallFuncs(int frameIndex)
416416
if (clipEndCallsIter != _frameEndCallFuncs.end())
417417
{
418418
auto clipEndCalls = (*clipEndCallsIter).second;
419-
for (auto call : clipEndCalls)
419+
for (const auto& call : clipEndCalls)
420420
(call).second();
421421
}
422422
}

cocos/editor-support/cocostudio/CCArmatureDataManager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,22 @@ void ArmatureDataManager::removeArmatureFileInfo(const std::string& configFilePa
9595
{
9696
if (RelativeData *data = getRelativeData(configFilePath))
9797
{
98-
for (std::string str : data->armatures)
98+
for (const std::string& str : data->armatures)
9999
{
100100
removeArmatureData(str);
101101
}
102102

103-
for (std::string str : data->animations)
103+
for (const std::string& str : data->animations)
104104
{
105105
removeAnimationData(str);
106106
}
107107

108-
for (std::string str : data->textures)
108+
for (const std::string& str : data->textures)
109109
{
110110
removeTextureData(str);
111111
}
112112

113-
for (std::string str : data->plistFiles)
113+
for (const std::string& str : data->plistFiles)
114114
{
115115
SpriteFrameCacheHelper::getInstance()->removeSpriteFrameFromFile(str);
116116
}

cocos/physics/CCPhysicsJoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool PhysicsJoint::initJoint()
180180

181181
void PhysicsJoint::flushDelayTasks()
182182
{
183-
for (auto tsk : _delayTasks)
183+
for (const auto& tsk : _delayTasks)
184184
{
185185
tsk();
186186
}

0 commit comments

Comments
 (0)