Skip to content

Commit 8002e8e

Browse files
pks-tgitster
authored andcommitted
builtin/cat-file: use bitmaps to efficiently filter by object type
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5ec702 commit 8002e8e

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

builtin/cat-file.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "streaming.h"
2222
#include "oid-array.h"
2323
#include "packfile.h"
24+
#include "pack-bitmap.h"
2425
#include "object-file.h"
2526
#include "object-name.h"
2627
#include "object-store-ll.h"
@@ -825,17 +826,48 @@ static int batch_one_object_packed(const struct object_id *oid,
825826
payload->payload);
826827
}
827828

828-
static void batch_each_object(for_each_object_fn callback,
829+
static int batch_one_object_bitmapped(const struct object_id *oid,
830+
enum object_type type UNUSED,
831+
int flags UNUSED,
832+
uint32_t hash UNUSED,
833+
struct packed_git *pack,
834+
off_t offset,
835+
void *_payload)
836+
{
837+
struct for_each_object_payload *payload = _payload;
838+
return payload->callback(oid, pack, offset, payload->payload);
839+
}
840+
841+
static void batch_each_object(struct batch_options *opt,
842+
for_each_object_fn callback,
829843
unsigned flags,
830844
void *_payload)
831845
{
832846
struct for_each_object_payload payload = {
833847
.callback = callback,
834848
.payload = _payload,
835849
};
850+
struct bitmap_index *bitmap = prepare_bitmap_git(the_repository);
851+
836852
for_each_loose_object(batch_one_object_loose, &payload, 0);
837-
for_each_packed_object(the_repository, batch_one_object_packed,
838-
&payload, flags);
853+
854+
if (bitmap && !for_each_bitmapped_object(bitmap, &opt->objects_filter,
855+
batch_one_object_bitmapped, &payload)) {
856+
struct packed_git *pack;
857+
858+
for (pack = get_all_packs(the_repository); pack; pack = pack->next) {
859+
if (bitmap_index_contains_pack(bitmap, pack) ||
860+
open_pack_index(pack))
861+
continue;
862+
for_each_object_in_pack(pack, batch_one_object_packed,
863+
&payload, flags);
864+
}
865+
} else {
866+
for_each_packed_object(the_repository, batch_one_object_packed,
867+
&payload, flags);
868+
}
869+
870+
free_bitmap_index(bitmap);
839871
}
840872

841873
static int batch_objects(struct batch_options *opt)
@@ -892,14 +924,14 @@ static int batch_objects(struct batch_options *opt)
892924

893925
cb.seen = &seen;
894926

895-
batch_each_object(batch_unordered_object,
927+
batch_each_object(opt, batch_unordered_object,
896928
FOR_EACH_OBJECT_PACK_ORDER, &cb);
897929

898930
oidset_clear(&seen);
899931
} else {
900932
struct oid_array sa = OID_ARRAY_INIT;
901933

902-
batch_each_object(collect_object, 0, &sa);
934+
batch_each_object(opt, collect_object, 0, &sa);
903935
oid_array_for_each_unique(&sa, batch_object_cb, &cb);
904936

905937
oid_array_clear(&sa);

0 commit comments

Comments
 (0)