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

[PATCH v3 11/11] builtin/cat-file: use bitmaps to efficiently filter by object type

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 2, 2025, 11:13 UTC
Message-ID
<20250402-pks-cat-file-object-type-filter-v3-11-4da6bb21871c@pks.im>
In-Reply-To
<20250402-pks-cat-file-object-type-filter-v3-0-4da6bb21871c@pks.im>

While it is now possible to filter objects by type, this mechanism is for now mostly a convenience. Most importantly, we still have to iterate through the whole packfile to find all objects of a specific type. This can be prohibitively expensive depending on the size of the packfiles.

It isn't really possible to do better than this when only considering a packfile itself, as the order of objects is not fixed. But when we have a packfile with a corresponding bitmap, either because the packfile itself has one or because the multi-pack index has a bitmap for it, then we can use these bitmaps to improve the runtime.

While bitmaps are typically used to compute reachability of objects, they also contain one bitmap per object type that encodes which object has what type. So instead of reading through the whole packfile(s), we can use the bitmaps and iterate through the type-specific bitmap. Typically, only a subset of packfiles will have a bitmap. But this isn't really much of a problem: we can use bitmaps when available, and then use the non-bitmap walk for every packfile that isn't covered by one.

Overall, this leads to quite a significant speedup depending on how many objects of a certain type exist. The following benchmarks have been executed in the Chromium repository, which has a 50GB packfile with almost 25 million objects. As expected, there isn't really much of a change in performance without an object filter:

    Benchmark 1: cat-file with no-filter (revision = HEAD~)
      Time (mean ± σ):     89.675 s ±  4.527 s    [User: 40.807 s, System: 10.782 s]
      Range (min … max):   83.052 s … 96.084 s    10 runs
    Benchmark 2: cat-file with no-filter (revision = HEAD)
      Time (mean ± σ):     88.991 s ±  2.488 s    [User: 42.278 s, System: 10.305 s]
      Range (min … max):   82.843 s … 91.271 s    10 runs
    Summary
      cat-file with no-filter (revision = HEAD) ran
        1.01 ± 0.06 times faster than cat-file with no-filter (revision = HEAD~)

We still have to scan through all objects as we yield all of them, so using the bitmap in this case doesn't really buy us anything. What is noticeable in this benchmark is that we're I/O-bound, not CPU-bound, as can be seen from the user/system runtimes, which combined are way lower than the overall benchmarked runtime.

But when we do use a filter we can see a significant improvement:
    Benchmark 1: cat-file with filter=object:type=commit (revision = HEAD~)
      Time (mean ± σ):     86.444 s ±  4.081 s    [User: 36.830 s, System: 11.312 s]
      Range (min … max):   80.305 s … 93.104 s    10 runs
    Benchmark 2: cat-file with filter=object:type=commit (revision = HEAD)
      Time (mean ± σ):      2.089 s ±  0.015 s    [User: 1.872 s, System: 0.207 s]
      Range (min … max):    2.073 s …  2.119 s    10 runs
    Summary
      cat-file with filter=object:type=commit (revision = HEAD) ran
       41.38 ± 1.98 times faster than cat-file with filter=object:type=commit (revision = HEAD~)

This is because we don't have to scan through all packfiles anymore, but can instead directly look up relevant objects.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/cat-file.c | 42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index b0c758eca02..ead7554a57a 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -21,6 +21,7 @@
 #include "streaming.h"
 #include "oid-array.h"
 #include "packfile.h"
+#include "pack-bitmap.h"
 #include "object-file.h"
 #include "object-name.h"
 #include "object-store-ll.h"
@@ -825,7 +826,20 @@ static int batch_one_object_packed(const struct object_id *oid,
 				 payload->payload);
 }
 
