Skip to content

Commit 6e36f63

Browse files
JohnCoconutminggo
authored and
minggo
committed
enable more clang-tidy performance options (#19656)
* enable more clang-tidy performance options * fix warning: performance-type-promotion-in-math-fn prefer c++ math functions, as it handles float and double case correctly. For example, std::sin has overload for both float and double, but sin only has double argument. C++ math functions avoids unnecessary floating point type conversions. * add missing cmath headers * change log to std::log
1 parent ac4af4c commit 6e36f63

33 files changed

+121
-90
lines changed

.clang-tidy

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Checks: >
33
-*,
44
performance-faster-string-find,
55
performance-for-range-copy,
6+
performance-implicit-conversion-in-loop,
7+
performance-inefficient-algorithm,
8+
performance-inefficient-vector-operation,
9+
performance-move-const-arg,
10+
performance-type-promotion-in-math-fn,
611
712
WarningsAsErrors: '*'
813
HeaderFilterRegex: '/(?!external)/.*'

cocos/2d/CCFastTMXLayer.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ THE SOFTWARE.
3636
3737
*/
3838
#include "2d/CCFastTMXLayer.h"
39+
#include <cmath>
3940
#include "2d/CCFastTMXTiledMap.h"
4041
#include "2d/CCSprite.h"
4142
#include "2d/CCCamera.h"
@@ -209,10 +210,10 @@ void TMXLayer::updateTiles(const Rect& culledRect)
209210
visibleTiles.origin.y += 1;
210211

211212
// if x=0.7, width=9.5, we need to draw number 0~10 of tiles, and so is height.
212-
visibleTiles.size.width = ceil(visibleTiles.origin.x + visibleTiles.size.width) - floor(visibleTiles.origin.x);
213-
visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y);
214-
visibleTiles.origin.x = floor(visibleTiles.origin.x);
215-
visibleTiles.origin.y = floor(visibleTiles.origin.y);
213+
visibleTiles.size.width = std::ceil(visibleTiles.origin.x + visibleTiles.size.width) - std::floor(visibleTiles.origin.x);
214+
visibleTiles.size.height = std::ceil(visibleTiles.origin.y + visibleTiles.size.height) - std::floor(visibleTiles.origin.y);
215+
visibleTiles.origin.x = std::floor(visibleTiles.origin.x);
216+
visibleTiles.origin.y = std::floor(visibleTiles.origin.y);
216217

