performance-inefficient-vector-operation.cpp
8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// RUN: %check_clang_tidy %s performance-inefficient-vector-operation %t -- \
// RUN: -format-style=llvm \
// RUN: -config='{CheckOptions: \
// RUN: [{key: performance-inefficient-vector-operation.EnableProto, value: 1}]}'
namespace std {
typedef int size_t;
template<class E> class initializer_list {
public:
using value_type = E;
using reference = E&;
using const_reference = const E&;
using size_type = size_t;
using iterator = const E*;
using const_iterator = const E*;
initializer_list();
size_t size() const; // number of elements
const E* begin() const; // first element
const E* end() const; // one past the last element
};
// initializer list range access
template<class E> const E* begin(initializer_list<E> il);
template<class E> const E* end(initializer_list<E> il);
template <class T>
class vector {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
explicit vector();
explicit vector(size_type n);
void push_back(const T& val);
template <class... Args> void emplace_back(Args &&... args);
void reserve(size_t n);
void resize(size_t n);
size_t size();
const_reference operator[] (size_type) const;
reference operator[] (size_type);
const_iterator begin() const;
const_iterator end() const;
};
} // namespace std
class Foo {
public:
explicit Foo(int);
};
class Bar {
public:
Bar(int);
};
int Op(int);
namespace proto2 {
class MessageLite {};
class Message : public MessageLite {};
} // namespace proto2
class FooProto : public proto2::Message {
public:
int *add_x(); // repeated int x;
void add_x(int x);
void mutable_x();
void mutable_y();
int add_z() const; // optional int add_z;
};
class BarProto : public proto2::Message {
public:
int *add_x();
void add_x(int x);
void mutable_x();
void mutable_y();
};
void f(std::vector<int>& t) {
{
std::vector<int> v0;
// CHECK-FIXES: v0.reserve(10);
for (int i = 0; i < 10; ++i)
v0.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
}
{
std::vector<int> v1;
// CHECK-FIXES: v1.reserve(10);
for (int i = 0; i < 10; i++)
v1.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v2;
// CHECK-FIXES: v2.reserve(10);
for (int i = 0; i < 10; ++i)
v2.push_back(0);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v3;
// CHECK-FIXES: v3.reserve(5);
for (int i = 0; i < 5; ++i) {
v3.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
// CHECK-FIXES-NOT: v3.reserve(10);
for (int i = 0; i < 10; ++i) {
// No fix for this loop as we encounter the prior loops.
v3.push_back(i);
}
}
{
std::vector<int> v4;
std::vector<int> v5;
v5.reserve(3);
// CHECK-FIXES: v4.reserve(10);
for (int i = 0; i < 10; ++i)
v4.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v6;
// CHECK-FIXES: v6.reserve(t.size());
for (std::size_t i = 0; i < t.size(); ++i) {
v6.push_back(t[i]);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<int> v7;
// CHECK-FIXES: v7.reserve(t.size() - 1);
for (std::size_t i = 0; i < t.size() - 1; ++i) {
v7.push_back(t[i]);
} // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v8;
// CHECK-FIXES: v8.reserve(t.size());
for (const auto &e : t) {
v8.push_back(e);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<int> v9;
// CHECK-FIXES: v9.reserve(t.size());
for (const auto &e : t) {
v9.push_back(Op(e));
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<Foo> v10;
// CHECK-FIXES: v10.reserve(t.size());
for (const auto &e : t) {
v10.push_back(Foo(e));
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<Bar> v11;
// CHECK-FIXES: v11.reserve(t.size());
for (const auto &e : t) {
v11.push_back(e);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<Foo> v12;
// CHECK-FIXES: v12.reserve(t.size());
for (const auto &e : t) {
v12.emplace_back(e);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'emplace_back' is called
}
}
{
FooProto foo;
// CHECK-FIXES: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'add_x' is called inside a loop; consider pre-allocating the container capacity before the loop
}
}
// ---- Non-fixed Cases ----
{
std::vector<int> z0;
z0.reserve(20);
// CHECK-FIXES-NOT: z0.reserve(10);
// There is a "reserve" call already.
for (int i = 0; i < 10; ++i) {
z0.push_back(i);
}
}
{
std::vector<int> z1;
z1.reserve(5);
// CHECK-FIXES-NOT: z1.reserve(10);
// There is a "reserve" call already.
for (int i = 0; i < 10; ++i) {
z1.push_back(i);
}
}
{
std::vector<int> z2;
z2.resize(5);
// CHECK-FIXES-NOT: z2.reserve(10);
// There is a ref usage of v before the loop.
for (int i = 0; i < 10; ++i) {
z2.push_back(i);
}
}
{
std::vector<int> z3;
z3.push_back(0);
// CHECK-FIXES-NOT: z3.reserve(10);
// There is a ref usage of v before the loop.
for (int i = 0; i < 10; ++i) {
z3.push_back(i);
}
}
{
std::vector<int> z4;
f(z4);
// CHECK-FIXES-NOT: z4.reserve(10);
// There is a ref usage of z4 before the loop.
for (int i = 0; i < 10; ++i) {
z4.push_back(i);
}
}
{
std::vector<int> z5(20);
// CHECK-FIXES-NOT: z5.reserve(10);
// z5 is not constructed with default constructor.
for (int i = 0; i < 10; ++i) {
z5.push_back(i);
}
}
{
std::vector<int> z6;
// CHECK-FIXES-NOT: z6.reserve(10);
// For-loop is not started with 0.
for (int i = 1; i < 10; ++i) {
z6.push_back(i);
}
}
{
std::vector<int> z7;
// CHECK-FIXES-NOT: z7.reserve(t.size());
// z7 isn't referenced in for-loop body.
for (std::size_t i = 0; i < t.size(); ++i) {
t.push_back(i);
}
}
{
std::vector<int> z8;
int k;
// CHECK-FIXES-NOT: z8.reserve(10);
// For-loop isn't a fixable loop.
for (std::size_t i = 0; k < 10; ++i) {
z8.push_back(t[i]);
}
}
{
std::vector<int> z9;
// CHECK-FIXES-NOT: z9.reserve(i + 1);
// The loop end expression refers to the loop variable i.
for (int i = 0; i < i + 1; i++)
z9.push_back(i);
}
{
std::vector<int> z10;
int k;
// CHECK-FIXES-NOT: z10.reserve(10);
// For-loop isn't a fixable loop.
for (std::size_t i = 0; i < 10; ++k) {
z10.push_back(t[i]);
}
}
{
std::vector<int> z11;
// initializer_list should not trigger the check.
for (int e : {1, 2, 3, 4, 5}) {
z11.push_back(e);
}
}
{
std::vector<int> z12;
std::vector<int>* z13 = &t;
// We only support detecting the range init expression which references
// container directly.
// Complex range init expressions like `*z13` is not supported.
for (const auto &e : *z13) {
z12.push_back(e);
}
}
{
FooProto foo;
foo.mutable_x();
// CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
}
}
{
FooProto foo;
// CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
foo.add_x(i);
}
}
{
FooProto foo;
// CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
foo.add_x(-1);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
}
}
{
FooProto foo;
BarProto bar;
bar.mutable_x();
// CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x();
bar.add_x();
}
}
{
FooProto foo;
foo.mutable_y();
// CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
}
}
{
FooProto foo;
// CHECK-FIXES-NOT: foo.mutable_z()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_z();
}
}
}