Skip to content

Commit fd41482

Browse files
committed
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 <[email protected]>
1 parent 48761b8 commit fd41482

File tree

4 files changed

+168
-11
lines changed

4 files changed

+168
-11
lines changed

Documentation/git-pack-objects.txt

Lines changed: 11 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 138 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#include "promisor-remote.h"
4040
#include "pack-mtimes.h"
4141
#include "parse-options.h"
42+
#include "blob.h"
43+
#include "tree.h"
44+
#include "path-walk.h"
4245

4346
/*
4447
* Objects we are going to pack are collected in the `to_pack` structure.
@@ -215,6 +218,7 @@ static int delta_search_threads;
215218
static int pack_to_stdout;
216219
static int sparse;
217220
static int thin;
221+
static int path_walk;
218222
static int num_preferred_base;
219223
static struct progress *progress_state;
220224

@@ -4122,6 +4126,105 @@ static void mark_bitmap_preferred_tips(void)
41224126
}
41234127
}
41244128

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

41694272
warn_on_object_refname_ambiguity = save_warning;
41704273

4171-
if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4274+
if (use_bitmap_index && !path_walk && !get_object_list_from_bitmap(revs))
41724275
return;
41734276

41744277
if (use_delta_islands)
@@ -4177,15 +4280,19 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
41774280
if (write_bitmap_index)
41784281
mark_bitmap_preferred_tips();
41794282

4180-
if (prepare_revision_walk(revs))
4181-
die(_("revision walk setup failed"));
4182-
mark_edges_uninteresting(revs, show_edge, sparse);
4183-
41844283
if (!fn_show_object)
41854284
fn_show_object = show_object;
4186-
traverse_commit_list(revs,
4187-
show_commit, fn_show_object,
4188-
NULL);
4285+
4286+
if (path_walk) {
4287+
get_object_list_path_walk(revs);
4288+
} else {
4289+
if (prepare_revision_walk(revs))
4290+
die(_("revision walk setup failed"));
4291+
mark_edges_uninteresting(revs, show_edge, sparse);
4292+
traverse_commit_list(revs,
4293+
show_commit, fn_show_object,
4294+
NULL);
4295+
}
41894296

41904297
if (unpack_unreachable_expiration) {
41914298
revs->ignore_missing_links = 1;
@@ -4380,6 +4487,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
43804487
N_("use the sparse reachability algorithm")),
43814488
OPT_BOOL(0, "thin", &thin,
43824489
N_("create thin packs")),
4490+
OPT_BOOL(0, "path-walk", &path_walk,
4491+
N_("use the path-walk API to walk objects when possible")),
43834492
OPT_BOOL(0, "shallow", &shallow,
43844493
N_("create packs suitable for shallow fetches")),
43854494
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
@@ -4462,7 +4571,27 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
44624571
window = 0;
44634572

44644573
strvec_push(&rp, "pack-objects");
4465-
if (thin) {
4574+
4575+
if (path_walk && filter_options.choice) {
4576+
warning(_("cannot use --filter with --path-walk"));
4577+
path_walk = 0;
4578+
}
4579+
if (path_walk && use_delta_islands) {
4580+
warning(_("cannot use delta islands with --path-walk"));
4581+
path_walk = 0;
4582+
}
4583+
if (path_walk && shallow) {
4584+
warning(_("cannot use --shallow with --path-walk"));
4585+
path_walk = 0;
4586+
}
4587+
if (path_walk) {
4588+
strvec_push(&rp, "--boundary");
4589+
/*
4590+
* We must disable the bitmaps because we are removing
4591+
* the --objects / --objects-edge[-aggressive] options.
4592+
*/
4593+
use_bitmap_index = 0;
4594+
} else if (thin) {
44664595
use_internal_rev_list = 1;
44674596
strvec_push(&rp, shallow
44684597
? "--objects-edge-aggressive"

t/t5300-pack-object.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,21 @@ test_expect_success '--full-name-hash and --write-bitmap-index are incompatible'
689689
git pack-objects --stdout --all --full-name-hash --write-bitmap-index >out
690690
'
691691

692+
# Basic "repack everything" test
693+
test_expect_success '--path-walk pack everything' '
694+
git -C server rev-parse HEAD >in &&
695+
git -C server pack-objects --stdout --revs --path-walk <in >out.pack &&
696+
git -C server index-pack --stdin <out.pack
697+
'
698+
699+
# Basic "thin pack" test
700+
test_expect_success '--path-walk thin pack' '
701+
cat >in <<-EOF &&
702+
$(git -C server rev-parse HEAD)
703+
^$(git -C server rev-parse HEAD~2)
704+
EOF
705+
git -C server pack-objects --thin --stdout --revs --path-walk <in >out.pack &&
706+
git -C server index-pack --fix-thin --stdin <out.pack
707+
'
708+
692709
test_done

0 commit comments

Comments
 (0)