217218
// for the bigger tiles.
218219
int tilesOverX = 0;
@@ -221,8 +222,8 @@ void TMXLayer::updateTiles(const Rect& culledRect)
221222
float tileSizeMax = std::max(tileSize.width, tileSize.height);
222223
if (_layerOrientation == FAST_TMX_ORIENTATION_ORTHO)
223224
{
224-
tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1;
225-
tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1;
225+
tilesOverX = std::ceil(tileSizeMax / mapTileSize.width) - 1;
226+
tilesOverY = std::ceil(tileSizeMax / mapTileSize.height) - 1;
226227

227228
if (tilesOverX < 0) tilesOverX = 0;
228229
if (tilesOverY < 0) tilesOverY = 0;
@@ -234,8 +235,8 @@ void TMXLayer::updateTiles(const Rect& culledRect)
234235
if (overTileRect.size.height < 0) overTileRect.size.height = 0;
235236
overTileRect = RectApplyTransform(overTileRect, nodeToTileTransform);
236237

237-
tilesOverX = ceil(overTileRect.origin.x + overTileRect.size.width) - floor(overTileRect.origin.x);
238-
tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y);
238+
tilesOverX = std::ceil(overTileRect.origin.x + overTileRect.size.width) - std::floor(overTileRect.origin.x);
239+
tilesOverY = std::ceil(overTileRect.origin.y + overTileRect.size.height) - std::floor(overTileRect.origin.y);
239240
}
240241
else
241242
{
@@ -346,15 +347,15 @@ void TMXLayer::setupTiles()
346347
switch (_layerOrientation)
347348
{
348349
case FAST_TMX_ORIENTATION_ORTHO:
349-
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 1;
350-
_screenGridSize.height = ceil(screenSize.height / _mapTileSize.height) + 1;
350+
_screenGridSize.width = std::ceil(screenSize.width / _mapTileSize.width) + 1;
351+
_screenGridSize.height = std::ceil(screenSize.height / _mapTileSize.height) + 1;
351352

352353
// tiles could be bigger than the grid, add additional rows if needed
353354
_screenGridSize.height += _tileSet->_tileSize.height / _mapTileSize.height;
354355
break;
355356
case FAST_TMX_ORIENTATION_ISO:
356-
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2;
357-
_screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4;
357+
_screenGridSize.width = std::ceil(screenSize.width / _mapTileSize.width) + 2;
358+
_screenGridSize.height = std::ceil(screenSize.height / (_mapTileSize.height/2)) + 4;
358359
break;
359360
case FAST_TMX_ORIENTATION_HEX:
360361
default:

cocos/2d/CCLight.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
****************************************************************************/
2424

2525
#include "2d/CCLight.h"
26+
#include <cmath>
2627
#include "2d/CCScene.h"
2728

2829
NS_CC_BEGIN
@@ -60,7 +61,7 @@ void BaseLight::onExit()
6061

6162
void BaseLight::setRotationFromDirection( const Vec3 &direction )
6263
{
63-
float projLen = sqrt(direction.x * direction.x + direction.z * direction.z);
64+
float projLen = std::sqrt(direction.x * direction.x + direction.z * direction.z);
6465
float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
6566
float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
6667
setRotation3D(Vec3(rotX, rotY, 0.0f));
@@ -205,4 +206,4 @@ AmbientLight::~AmbientLight()
205206

206207
}
207208

208-
NS_CC_END
209+
NS_CC_END

cocos/2d/CCTweenFunction.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ THE SOFTWARE.
2424
****************************************************************************/
2525

2626
#include "2d/CCTweenFunction.h"
27+
#include <cmath>
2728

2829
#define _USE_MATH_DEFINES // needed for M_PI and M_PI2
2930
#include <math.h> // M_PI
@@ -316,20 +317,20 @@ float expoEaseInOut(float time)
316317
// Circ Ease
317318
float circEaseIn(float time)
318319
{
319-
return -1 * (sqrt(1 - time * time) - 1);
320+
return -1 * (std::sqrt(1 - time * time) - 1);
320321
}
321322
float circEaseOut(float time)
322323
{
323324
time = time - 1;
324-
return sqrt(1 - time * time);
325+
return std::sqrt(1 - time * time);
325326
}
326327
float circEaseInOut(float time)
327328
{
328329
time = time * 2;
329330
if (time < 1)
330-
return -0.5f * (sqrt(1 - time * time) - 1);
331+
return -0.5f * (std::sqrt(1 - time * time) - 1);
331332
time -= 2;
332-
return 0.5f * (sqrt(1 - time * time) + 1);
333+
return 0.5f * (std::sqrt(1 - time * time) + 1);
333334
}
334335

335336

cocos/3d/CCOBB.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
****************************************************************************/
2525

2626
#include "3d/CCOBB.h"
27+
#include <cmath>
2728

2829
NS_CC_BEGIN
2930

@@ -109,7 +110,7 @@ static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a)
109110
for(i = 0; i < 50; i++)
110111
{
111112
sm = 0.0;
112-
for(ip = 0; ip < n; ip++) for(iq = ip+1; iq < n; iq++) sm += fabs(a.m[ip + 4 * iq]);
113+
for(ip = 0; ip < n; ip++) for(iq = ip+1; iq < n; iq++) sm += std::fabs(a.m[ip + 4 * iq]);
113114
if( fabs(sm) < FLT_EPSILON )
114115
{
115116
v.transpose();
@@ -127,15 +128,15 @@ static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a)
127128
{
128129
for(iq = ip + 1; iq < n; iq++)
129130
{
130-
g = 100.0 * fabs(a.m[ip + iq * 4]);
131+
g = 100.0 * std::fabs(a.m[ip + iq * 4]);
131132
float dmip = _getElement(d, ip);
132133
float dmiq = _getElement(d, iq);
133134

134-
if( i>3 && fabs(dmip) + g == fabs(dmip) && fabs(dmiq) + g == fabs(dmiq) )
135+
if( i>3 && std::fabs(dmip) + g == std::fabs(dmip) && std::fabs(dmiq) + g == std::fabs(dmiq) )
135136
{
136137
a.m[ip + 4 * iq] = 0.0;
137138
}
138-
else if (fabs(a.m[ip + 4 * iq]) > tresh)
139+
else if (std::fabs(a.m[ip + 4 * iq]) > tresh)
139140
{
140141
h = dmiq - dmip;
141142
if (fabs(h) + g == fabs(h))

cocos/base/ccUTF8.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ long cc_utf8_strlen (const char * p, int /*max*/)
526526
unsigned int cc_utf8_find_last_not_char(const std::vector<unsigned short>& str, unsigned short c)
527527
{
528528
std::vector<char16_t> char16Vector;
529+
char16Vector.reserve(str.size());
529530
for (const auto& e : str)
530531
{
531532
char16Vector.push_back(e);

cocos/deprecated/CCString.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ __String* __String::createWithFormat(const char* format, ...)
260260
__String* __String::createWithContentsOfFile(const std::string &filename)
261261
{
262262
std::string str = FileUtils::getInstance()->getStringFromFile(filename);
263-
return __String::create(std::move(str));
263+
return __String::create(str);
264264
}
265265

266266
void __String::acceptVisitor(DataVisitor &visitor)

cocos/editor-support/cocostudio/CCTransformHelp.cpp

+17-16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ THE SOFTWARE.
2424
****************************************************************************/
2525

2626
#include "editor-support/cocostudio/CCTransformHelp.h"
27+
#include <cmath>
2728
#include "editor-support/cocostudio/CCUtilMath.h"
2829

2930
using namespace cocos2d;
@@ -99,8 +100,8 @@ void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix)
99100
{
100101
if (node.skewX == -node.skewY)
101102
{
102-
double sine = sin(node.skewX);
103-
double cosine = cos(node.skewX);
103+
double sine = std::sin(node.skewX);
104+
double cosine = std::cos(node.skewX);
104105

105106
matrix.a = node.scaleX * cosine;
106107
matrix.b = node.scaleX * -sine;
@@ -109,10 +110,10 @@ void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix)
109110
}
110111
else
111112
{
112-
matrix.a = node.scaleX * cos(node.skewY);
113-
matrix.b = node.scaleX * sin(node.skewY);
114-
matrix.c = node.scaleY * sin(node.skewX);
115-
matrix.d = node.scaleY * cos(node.skewX);
113+
matrix.a = node.scaleX * std::cos(node.skewY);
114+
matrix.b = node.scaleX * std::sin(node.skewY);
115+
matrix.c = node.scaleY * std::sin(node.skewX);
116+
matrix.d = node.scaleY * std::cos(node.skewX);
116117
}
117118

118119
matrix.tx = node.x;
@@ -125,8 +126,8 @@ void TransformHelp::nodeToMatrix(const BaseData &node, Mat4 &matrix)
125126

126127
if (node.skewX == -node.skewY)
127128
{
128-
double sine = sin(node.skewX);
129-
double cosine = cos(node.skewX);
129+
double sine = std::sin(node.skewX);
130+
double cosine = std::cos(node.skewX);
130131

131132
matrix.m[0] = node.scaleX * cosine;
132133
matrix.m[1] = node.scaleX * -sine;
@@ -135,10 +136,10 @@ void TransformHelp::nodeToMatrix(const BaseData &node, Mat4 &matrix)
135136
}
136137
else
137138
{
138-
matrix.m[0] = node.scaleX * cos(node.skewY);
139-
matrix.m[1] = node.scaleX * sin(node.skewY);
140-
matrix.m[4] = node.scaleY * sin(node.skewX);
141-
matrix.m[5] = node.scaleY * cos(node.skewX);
139+
matrix.m[0] = node.scaleX * std::cos(node.skewY);
140+
matrix.m[1] = node.scaleX * std::sin(node.skewY);
141+
matrix.m[4] = node.scaleY * std::sin(node.skewX);
142+
matrix.m[5] = node.scaleY * std::cos(node.skewX);
142143
}
143144

144145
matrix.m[12] = node.x;
@@ -166,8 +167,8 @@ void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
166167

167168
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
168169
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
169-
node.scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
170-
node.scaleY = sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
170+
node.scaleX = std::sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
171+
node.scaleY = std::sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
171172
node.x = matrix.tx;
172173
node.y = matrix.ty;
173174
}
@@ -192,8 +193,8 @@ void TransformHelp::matrixToNode(const Mat4 &matrix, BaseData &node)
192193

193194
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
194195
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
195-
node.scaleX = sqrt(matrix.m[0] * matrix.m[0] + matrix.m[1] * matrix.m[1]);
196-
node.scaleY = sqrt(matrix.m[4] * matrix.m[4] + matrix.m[5] * matrix.m[5]);
196+
node.scaleX = std::sqrt(matrix.m[0] * matrix.m[0] + matrix.m[1] * matrix.m[1]);
197+
node.scaleY = std::sqrt(matrix.m[4] * matrix.m[4] + matrix.m[5] * matrix.m[5]);
197198
node.x = matrix.m[12];
198199
node.y = matrix.m[13];
199200
}

cocos/editor-support/cocostudio/CCUtilMath.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ THE SOFTWARE.
2424
****************************************************************************/
2525

2626
#include "editor-support/cocostudio/CCUtilMath.h"
27+
#include <cmath>
2728

2829
using namespace cocos2d;
2930

@@ -72,8 +73,8 @@ Vec2 circleTo(float t, Vec2 &center, float radius, float fromRadian, float radia
7273
{
7374
Vec2 p;
7475

75-
p.x = center.x + radius * cos(fromRadian + radianDif * t);
76-
p.y = center.y + radius * sin(fromRadian + radianDif * t);
76+
p.x = center.x + radius * std::cos(fromRadian + radianDif * t);
77+
p.y = center.y + radius * std::sin(fromRadian + radianDif * t);
7778

7879
return p;
7980
}

cocos/editor-support/cocostudio/TriggerMng.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ THE SOFTWARE.
2424
****************************************************************************/
2525

2626
#include "editor-support/cocostudio/TriggerMng.h"
27+
#include <cmath>
2728
#include "json/prettywriter.h"
2829
#include "json/stringbuffer.h"
2930
#include "base/CCDirector.h"
@@ -275,7 +276,7 @@ void TriggerMng::buildJson(rapidjson::Document &document, cocostudio::CocoLoader
275276
{
276277
int nV = atoi(str3);
277278
float fV = utils::atof(str3);
278-
if (fabs(nV - fV) < 0.0000001)
279+
if (std::fabs(nV - fV) < 0.0000001)
279280
{
280281
dataitem.AddMember("value", nV, allocator);
281282
}
@@ -351,7 +352,7 @@ void TriggerMng::buildJson(rapidjson::Document &document, cocostudio::CocoLoader
351352
{
352353
int nV = atoi(str5);
353354
float fV = utils::atof(str5);
354-
if (fabs(nV - fV) < 0.0000001)
355+
if (std::fabs(nV - fV) < 0.0000001)
355356
{
356357
dataitem.AddMember("value", nV, allocator);
357358
}

cocos/editor-support/spine/Bone.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
166166
za *= s;
167167
zc *= s;
168168
s = SQRT(za * za + zc * zc);
169-
r = PI / 2 + atan2(zc, za);
169+
r = PI / 2 + atan2f(zc, za);
170170
zb = COS(r) * s;
171171
zd = SIN(r) * s;
172172
la = COS_DEG(shearX) * scaleX;

cocos/platform/CCFileUtils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ std::string FileUtils::fullPathForDirectory(const std::string &dir) const
898898
{
899899
for (const auto& resolutionIt : _searchResolutionsOrderArray)
900900
{
901-
fullpath = searchIt + longdir + resolutionIt;
901+
fullpath.append(searchIt).append(longdir).append(resolutionIt);
902902
auto exists = isDirectoryExistInternal(fullpath);
903903

904904
if (exists && !fullpath.empty())
@@ -1180,7 +1180,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11801180
for (const auto& resolutionIt : _searchResolutionsOrderArray)
11811181
{
11821182
// searchPath + file_path + resourceDirectory
1183-
fullpath = fullPathForDirectory(searchIt + dirPath + resolutionIt);
1183+
fullpath = fullPathForDirectory(std::string(searchIt).append(dirPath).append(resolutionIt));
11841184
if (isDirectoryExistInternal(fullpath))
11851185
{
11861186
_fullPathCacheDir.emplace(dirPath, fullpath);

cocos/renderer/CCGLProgramState.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ bool GLProgramState::init(GLProgram* glprogram)
500500

501501
for(auto &uniform : _glprogram->_userUniforms) {
502502
UniformValue value(&uniform.second, _glprogram);
503-
_uniforms[uniform.second.location] = std::move(value);
503+
_uniforms[uniform.second.location] = value;
504504
_uniformsByName[uniform.first] = uniform.second.location;
505505
}
506506

extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929

3030
#include "CCControlPotentiometer.h"
31+
#include <cmath>
3132

3233
NS_CC_EXT_BEGIN
3334

@@ -206,7 +207,7 @@ float ControlPotentiometer::distanceBetweenPointAndPoint(Vec2 point1, Vec2 point
206207
{
207208
float dx = point1.x - point2.x;
208209
float dy = point1.y - point2.y;
209-
return sqrt(dx*dx + dy*dy);
210+
return std::sqrt(dx*dx + dy*dy);
210211
}
211212

212213
float ControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
@@ -220,8 +221,8 @@ float ControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFro
220221
float c = endLineB.x - beginLineB.x;
221222
float d = endLineB.y - beginLineB.y;
222223

223-
float atanA = atan2(a, b);
224-
float atanB = atan2(c, d);
224+
float atanA = std::atan2(a, b);
225+
float atanB = std::atan2(c, d);
225226

226227
// convert radiants to degrees
227228
return (atanA - atanB) * 180 / M_PI;

extensions/GUI/CCControlExtension/CCControlSaturationBrightnessPicker.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333

3434
#include "CCControlSaturationBrightnessPicker.h"
35+
#include <cmath>
3536

3637
NS_CC_EXT_BEGIN
3738

@@ -154,8 +155,8 @@ void ControlSaturationBrightnessPicker::updateSliderPosition(Vec2 sliderPosition
154155
else if (sliderPosition.y > _startPos.y + boxPos + boxSize) sliderPosition.y = _startPos.y + boxPos + boxSize;
155156

156157
// Use the position / slider width to determine the percentage the dragger is at
157-
_saturation = 1.0f - fabs((_startPos.x + (float)boxPos - sliderPosition.x)/(float)boxSize);
158-
_brightness = fabs((_startPos.y + (float)boxPos - sliderPosition.y)/(float)boxSize);
158+
_saturation = 1.0f - std::fabs((_startPos.x + (float)boxPos - sliderPosition.x)/(float)boxSize);
159+
_brightness = std::fabs((_startPos.y + (float)boxPos - sliderPosition.y)/(float)boxSize);
159160
}
160161

161162
bool ControlSaturationBrightnessPicker::checkSliderPosition(Vec2 location)

0 commit comments

Comments
 (0)