git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] sequencer: simplify allocation of result array in todo_list_rearrange_squash()

From
Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Date
Aug 9, 2023, 17:15 UTC
Message-ID
<20230809171532.2564880-1-oswald.buddenhagen@gmx.de>

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>
---
v2:
- removed redundant assignment of `items` in ALLOC_ARRAY() call
- added 2nd paragraph to commit message
- broken out of the bigger series, as the aggregation just unnecessarily
  holds it up
Cc: Phillip Wood <phillip.wood123@gmail.com>
---
 sequencer.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index cc9821ece2..947cb89e60 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6233,7 +6233,7 @@ static int skip_fixupish(const char *subject, const char **p) {
 int todo_list_rearrange_squash(struct todo_list *todo_list)
 {
 	struct hashmap subject2item;
-	int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0;
+	int rearranged = 0, *next, *tail, i, nr = 0;
 	char **subjects;
 	struct commit_todo_item commit_todo;
 	struct todo_item *items = NULL;
@@ -6345,6 +6345,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 	}
 
 	if (rearranged) {
+		ALLOC_ARRAY(items, todo_list->nr);
+
 		for (i = 0; i < todo_list->nr; i++) {
 			enum todo_command command = todo_list->items[i].command;
 			int cur = i;
@@ -6357,16 +6359,15 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 				continue;
 
 			while (cur >= 0) {
-				ALLOC_GROW(items, nr + 1, alloc);
 				items[nr++] = todo_list->items[cur];
 				cur = next[cur];
 			}
 		}
 
+		assert(nr == todo_list->nr);
+		todo_list->alloc = nr;
 		FREE_AND_NULL(todo_list->items);
 		todo_list->items = items;
-		todo_list->nr = nr;
-		todo_list->alloc = alloc;
 	}
 
 	free(next);
-- 
2.40.0.152.g15d061e6df
Next: Junio C Hamano
Message 1 of 2 in “sequencer: simplify allocation of result array in todo_list_rearrange_squash()”
  1. sequencer: simplify allocation of result array in todo_list_rearrange_squash()Oswald Buddenhagen, Aug 9, 2023
  2. Junio C HamanoAug 9, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.