Merge lp:~hjd/widelands/optimizations into lp:widelands

Proposed by Hans Joachim Desserud
Status: Merged
Merged at revision: 6370
Proposed branch: lp:~hjd/widelands/optimizations
Merge into: lp:widelands
Diff against target: 473 lines (+45/-45)
22 files modified
src/economy/flag.cc (+2/-2)
src/economy/test/test_routing.cc (+2/-2)
src/game_io/game_cmd_queue_data_packet.cc (+1/-1)
src/graphic/richtext.cc (+1/-1)
src/i18n.cc (+2/-2)
src/logic/bob.cc (+3/-3)
src/logic/building.cc (+2/-2)
src/logic/cmd_queue.cc (+1/-1)
src/logic/game.cc (+1/-1)
src/logic/immovable.cc (+1/-1)
src/logic/militarysite.cc (+1/-1)
src/logic/player.cc (+1/-1)
src/logic/production_program.cc (+1/-1)
src/logic/productionsite.cc (+3/-3)
src/logic/soldier.cc (+11/-11)
src/logic/tribe.cc (+1/-1)
src/logic/warehouse.cc (+3/-3)
src/logic/worker.cc (+2/-2)
src/network/nethost.cc (+2/-2)
src/ui_basic/box.cc (+2/-2)
src/writeHTML.cc (+1/-1)
src/wui/soldierlist.cc (+1/-1)
To merge this branch: bzr merge lp:~hjd/widelands/optimizations
Reviewer Review Type Date Requested Status
Widelands Developers Pending
Review via email: mp+101116@code.launchpad.net

Description of the change

I ran utils/create_cppcheck_report and saw it complained a bit about "Possible inefficient checking for 'variable' emptiness." in a few places. This identifies a few places which check for variable.size(), where it looks like !variable.empty() would be equivelant. (Or better, as size() in worst case will count all elements to give an accurate number, while empty only checks whether it has at least one item or not, which would mean O(n) vs O(1) ) I'm not sure how often these methods are called though, so I don't know whether this would be a noticable performance gain.

Now, c++ is not the language which I'm most proficient in. So to check whether this seems like a good idea, I have pushed this inital patch which fix one file. If people approve of this intial work, I can submit a larger patch which fix more issues in the future.

Also, I'm a bit confused, though this might be due to how cppcheck works. Because I have fixed the two places it complained about in economy/flag.cc, however, size() is used in a similar way at line 72. I guess this isn't listed since it is only a if check, not a while loop which would run the same comparision multiple times. Still, I wonder if checks like this should be changed as well?

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

I totally approve! In fact, we have a code-style checker rule for
exactly this, seems like it missed these places though.

Revision history for this message
Hans Joachim Desserud (hjd) wrote :

Ok, thanks. :) I've pushed some more changes now. Some comments:

If we have a check as part of the stylechecker for this, then I think someone should check whether that still works and is run.

How can logic/building.cc:686 ever be called when the if has the same check as the while loop above?

The cppcheck report also listed logic/tribe.cc:233, but that section looks a bit special and I'm not sure what's going on there, so I left it alone...

Revision history for this message
SirVer (sirver) wrote :

>If we have a check as part of the stylechecker for this, then I think someone should check whether that still works and is run.
I was mistaken here. We have a check which checks for the inverse (!blah.size()) which should then
be replaced through blah.empty().

>How can logic/building.cc:686 ever be called when the if has the same check as the while loop above?
There is a break statement in the while loop which will terminate the
loop. So this is valid.

>The cppcheck report also listed logic/tribe.cc:233, but that section looks a bit special and I'm not sure what's going on there, so I left it alone...
It is save to replace this too:
    if (!column.empty())

but it will not make a notable difference in performance. It will only
shut up cppcheck.

Revision history for this message
Hans Joachim Desserud (hjd) wrote :

