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

[PATCH] speedup allocation in pack-redundant.c

From
Alex Riesen <raa.lkml@gmail.com>
Date
Nov 22, 2005, 14:56 UTC
Message-ID
<81b0412b0511220656l528436b1xea80ee18965e4dda@mail.gmail.com>
Reuse discarded nodes of llists
Subject: [PATCH] speedup allocation in pack-redundant.c
Signed-off-by: Alex Riesen <ariesen@harmanbecker.com>
---
 pack-redundant.c |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)

applies-to: 0a8441bc998f995dd35380472314802f53c6e1f3 738ce6cef594ae09b89372859d28f37b1bef9aa1

diff --git a/pack-redundant.c b/pack-redundant.c
index 1519385..3681170 100644
--- a/pack-redundant.c
+++ b/pack-redundant.c
@@ -36,11 +36,31 @@ struct pll {
 	size_t pl_size;
 };
 
-static inline void llist_free(struct llist *list)
+static struct llist_item *free_nodes = NULL;
+
+static inline struct llist_item *llist_item_get()
+{
+	struct llist_item *new;
+	if ( free_nodes ) {
+		new = free_nodes;
+		free_nodes = free_nodes->next;
+	} else
+		new = xmalloc(sizeof(struct llist_item));
+
+	return new;
+}
+
+static inline void llist_item_put(struct llist_item *item)
+{
+	item->next = free_nodes;
+	free_nodes = item;
+}
+
+static void llist_free(struct llist *list)
 {
 	while((list->back = list->front)) {
 		list->front = list->front->next;
-		free(list->back);
+		llist_item_put(list->back);
 	}
 	free(list);
 }
@@ -62,13 +82,13 @@ static struct llist * llist_copy(struct 
 	if ((ret->size = list->size) == 0)
 		return ret;
 
-	new = ret->front = xmalloc(sizeof(struct llist_item));
+	new = ret->front = llist_item_get();
 	new->sha1 = list->front->sha1;
 
 	old = list->front->next;
 	while (old) {
 		prev = new;
-		new = xmalloc(sizeof(struct llist_item));
+		new = llist_item_get();
 		prev->next = new;
 		new->sha1 = old->sha1;
 		old = old->next;
@@ -82,7 +102,7 @@ static struct llist * llist_copy(struct 
 static inline struct llist_item * llist_insert(struct llist *list,
 					struct llist_item *after, char *sha1)
 {
-	struct llist_item *new = xmalloc(sizeof(struct llist_item));
+	struct llist_item *new = llist_item_get();
 	new->sha1 = sha1;
 	new->next = NULL;
 
@@ -153,7 +173,7 @@ redo_from_start:
 				prev->next = l->next;
 			if (l == list->back)
 				list->back = prev;
-			free(l);
+			llist_item_put(l);
 			list->size--;
 			return prev;
 		}
---
0.99.9.GIT
Next: Junio C Hamano
Message 1 of 10 in “speedup allocation in pack-redundant.c”
  1. speedup allocation in pack-redundant.cAlex Riesen, Nov 22, 2005
  2. Junio C HamanoNov 22, 2005
  3. Lukas SandströmNov 22, 2005
  4. Junio C HamanoNov 22, 2005
  5. Alex RiesenNov 22, 2005
  6. Alex RiesenNov 22, 2005
  7. Lukas SandströmNov 22, 2005
  8. Alex RiesenNov 22, 2005
  9. Lukas SandströmNov 22, 2005
  10. Alex RiesenNov 23, 2005

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.