Subject: [PATCH 08/11] pack-objects: faster reverse packed_git lookup

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 1 Mar 2018 16:10:49 +0700

We do a linear search for in_pack index in create_object_entry(). This
function is called for every available object in the worst case (and
on linux-2.6.git, that's about 6.5M). Try to avoid that by saving the
index in packed_git. Since we should not have zillions of packs, this
extra space should not be a big deal.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/pack-objects.c | 11 ++---------
 cache.h                |  1 +
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index d0d371714a..1fdb85ebb5 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1074,15 +1074,7 @@ static void create_object_entry(const struct object_id *oid,
 	else
 		nr_result++;
 	if (found_pack) {
-		int i;
-
-		for (i = 0; i < (1 << OE_IN_PACK_BITS); i++)
-			if (to_pack.in_pack[i] == found_pack) {
-				entry->in_pack_idx = i;
-				break;
-			}
-		if (i == (1 << OE_IN_PACK_BITS))
-			die("BUG: pack not found!");
+		entry->in_pack_idx = found_pack->index;
 		entry->in_pack_offset = found_offset;
 	}
 
@@ -2980,6 +2972,7 @@ static void init_in_pack_mapping(struct packing_data *to_pack)
 		if (i >= (1 << OE_IN_PACK_BITS))
 			die("BUG: too many packs to handle!");
 		to_pack->in_pack[i] = p;
+		p->index = i;
 	}
 }
 
diff --git a/cache.h b/cache.h
index 862bdff83a..b90feb3802 100644
--- a/cache.h
+++ b/cache.h
@@ -1635,6 +1635,7 @@ extern struct packed_git {
 	int index_version;
 	time_t mtime;
 	int pack_fd;
+	int index;		/* for builtin/pack-objects.c */
 	unsigned pack_local:1,
 		 pack_keep:1,
 		 freshened:1,
-- 
2.16.1.435.g8f24da2e1a