>There is a break statement in the while loop which will terminate the
loop. So this is valid.
Oh, right. I have to admit I only skimmed this section.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/economy/flag.cc'
2--- src/economy/flag.cc 2012-03-23 16:08:40 +0000
3+++ src/economy/flag.cc 2012-04-07 09:48:20 +0000
4@@ -499,7 +499,7 @@
5 */
6 void Flag::wake_up_capacity_queue(Game & game)
7 {
8- while (m_capacity_wait.size()) {
9+ while (!m_capacity_wait.empty()) {
10 Worker * const w = m_capacity_wait[0].get(game);
11 m_capacity_wait.erase(m_capacity_wait.begin());
12 if (w and w->wakeup_flag_capacity(game, *this))
13@@ -714,7 +714,7 @@
14 {
15 //molog("Flag::cleanup\n");
16
17- while (m_flag_jobs.size()) {
18+ while (!m_flag_jobs.empty()) {
19 delete m_flag_jobs.begin()->request;
20 m_flag_jobs.erase(m_flag_jobs.begin());
21 }
22
23=== modified file 'src/economy/test/test_routing.cc'
24--- src/economy/test/test_routing.cc 2012-03-03 19:23:20 +0000
25+++ src/economy/test/test_routing.cc 2012-04-07 09:48:20 +0000
26@@ -184,7 +184,7 @@
27 nodes.push_back(d1);
28 }
29 ~TestingNode_DefaultNodes_Fixture() {
30- while (nodes.size()) {
31+ while (!nodes.empty()) {
32 TestingRoutingNode * n = nodes.back();
33 delete n;
34 nodes.pop_back();
35@@ -396,7 +396,7 @@
36 nodes.push_back(d0);
37 }
38 ~ComplexRouterFixture() {
39- while (nodes.size()) {
40+ while (!nodes.empty()) {
41 RoutingNode * n = nodes.back();
42 delete n;
43 nodes.pop_back();
44
45=== modified file 'src/game_io/game_cmd_queue_data_packet.cc'
46--- src/game_io/game_cmd_queue_data_packet.cc 2012-02-15 21:25:34 +0000
47+++ src/game_io/game_cmd_queue_data_packet.cc 2012-04-07 09:48:20 +0000
48@@ -114,7 +114,7 @@
49 // Make a copy, so we can pop stuff
50 std::priority_queue<Cmd_Queue::cmditem> p = cmdq.m_cmds[time % CMD_QUEUE_BUCKET_SIZE];
51
52- while (p.size()) {
53+ while (!p.empty()) {
54 Cmd_Queue::cmditem const & it = p.top();
55 if (it.cmd->duetime() == time) {
56 if (upcast(GameLogicCommand, cmd, it.cmd)) {
57
58=== modified file 'src/graphic/richtext.cc'
59--- src/graphic/richtext.cc 2012-02-15 21:25:34 +0000
60+++ src/graphic/richtext.cc 2012-04-07 09:48:20 +0000
61@@ -139,7 +139,7 @@
62 */
63 void RichTextImpl::clear()
64 {
65- while (elements.size()) {
66+ while (!elements.empty()) {
67 delete elements.back();
68 elements.pop_back();
69 }
70
71=== modified file 'src/i18n.cc'
72--- src/i18n.cc 2012-02-15 21:25:34 +0000
73+++ src/i18n.cc 2012-04-07 09:48:20 +0000
74@@ -100,7 +100,7 @@
75
76 //don't try to get the previous TD when the very first one ('widelands')
77 //just got dropped
78- if (textdomains.size()) {
79+ if (!textdomains.empty()) {
80 char const * const domain = textdomains.back().first.c_str();
81 char const * const localedir = textdomains.back().second.c_str();
82
83@@ -235,7 +235,7 @@
84
85 SETLOCALE(LC_ALL, ""); // call to libintl
86
87- if (textdomains.size()) {
88+ if (!textdomains.empty()) {
89 char const * const domain = textdomains.back().first.c_str();
90 char const * const localedir = textdomains.back().second.c_str();
91 bind_textdomain_codeset (domain, "UTF-8");
92
93=== modified file 'src/logic/bob.cc'
94--- src/logic/bob.cc 2012-03-02 13:21:49 +0000
95+++ src/logic/bob.cc 2012-04-07 09:48:20 +0000
96@@ -160,7 +160,7 @@
97 */
98 void Bob::cleanup(Editor_Game_Base & egbase)
99 {
100- while (m_stack.size()) // bobs in the editor do not have tasks
101+ while (!m_stack.empty()) // bobs in the editor do not have tasks
102 do_pop_task(ref_cast<Game, Editor_Game_Base>(egbase));
103
104 set_owner(0); // implicitly remove ourselves from owner's map
105@@ -394,7 +394,7 @@
106 */
107 void Bob::reset_tasks(Game & game)
108 {
109- while (m_stack.size())
110+ while (!m_stack.empty())
111 do_pop_task(game);
112
113 m_signal.clear();
114@@ -928,7 +928,7 @@
115 game.map().find_bobs
116 (Area<FCoords>(field, 0), &soldiers, FindBobEnemySoldier(get_owner()));
117
118- if (soldiers.size()) {
119+ if (!soldiers.empty()) {
120 container_iterate(std::vector<Bob *>, soldiers, i) {
121 Soldier & soldier = ref_cast<Soldier, Bob>(**i.current);
122 if (soldier.getBattle())
123
124=== modified file 'src/logic/building.cc'
125--- src/logic/building.cc 2012-02-20 15:54:26 +0000
126+++ src/logic/building.cc 2012-04-07 09:48:20 +0000
127@@ -666,7 +666,7 @@
128 bool wakeup = false;
129
130 // Wake up one worker
131- while (m_leave_queue.size()) {
132+ while (!m_leave_queue.empty()) {
133 upcast(Worker, worker, m_leave_queue[0].get(game));
134
135 m_leave_queue.erase(m_leave_queue.begin());
136@@ -682,7 +682,7 @@
137 }
138 }
139
140- if (m_leave_queue.size())
141+ if (!m_leave_queue.empty())
142 schedule_act(game, m_leave_time - time);
143
144 if (!wakeup)
145
146=== modified file 'src/logic/cmd_queue.cc'
147--- src/logic/cmd_queue.cc 2012-02-15 21:25:34 +0000
148+++ src/logic/cmd_queue.cc 2012-04-07 09:48:20 +0000
149@@ -113,7 +113,7 @@
150 while (game_time_var < final) {
151 std::priority_queue<cmditem> & current_cmds = m_cmds[game_time_var % CMD_QUEUE_BUCKET_SIZE];
152
153- while (current_cmds.size()) {
154+ while (!current_cmds.empty()) {
155 Command & c = *current_cmds.top().cmd;
156 if (game_time_var < c.duetime())
157 break;
158
159=== modified file 'src/logic/game.cc'
160--- src/logic/game.cc 2012-03-07 09:39:47 +0000
161+++ src/logic/game.cc 2012-04-07 09:48:20 +0000
162@@ -1079,7 +1079,7 @@
163
164 const Player_Number nr_players = map().get_nrplayers();
165 iterate_players_existing_novar(p, nr_players, *this)
166- if (m_general_stats.size()) {
167+ if (!m_general_stats.empty()) {
168 entries = m_general_stats[p - 1].land_size.size();
169 break;
170 }
171
172=== modified file 'src/logic/immovable.cc'
173--- src/logic/immovable.cc 2012-02-15 21:25:34 +0000
174+++ src/logic/immovable.cc 2012-04-07 09:48:20 +0000
175@@ -1401,7 +1401,7 @@
176 */
177 void PlayerImmovable::cleanup(Editor_Game_Base & egbase)
178 {
179- while (m_workers.size())
180+ while (!m_workers.empty())
181 m_workers[0]->set_location(0);
182
183 if (m_owner)
184
185=== modified file 'src/logic/militarysite.cc'
186--- src/logic/militarysite.cc 2012-02-23 16:17:50 +0000
187+++ src/logic/militarysite.cc 2012-04-07 09:48:20 +0000
188@@ -528,7 +528,7 @@
189 std::vector<Soldier *> present = presentSoldiers();
190 Soldier * defender = 0;
191
192- if (present.size()) {
193+ if (!present.empty()) {
194 // Find soldier with greatest hitpoints
195 uint32_t current_max = 0;
196 container_iterate_const(std::vector<Soldier *>, present, i)
197
198=== modified file 'src/logic/player.cc'
199--- src/logic/player.cc 2012-02-28 22:10:23 +0000
200+++ src/logic/player.cc 2012-04-07 09:48:20 +0000
201@@ -495,7 +495,7 @@
202 std::vector<OPtr<PlayerImmovable> > bulldozelist;
203 bulldozelist.push_back(&_imm);
204
205- while (bulldozelist.size()) {
206+ while (!bulldozelist.empty()) {
207 PlayerImmovable * imm = bulldozelist.back().get(egbase());
208 bulldozelist.pop_back();
209 if (!imm)
210
211=== modified file 'src/logic/production_program.cc'
212--- src/logic/production_program.cc 2012-02-28 16:51:08 +0000
213+++ src/logic/production_program.cc 2012-04-07 09:48:20 +0000
214@@ -412,7 +412,7 @@
215 m_result == Completed ? _("completed") : _("skipped");
216 statistics_string += ' ';
217 statistics_string += ps.top_state().program->descname();
218- if (m_conditions.size()) {
219+ if (!m_conditions.empty()) {
220 std::string result_string = statistics_string;
221 if (m_is_when) { // "when a and b and ..." (all conditions must be true)
222 char const * const operator_string = _(" and ");
223
224=== modified file 'src/logic/productionsite.cc'
225--- src/logic/productionsite.cc 2012-02-15 21:25:34 +0000
226+++ src/logic/productionsite.cc 2012-04-07 09:48:20 +0000
227@@ -719,7 +719,7 @@
228 return true;
229 }
230
231- if (m_produced_items.size()) {
232+ if (!m_produced_items.empty()) {
233 // There is still a produced item waiting for delivery. Carry it out
234 // before continuing with the program.
235 std::pair<Ware_Index, uint8_t> & ware_type_with_count =
236@@ -742,7 +742,7 @@
237 return true;
238 }
239
240- if (m_recruited_workers.size()) {
241+ if (!m_recruited_workers.empty()) {
242 // There is still a recruited worker waiting to be released. Send it
243 // out.
244 std::pair<Ware_Index, uint8_t> & worker_type_with_count =
245@@ -853,7 +853,7 @@
246 std::string const & program_name = top_state().program->name();
247
248 m_stack.pop_back();
249- if (m_stack.size())
250+ if (!m_stack.empty())
251 top_state().phase = result;
252
253 switch (result) {
254
255=== modified file 'src/logic/soldier.cc'
256--- src/logic/soldier.cc 2012-03-14 21:05:00 +0000
257+++ src/logic/soldier.cc 2012-04-07 09:48:20 +0000
258@@ -233,60 +233,60 @@
259 std::string run = animation_name;
260
261 if (strcmp(animation_name, "attack_success_w") == 0) {
262- assert(m_attack_success_w_name.size() > 0);
263+ assert(!m_attack_success_w_name.empty());
264 uint32_t i = game.logic_rand() % m_attack_success_w_name.size();
265 run = m_attack_success_w_name[i];
266 }
267
268 if (strcmp(animation_name, "attack_success_e") == 0) {
269- assert(m_attack_success_e_name.size() > 0);
270+ assert(!m_attack_success_e_name.empty());
271 uint32_t i = game.logic_rand() % m_attack_success_e_name.size();
272 run = m_attack_success_e_name[i];
273 }
274
275 if (strcmp(animation_name, "attack_failure_w") == 0) {
276- assert(m_attack_failure_w_name.size() > 0);
277+ assert(!m_attack_failure_w_name.empty());
278 uint32_t i = game.logic_rand() % m_attack_failure_w_name.size();
279 run = m_attack_failure_w_name[i];
280 }
281
282 if (strcmp(animation_name, "attack_failure_e") == 0) {
283- assert(m_attack_failure_e_name.size() > 0);
284+ assert(!m_attack_failure_e_name.empty());
285 uint32_t i = game.logic_rand() % m_attack_failure_e_name.size();
286 run = m_attack_failure_e_name[i];
287 }
288
289 if (strcmp(animation_name, "evade_success_w") == 0) {
290- assert(m_evade_success_w_name.size() > 0);
291+ assert(!m_evade_success_w_name.empty());
292 uint32_t i = game.logic_rand() % m_evade_success_w_name.size();
293 run = m_evade_success_w_name[i];
294 }
295
296 if (strcmp(animation_name, "evade_success_e") == 0) {
297- assert(m_evade_success_e_name.size() > 0);
298+ assert(!m_evade_success_e_name.empty());
299 uint32_t i = game.logic_rand() % m_evade_success_e_name.size();
300 run = m_evade_success_e_name[i];
301 }
302
303 if (strcmp(animation_name, "evade_failure_w") == 0) {
304- assert(m_evade_failure_w_name.size() > 0);
305+ assert(!m_evade_failure_w_name.empty());
306 uint32_t i = game.logic_rand() % m_evade_failure_w_name.size();
307 run = m_evade_failure_w_name[i];
308 }
309
310 if (strcmp(animation_name, "evade_failure_e") == 0) {
311- assert(m_evade_failure_e_name.size() > 0);
312+ assert(!m_evade_failure_e_name.empty());
313 uint32_t i = game.logic_rand() % m_evade_failure_e_name.size();
314 run = m_evade_failure_e_name[i];
315 }
316 if (strcmp(animation_name, "die_w") == 0) {
317- assert(m_die_w_name.size() > 0);
318+ assert(!m_die_w_name.empty());
319 uint32_t i = game.logic_rand() % m_die_w_name.size();
320 run = m_die_w_name[i];
321 }
322
323 if (strcmp(animation_name, "die_e") == 0) {
324- assert(m_die_e_name.size() > 0);
325+ assert(!m_die_e_name.empty());
326 uint32_t i = game.logic_rand() % m_die_e_name.size();
327 run = m_die_e_name[i];
328 }
329@@ -1185,7 +1185,7 @@
330
331 std::stable_sort(targets.begin(), targets.end(), SoldierDistance::Greater());
332
333- while (targets.size() > 0) {
334+ while (!targets.empty()) {
335 const SoldierDistance & target = targets.back();
336
337 if (position == location) {
338
339=== modified file 'src/logic/tribe.cc'
340--- src/logic/tribe.cc 2012-03-10 16:58:50 +0000
341+++ src/logic/tribe.cc 2012-04-07 09:48:20 +0000
342@@ -230,7 +230,7 @@
343 m_ ## w ## s_order_coords[id] = std::pair<uint32_t, uint32_t> \
344 (m_ ## w ## s_order.size(), column.size() - 1); \
345 } \
346- if (column.size()) m_##w##s_order.push_back(column); \
347+ if (!column.empty()) m_##w##s_order.push_back(column); \
348 } \
349 \
350 /* Check that every ##w## has been added */ \
351
352=== modified file 'src/logic/warehouse.cc'
353--- src/logic/warehouse.cc 2012-02-15 21:25:34 +0000
354+++ src/logic/warehouse.cc 2012-04-07 09:48:20 +0000
355@@ -581,7 +581,7 @@
356 m_portdock = 0;
357 }
358
359- while (m_planned_workers.size()) {
360+ while (!m_planned_workers.empty()) {
361 m_planned_workers.back().cleanup();
362 m_planned_workers.pop_back();
363 }
364@@ -589,7 +589,7 @@
365 // all cached workers are unbound and freed
366 container_iterate(IncorporatedWorkers, m_incorporated_workers, cpair) {
367 WorkerList & clist = cpair->second;
368- while (clist.size()) {
369+ while (!clist.empty()) {
370 Worker * w = clist.back();
371 clist.pop_back();
372
373@@ -1298,7 +1298,7 @@
374
375 void Warehouse::PlannedWorkers::cleanup()
376 {
377- while (requests.size()) {
378+ while (!requests.empty()) {
379 delete requests.back();
380 requests.pop_back();
381 }
382
383=== modified file 'src/logic/worker.cc'
384--- src/logic/worker.cc 2012-03-06 18:14:55 +0000
385+++ src/logic/worker.cc 2012-04-07 09:48:20 +0000
386@@ -418,7 +418,7 @@
387 }
388 }
389
390- if (list.size()) {
391+ if (!list.empty()) {
392 set_program_objvar
393 (game, state, list[game.logic_rand() % list.size()].object);
394 break;
395@@ -443,7 +443,7 @@
396 map.find_reachable_bobs
397 (area, &list, cstep, FindBobAttribute(action.iparam2));
398
399- if (list.size()) {
400+ if (!list.empty()) {
401 set_program_objvar
402 (game, state, list[game.logic_rand() % list.size()]);
403 break;
404
405=== modified file 'src/network/nethost.cc'
406--- src/network/nethost.cc 2012-03-06 17:15:21 +0000
407+++ src/network/nethost.cc 2012-04-07 09:48:20 +0000
408@@ -885,7 +885,7 @@
409 clearComputerPlayers();
410 d->game = 0;
411
412- while (d->clients.size() > 0) {
413+ while (!d->clients.empty()) {
414 disconnectClient(0, "SERVER_CRASHED");
415 reaper();
416 }
417@@ -1252,7 +1252,7 @@
418 // Read in maps
419 std::vector<std::string> directories;
420 directories.push_back("maps");
421- while (directories.size()) {
422+ while (!directories.empty()) {
423 filenameset_t files;
424 g_fs->FindFiles(directories.at(directories.size() - 1).c_str(), "*", &files, 0);
425 directories.resize(directories.size() - 1);
426
427=== modified file 'src/ui_basic/box.cc'
428--- src/ui_basic/box.cc 2012-02-15 21:25:34 +0000
429+++ src/ui_basic/box.cc 2012-04-07 09:48:20 +0000
430@@ -100,7 +100,7 @@
431 maxbreadth = breadth;
432 }
433
434- if (m_items.size())
435+ if (!m_items.empty())
436 totaldepth += (m_items.size() - 1) * m_inner_spacing;
437
438 if (m_orientation == Horizontal) {
439@@ -142,7 +142,7 @@
440 totaldepth += depth;
441 }
442
443- if (m_items.size())
444+ if (!m_items.empty())
445 totaldepth += (m_items.size() - 1) * m_inner_spacing;
446
447 bool needscrollbar = false;
448
449=== modified file 'src/writeHTML.cc'
450--- src/writeHTML.cc 2012-03-08 22:18:17 +0000
451+++ src/writeHTML.cc 2012-04-07 09:48:20 +0000
452@@ -902,7 +902,7 @@
453 (m_result == Failed ? "failed" :
454 m_result == Completed ? "completed" :
455 "skipped");
456- if (m_conditions.size()) {
457+ if (!m_conditions.empty()) {
458 std::string op;
459 fw.Text(" <span class=\"keyword\">");
460 if (m_is_when) {
461
462=== modified file 'src/wui/soldierlist.cc'
463--- src/wui/soldierlist.cc 2012-02-15 21:25:34 +0000
464+++ src/wui/soldierlist.cc 2012-04-07 09:48:20 +0000
465@@ -203,7 +203,7 @@
466 }
467
468 // Second pass: add new soldiers
469- while (soldierlist.size()) {
470+ while (!soldierlist.empty()) {
471 Icon icon;
472 icon.soldier = soldierlist.back();
473 soldierlist.pop_back();

Subscribers

People subscribed via source and target branches

to status/vote changes: