# [PATCH] pack-object: slightly more efficient

1 messages from 2006-05-15 to 2006-05-15. Participants: Nicolas Pitre.
Thread: https://gitlist.dev/t/4150

## Nicolas Pitre, 2006-05-15 17:47

Subject: [PATCH] pack-object: slightly more efficient
Message-ID: <Pine.LNX.4.64.0605151319580.18071@localhost.localdomain>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0605151319580.18071%40localhost.localdomain

```
Avoid creating a delta index for objects with maximum depth since they 
are not going to be used as delta base anyway.  This also reduce peak 
memory usage slightly as the current object's delta index is not useful 
until the next object in the loop is considered for deltification. This 
saves a bit more than 1% on CPU usage.

Signed-off-by: Nicolas Pitre <nico@cam.org>

---

diff --git a/delta.h b/delta.h
index 727ae30..7b3f86d 100644
--- a/delta.h
+++ b/delta.h
@@ -18,6 +18,8 @@ create_delta_index(const void *buf, unsi
 
 /*
  * free_delta_index: free the index created by create_delta_index()
+ *
+ * Given pointer must be what create_delta_index() returned, or NULL.
  */
 extern void free_delta_index(struct delta_index *index);
 
diff --git a/pack-objects.c b/pack-objects.c
index b0388d7..9daf1c1 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -1104,17 +1104,14 @@ static void find_deltas(struct object_en
 
 		if (entry->size < 50)
 			continue;
-		if (n->index)
-			free_delta_index(n->index);
+		free_delta_index(n->index);
+		n->index = NULL;
 		free(n->data);
 		n->entry = entry;
 		n->data = read_sha1_file(entry->sha1, type, &size);
 		if (size != entry->size)
 			die("object %s inconsistent object length (%lu vs %lu)",
 			    sha1_to_hex(entry->sha1), size, entry->size);
-		n->index = create_delta_index(n->data, size);
-		if (!n->index)
-			die("out of memory");
 
 		j = window;
 		while (--j > 0) {
@@ -1134,6 +1131,11 @@ static void find_deltas(struct object_en
 		 */
 		if (entry->delta && depth <= entry->depth)
 			continue;
+
+		n->index = create_delta_index(n->data, size);
+		if (!n->index)
+			die("out of memory");
+
 		idx++;
 		if (idx >= window)
 			idx = 0;
@@ -1143,8 +1145,7 @@ static void find_deltas(struct object_en
 		fputc('\n', stderr);
 
 	for (i = 0; i < window; ++i) {
-		if (array[i].index)
-			free_delta_index(array[i].index);
+		free_delta_index(array[i].index);
 		free(array[i].data);
 	}
 	free(array);

```
