sequencer: simplify allocation of result array in todo_list_rearrange_squash()

The operation doesn't change the number of elements in the array, so we do not need to allocate the result piecewise. This moves the re-assignment of todo_list->alloc at the end slighly up, so it's right after the newly added assert which also refers to `nr` (and which indeed should come first). Also, the value is more likely to be still in a register at that point. Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Oswald Buddenhagen committed Aug 9, 2023 at 19:15 UTC 82dc42cbd1c4576c2fd879996d9e73a86434769a
1 file changed +5 -4
sequencer.c
+5 -4
@@ -6233,7 +6233,7 @@ static int skip_fixupish(const char *subject, const char **p) {
6233 int todo_list_rearrange_squash(struct todo_list *todo_list)
6234 {
6235 struct hashmap subject2item;
6236 - int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0;
6236 + int rearranged = 0, *next, *tail, i, nr = 0;
6237 char **subjects;
6238 struct commit_todo_item commit_todo;
6239 struct todo_item *items = NULL;
@@ -6345,6 +6345,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6345 }
6346
6347 if (rearranged) {
6348 + ALLOC_ARRAY(items, todo_list->nr);
6349 +
6350 for (i = 0; i < todo_list->nr; i++) {
6351 enum todo_command command = todo_list->items[i].command;
6352 int cur = i;
@@ -6357,16 +6359,15 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6359 continue;
6360
6361 while (cur >= 0) {
6360 - ALLOC_GROW(items, nr + 1, alloc);
6362 items[nr++] = todo_list->items[cur];
6363 cur = next[cur];
6364 }
6365 }
6366
6367 + assert(nr == todo_list->nr);
6368 + todo_list->alloc = nr;
6369 FREE_AND_NULL(todo_list->items);
6370 todo_list->items = items;
6368 - todo_list->nr = nr;
6369 - todo_list->alloc = alloc;
6371 }
6372
6373 free(next);