Skip to content

Commit 6b5c2b8

Browse files
derrickstoleedscho
authored andcommitted
pack-objects: add --path-walk option
In order to more easily compute delta bases among objects that appear at the exact same path, add a --path-walk option to 'git pack-objects'. This option will use the path-walk API instead of the object walk given by the revision machinery. Since objects will be provided in batches representing a common path, those objects can be tested for delta bases immediately instead of waiting for a sort of the full object list by name-hash. This has multiple benefits, including avoiding collisions by name-hash. The objects marked as UNINTERESTING are included in these batches, so we are guaranteeing some locality to find good delta bases. After the individual passes are done on a per-path basis, the default name-hash is used to find other opportunistic delta bases that did not match exactly by the full path name. RFC TODO: It is important to note that this option is inherently incompatible with using a bitmap index. This walk probably also does not work with other advanced features, such as delta islands. Getting ahead of myself, this option compares well with --full-name-hash when the packfile is large enough, but also performs at least as well as the default in all cases that I've seen. RFC TODO: this should probably be recording the batch locations to another list so they could be processed in a second phase using threads. RFC TODO: list some examples of how this outperforms previous pack-objects strategies. (This is coming in later commits that include performance test changes.) Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 812b057 commit 6b5c2b8

File tree

4 files changed

+168
-11
lines changed

4 files changed

+168
-11
lines changed

Documentation/git-pack-objects.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SYNOPSIS
1616
[--cruft] [--cruft-expiration=<time>]
1717
[--stdout [--filter=<filter-spec>] | <base-name>]
1818
[--shallow] [--keep-true-parents] [--[no-]sparse]
19-
[--full-name-hash] < <object-list>
19+
[--full-name-hash] [--path-walk] < <object-list>
2020

2121

2222
DESCRIPTION
@@ -346,6 +346,16 @@ raise an error.
346346
Restrict delta matches based on "islands". See DELTA ISLANDS
347347
below.
348348

349+
--path-walk::
350+
By default, `git pack-objects` walks objects in an order that
351+
presents trees and blobs in an order unrelated to the path they
352+
appear relative to a commit's root tree. The `--path-walk` option
353+
enables a different walking algorithm that organizes trees and
354+
blobs by path. This has the potential to improve delta compression
355+
especially in the presence of filenames that cause collisions in
356+
Git's default name-hash algorithm. Due to changing how the objects
357+
are walked, this option is not compatible with `--delta-islands`,
358+
`--shallow`, or `--filter`.
349359

350360
DELTA ISLANDS
351361
-------------

Documentation/technical/api-path-walk.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ Examples
6969
--------
7070

7171
See example usages in:
72-
`t/helper/test-path-walk.c`
72+
`t/helper/test-path-walk.c`,
73+
`builtin/pack-objects.c`

builtin/pack-objects.c

+138-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#include "promisor-remote.h"
4242
#include "pack-mtimes.h"
4343
#include "parse-options.h"
44+
#include "blob.h"
45+
#include "tree.h"
46+
#include "path-walk.h"
4447

