Skip to content

Commit bce0eab

Browse files
JohnCoconuthuangwei1024
authored andcommitted
Optimize cases where loop variable is unecessarily copied in a range-for loop. (cocos2d#19637)
Pass by const reference when appropriate.
1 parent e6a3734 commit bce0eab

File tree

16 files changed

+25
-24
lines changed

16 files changed

+25
-24
lines changed

cocos/2d/CCSpriteFrameCache.cpp

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

759759
auto &frames = it->second;
760-
for (auto f : frames)
760+
for (const auto& f : frames)
761761
{
762762
// !!do not!! call `_spriteFrames.erase(f);` to erase SpriteFrame
763763
// only erase index here

cocos/2d/CCTMXXMLParser.cpp

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

754754
uint32_t* bufferPtr = reinterpret_cast<uint32_t*>(buffer);
755-
for(auto gidToken : gidTokens) {
755+
for(const auto& gidToken : gidTokens) {
756756
auto tileGid = (uint32_t)strtoul(gidToken.c_str(), nullptr, 10);
757757
*bufferPtr = tileGid;
758758
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
@@ -2210,7 +2210,7 @@ std::vector<Vec3> Bundle3D::getTrianglesList(const std::string& path)
22102210
Bundle3D::destroyBundle(bundle);
22112211
for (auto iter : meshs.meshDatas){
22122212
int preVertexSize = iter->getPerVertexSize() / sizeof(float);
2213-
for (auto indexArray : iter->subMeshIndices){
2213+
for (const auto& indexArray : iter->subMeshIndices){
22142214
for (auto i : indexArray){
22152215
trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1], iter->vertex[i * preVertexSize + 2]));
22162216
}

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
@@ -1335,7 +1335,7 @@ bool Terrain::Chunk::getIntersectPointWithRay(const Ray& ray, Vec3& intersectPoi
13351335

13361336
float minDist = FLT_MAX;
13371337
bool isFind = false;
1338-
for (auto triangle : _trianglesList)
1338+
for (const auto& triangle : _trianglesList)
13391339
{
13401340
Vec3 p;
13411341
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)