-static void batch_each_object(for_each_object_fn callback,
+static int batch_one_object_bitmapped(const struct object_id *oid,
+				      enum object_type type UNUSED,
+				      int flags UNUSED,
+				      uint32_t hash UNUSED,
+				      struct packed_git *pack,
+				      off_t offset,
+				      void *_payload)
+{
+	struct for_each_object_payload *payload = _payload;
+	return payload->callback(oid, pack, offset, payload->payload);
+}
+
+static void batch_each_object(struct batch_options *opt,
+			      for_each_object_fn callback,
 			      unsigned flags,
 			      void *_payload)
 {
@@ -833,9 +847,27 @@ static void batch_each_object(for_each_object_fn callback,
 		.callback = callback,
 		.payload = _payload,
 	};
+	struct bitmap_index *bitmap = prepare_bitmap_git(the_repository);
+
 	for_each_loose_object(batch_one_object_loose, &payload, 0);
-	for_each_packed_object(the_repository, batch_one_object_packed,
-			       &payload, flags);
+
+	if (bitmap && !for_each_bitmapped_object(bitmap, &opt->objects_filter,
+						 batch_one_object_bitmapped, &payload)) {
+		struct packed_git *pack;
+
+		for (pack = get_all_packs(the_repository); pack; pack = pack->next) {
+			if (bitmap_index_contains_pack(bitmap, pack) ||
+			    open_pack_index(pack))
+				continue;
+			for_each_object_in_pack(pack, batch_one_object_packed,
+						&payload, flags);
+		}
+	} else {
+		for_each_packed_object(the_repository, batch_one_object_packed,
+				       &payload, flags);
+	}
+
+	free_bitmap_index(bitmap);
 }
 
 static int batch_objects(struct batch_options *opt)
@@ -892,14 +924,14 @@ static int batch_objects(struct batch_options *opt)
 
 			cb.seen = &seen;
 
-			batch_each_object(batch_unordered_object,
+			batch_each_object(opt, batch_unordered_object,
 					  FOR_EACH_OBJECT_PACK_ORDER, &cb);
 
 			oidset_clear(&seen);
 		} else {
 			struct oid_array sa = OID_ARRAY_INIT;
 
-			batch_each_object(collect_object, 0, &sa);
+			batch_each_object(opt, collect_object, 0, &sa);
 			oid_array_for_each_unique(&sa, batch_object_cb, &cb);
 
 			oid_array_clear(&sa);
-- 
2.49.0.604.gff1f9ca942.dirty
Previous: Patrick SteinhardtNext: Karthik Nayak
Message 42 of 44 in “builtin/cat-file: allow filtering objects in batch mode”
  1. 0/9 builtin/cat-file: allow filtering objects in batch modePatrick Steinhardt, Feb 21, 2025
  2. 1/9 builtin/cat-file: rename variable that tracks usagePatrick Steinhardt, Feb 21, 2025
  3. 3/9 builtin/cat-file: support "blob:none" objects filterPatrick Steinhardt, Feb 21, 2025
  4. Toon ClaesFeb 26, 2025
  5. Karthik NayakFeb 27, 2025
  6. 2/9 builtin/cat-file: wire up an option to filter objectsPatrick Steinhardt, Feb 21, 2025
  7. Toon ClaesFeb 26, 2025
  8. Patrick SteinhardtFeb 28, 2025
  9. Junio C HamanoFeb 28, 2025
  10. Patrick SteinhardtMar 3, 2025
  11. Karthik NayakFeb 27, 2025
  12. 4/9 builtin/cat-file: support "blob:limit=" objects filterPatrick Steinhardt, Feb 21, 2025
  13. 5/9 builtin/cat-file: support "object:type=" objects filterPatrick Steinhardt, Feb 21, 2025
  14. Toon ClaesFeb 26, 2025
  15. Patrick SteinhardtFeb 28, 2025
  16. 6/9 pack-bitmap: expose function to iterate over bitmapped objectsPatrick Steinhardt, Feb 21, 2025
  17. Junio C HamanoFeb 24, 2025
  18. Patrick SteinhardtFeb 25, 2025
  19. Junio C HamanoFeb 25, 2025
  20. Taylor BlauFeb 27, 2025
  21. Patrick SteinhardtFeb 28, 2025
  22. Taylor BlauFeb 27, 2025
  23. Junio C HamanoFeb 27, 2025
  24. Taylor BlauFeb 27, 2025
  25. 8/9 builtin/cat-file: deduplicate logic to iterate over all objectsPatrick Steinhardt, Feb 21, 2025
  26. 7/9 pack-bitmap: introduce function to check whether a pack is bitmappedPatrick Steinhardt, Feb 21, 2025
  27. Taylor BlauFeb 27, 2025
  28. 9/9 builtin/cat-file: use bitmaps to efficiently filter by object typePatrick Steinhardt, Feb 21, 2025
  29. Karthik NayakFeb 27, 2025
  30. Taylor BlauFeb 27, 2025
  31. 00/11 builtin/cat-file: allow filtering objects in batch modePatrick Steinhardt, Apr 2, 2025
  32. 01/11 builtin/cat-file: rename variable that tracks usagePatrick Steinhardt, Apr 2, 2025
  33. 02/11 builtin/cat-file: introduce function to report object statusPatrick Steinhardt, Apr 2, 2025
  34. 03/11 builtin/cat-file: wire up an option to filter objectsPatrick Steinhardt, Apr 2, 2025
  35. 04/11 builtin/cat-file: support "blob:none" objects filterPatrick Steinhardt, Apr 2, 2025
  36. 05/11 builtin/cat-file: support "blob:limit=" objects filterPatrick Steinhardt, Apr 2, 2025
  37. 06/11 builtin/cat-file: support "object:type=" objects filterPatrick Steinhardt, Apr 2, 2025
  38. 07/11 pack-bitmap: allow passing payloads to `show_reachable_fn()`Patrick Steinhardt, Apr 2, 2025
  39. 08/11 pack-bitmap: add function to iterate over filtered bitmapped objectsPatrick Steinhardt, Apr 2, 2025
  40. 09/11 pack-bitmap: introduce function to check whether a pack is bitmappedPatrick Steinhardt, Apr 2, 2025
  41. 10/11 builtin/cat-file: deduplicate logic to iterate over all objectsPatrick Steinhardt, Apr 2, 2025
  42. 11/11 builtin/cat-file: use bitmaps to efficiently filter by object typePatrick Steinhardt, Apr 2, 2025
  43. Karthik NayakApr 3, 2025
  44. Junio C HamanoApr 8, 2025

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.