4548
/*
4649
* Objects we are going to pack are collected in the `to_pack` structure.
@@ -217,6 +220,7 @@ static int delta_search_threads;
217220
static int pack_to_stdout;
218221
static int sparse;
219222
static int thin;
223+
static int path_walk;
220224
static int num_preferred_base;
221225
static struct progress *progress_state;
222226

@@ -4142,6 +4146,105 @@ static void mark_bitmap_preferred_tips(void)
41424146
}
41434147
}
41444148

4149+
static inline int is_oid_interesting(struct repository *repo,
4150+
struct object_id *oid)
4151+
{
4152+
struct object *o = lookup_object(repo, oid);
4153+
return o && !(o->flags & UNINTERESTING);
4154+
}
4155+
4156+
static int add_objects_by_path(const char *path,
4157+
struct oid_array *oids,
4158+
enum object_type type,
4159+
void *data)
4160+
{
4161+
struct object_entry **delta_list;
4162+
size_t oe_start = to_pack.nr_objects;
4163+
size_t oe_end;
4164+
unsigned int sub_list_size;
4165+
unsigned int *processed = data;
4166+
4167+
/*
4168+
* First, add all objects to the packing data, including the ones
4169+
* marked UNINTERESTING (translated to 'exclude') as they can be
4170+
* used as delta bases.
4171+
*/
4172+
for (size_t i = 0; i < oids->nr; i++) {
4173+
int exclude;
4174+
struct object_info oi = OBJECT_INFO_INIT;
4175+
struct object_id *oid = &oids->oid[i];
4176+
4177+
/* Skip objects that do not exist locally. */
4178+
if (exclude_promisor_objects &&
4179+
oid_object_info_extended(the_repository, oid, &oi,
4180+
OBJECT_INFO_FOR_PREFETCH) < 0)
4181+
continue;
4182+
4183+
exclude = !is_oid_interesting(the_repository, oid);
4184+
4185+
if (exclude && !thin)
4186+
continue;
4187+
4188+
add_object_entry(oid, type, path, exclude);
4189+
}
4190+
4191+
oe_end = to_pack.nr_objects;
4192+
4193+
/* We can skip delta calculations if it is a no-op. */
4194+
if (oe_end == oe_start || !window)
4195+
return 0;
4196+
4197+
sub_list_size = 0;
4198+
ALLOC_ARRAY(delta_list, oe_end - oe_start);
4199+
4200+
for (size_t i = 0; i < oe_end - oe_start; i++) {
4201+
struct object_entry *entry = to_pack.objects + oe_start + i;
4202+
4203+
if (!should_attempt_deltas(entry))
4204+
continue;
4205+
4206+
delta_list[sub_list_size++] = entry;
4207+
}
4208+
4209+
/*
4210+
* Find delta bases among this list of objects that all match the same
4211+
* path. This causes the delta compression to be interleaved in the
4212+
* object walk, which can lead to confusing progress indicators. This is
4213+
* also incompatible with threaded delta calculations. In the future,
4214+
* consider creating a list of regions in the full to_pack.objects array
4215+
* that could be picked up by the threaded delta computation.
4216+
*/
4217+
if (sub_list_size && window) {
4218+
QSORT(delta_list, sub_list_size, type_size_sort);
4219+
find_deltas(delta_list, &sub_list_size, window, depth, processed);
4220+
}
4221+
4222+
free(delta_list);
4223+
return 0;
4224+
}
4225+
4226+
static void get_object_list_path_walk(struct rev_info *revs)
4227+
{
4228+
struct path_walk_info info = PATH_WALK_INFO_INIT;
4229+
unsigned int processed = 0;
4230+
4231+
info.revs = revs;
4232+
info.path_fn = add_objects_by_path;
4233+
info.path_fn_data = &processed;
4234+
revs->tag_objects = 1;
4235+
4236+
/*
4237+
* Allow the --[no-]sparse option to be interesting here, if only
4238+
* for testing purposes. Paths with no interesting objects will not
4239+
* contribute to the resulting pack, but only create noisy preferred
4240+
* base objects.
4241+
*/
4242+
info.prune_all_uninteresting = sparse;
4243+
4244+
if (walk_objects_by_path(&info))
4245+
die(_("failed to pack objects via path-walk"));
4246+
}
4247+
41454248
static void get_object_list(struct rev_info *revs, int ac, const char **av)
41464249
{
41474250
struct setup_revision_opt s_r_opt = {
@@ -4188,7 +4291,7 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
41884291

41894292
warn_on_object_refname_ambiguity = save_warning;
41904293

4191-
if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4294+
if (use_bitmap_index && !path_walk && !get_object_list_from_bitmap(revs))
41924295
return;
41934296

41944297
if (use_delta_islands)
@@ -4197,15 +4300,19 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
41974300
if (write_bitmap_index)
41984301
mark_bitmap_preferred_tips();
41994302

4200-
if (prepare_revision_walk(revs))
4201-
die(_("revision walk setup failed"));
4202-
mark_edges_uninteresting(revs, show_edge, sparse);
4203-
42044303
if (!fn_show_object)
42054304
fn_show_object = show_object;
4206-
traverse_commit_list(revs,
4207-
show_commit, fn_show_object,
4208-
NULL);
4305+
4306+
if (path_walk) {
4307+
get_object_list_path_walk(revs);
4308+
} else {
4309+
if (prepare_revision_walk(revs))
4310+
die(_("revision walk setup failed"));
4311+
mark_edges_uninteresting(revs, show_edge, sparse);
4312+
traverse_commit_list(revs,
4313+
show_commit, fn_show_object,
4314+
NULL);
4315+
}
42094316

42104317
if (unpack_unreachable_expiration) {
42114318
revs->ignore_missing_links = 1;
@@ -4415,6 +4522,8 @@ int cmd_pack_objects(int argc,
44154522
N_("use the sparse reachability algorithm")),
44164523
OPT_BOOL(0, "thin", &thin,
44174524
N_("create thin packs")),
4525+
OPT_BOOL(0, "path-walk", &path_walk,
4526+
N_("use the path-walk API to walk objects when possible")),
44184527
OPT_BOOL(0, "shallow", &shallow,
44194528
N_("create packs suitable for shallow fetches")),
44204529
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
@@ -4500,7 +4609,27 @@ int cmd_pack_objects(int argc,
45004609
window = 0;
45014610

45024611
strvec_push(&rp, "pack-objects");
4503-
if (thin) {
4612+
4613+
if (path_walk && filter_options.choice) {
4614+
warning(_("cannot use --filter with --path-walk"));
4615+
path_walk = 0;
4616+
}
4617+
if (path_walk && use_delta_islands) {
4618+
warning(_("cannot use delta islands with --path-walk"));
4619+
path_walk = 0;
4620+
}
4621+
if (path_walk && shallow) {
4622+
warning(_("cannot use --shallow with --path-walk"));
4623+
path_walk = 0;
4624+
}
4625+
if (path_walk) {
4626+
strvec_push(&rp, "--boundary");
4627+
/*
4628+
* We must disable the bitmaps because we are removing
4629+
* the --objects / --objects-edge[-aggressive] options.
4630+
*/
4631+
use_bitmap_index = 0;
4632+
} else if (thin) {
45044633
use_internal_rev_list = 1;
45054634
strvec_push(&rp, shallow
45064635
? "--objects-edge-aggressive"

t/t5300-pack-object.sh

+17
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,21 @@ test_expect_success '--full-name-hash and --write-bitmap-index are incompatible'
704704
git pack-objects --stdout --all --full-name-hash --write-bitmap-index >out
705705
'
706706

707+
# Basic "repack everything" test
708+
test_expect_success '--path-walk pack everything' '
709+
git -C server rev-parse HEAD >in &&
710+
git -C server pack-objects --stdout --revs --path-walk <in >out.pack &&
711+
git -C server index-pack --stdin <out.pack
712+
'
713+
714+
# Basic "thin pack" test
715+
test_expect_success '--path-walk thin pack' '
716+
cat >in <<-EOF &&
717+
$(git -C server rev-parse HEAD)
718+
^$(git -C server rev-parse HEAD~2)
719+
EOF
720+
git -C server pack-objects --thin --stdout --revs --path-walk <in >out.pack &&
721+
git -C server index-pack --fix-thin --stdin <out.pack
722+
'
723+
707724
test_done

0 commit comments

Comments
 (0)