Skip to content

Commit 2e4173b

Browse files
derrickstoleeGit for Windows Build Agent
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 <[email protected]>
1 parent 2cfdda7 commit 2e4173b

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

@@ -4151,6 +4155,105 @@ static void mark_bitmap_preferred_tips(void)
41514155
}
41524156
}
41534157

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

41984301
warn_on_object_refname_ambiguity = save_warning;
41994302

4200-
if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4303+
if (use_bitmap_index && !path_walk && !get_object_list_from_bitmap(revs))
42014304
return;
42024305

42034306
if (use_delta_islands)
@@ -4206,15 +4309,19 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
42064309
if (write_bitmap_index)
42074310
mark_bitmap_preferred_tips();
42084311

4209-
if (prepare_revision_walk(revs))
4210-
die(_("revision walk setup failed"));
4211-
mark_edges_uninteresting(revs, show_edge, sparse);
4212-
42134312
if (!fn_show_object)
42144313
fn_show_object = show_object;
4215-
traverse_commit_list(revs,
4216-
show_commit, fn_show_object,
4217-
NULL);
4314+
4315+
if (path_walk) {
4316+
get_object_list_path_walk(revs);
4317+
} else {
4318+
if (prepare_revision_walk(revs))
4319+
die(_("revision walk setup failed"));
4320+
mark_edges_uninteresting(revs, show_edge, sparse);
4321+
traverse_commit_list(revs,
4322+
show_commit, fn_show_object,
4323+
NULL);
4324+
}
42184325

42194326
if (unpack_unreachable_expiration) {
42204327
revs->ignore_missing_links = 1;
@@ -4412,6 +4519,8 @@ int cmd_pack_objects(int argc,
44124519
N_("use the sparse reachability algorithm")),
44134520
OPT_BOOL(0, "thin", &thin,
44144521
N_("create thin packs")),
4522+
OPT_BOOL(0, "path-walk", &path_walk,
4523+
N_("use the path-walk API to walk objects when possible")),
44154524
OPT_BOOL(0, "shallow", &shallow,
44164525
N_("create packs suitable for shallow fetches")),
44174526
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
@@ -4494,7 +4603,27 @@ int cmd_pack_objects(int argc,
44944603
window = 0;
44954604

44964605
strvec_push(&rp, "pack-objects");
4497-
if (thin) {
4606+
4607+
if (path_walk && filter_options.choice) {
4608+
warning(_("cannot use --filter with --path-walk"));
4609+
path_walk = 0;
4610+
}
4611+
if (path_walk && use_delta_islands) {
4612+
warning(_("cannot use delta islands with --path-walk"));
4613+
path_walk = 0;
4614+
}
4615+
if (path_walk && shallow) {
4616+
warning(_("cannot use --shallow with --path-walk"));
4617+
path_walk = 0;
4618+
}
4619+
if (path_walk) {
4620+
strvec_push(&rp, "--boundary");
4621+
/*
4622+
* We must disable the bitmaps because we are removing
4623+
* the --objects / --objects-edge[-aggressive] options.
4624+
*/
4625+
use_bitmap_index = 0;
4626+
} else if (thin) {
44984627
use_internal_rev_list = 1;
44994628
strvec_push(&rp, shallow
45004629
? "--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)