Skip to content

Commit 2e23899

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 b7bc6ec commit 2e23899

File tree

4 files changed

+167
-10
lines changed

4 files changed

+167
-10
lines changed

Documentation/git-pack-objects.adoc

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-
[--name-hash-version=<n>] < <object-list>
19+
[--name-hash-version=<n>] [--path-walk] < <object-list>
2020

2121

2222
DESCRIPTION
@@ -375,6 +375,16 @@ many different directories. At the moment, this version is not allowed
375375
when writing reachability bitmap files with `--write-bitmap-index` and it
376376
will be automatically changed to version `1`.
377377
378+
--path-walk::
379+
By default, `git pack-objects` walks objects in an order that
380+
presents trees and blobs in an order unrelated to the path they
381+
appear relative to a commit's root tree. The `--path-walk` option
382+
enables a different walking algorithm that organizes trees and
383+
blobs by path. This has the potential to improve delta compression
384+
especially in the presence of filenames that cause collisions in
385+
Git's default name-hash algorithm. Due to changing how the objects
386+
are walked, this option is not compatible with `--delta-islands`,
387+
`--shallow`, or `--filter`.
378388
379389
DELTA ISLANDS
380390
-------------

Documentation/technical/api-path-walk.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ Examples
7070
See example usages in:
7171
`t/helper/test-path-walk.c`,
7272
`builtin/backfill.c`
73+
`builtin/pack-objects.c`

builtin/pack-objects.c

Lines changed: 138 additions & 9 deletions
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

@@ -4189,6 +4193,105 @@ static void mark_bitmap_preferred_tips(void)
41894193
}
41904194
}
41914195

4196+
static inline int is_oid_interesting(struct repository *repo,
4197+
struct object_id *oid)
4198+
{
4199+
struct object *o = lookup_object(repo, oid);
4200+
return o && !(o->flags & UNINTERESTING);
4201+
}
4202+
4203+
static int add_objects_by_path(const char *path,
4204+
struct oid_array *oids,
4205+
enum object_type type,
4206+
void *data)
4207+
{
4208+
struct object_entry **delta_list;
4209+
size_t oe_start = to_pack.nr_objects;
4210+
size_t oe_end;
4211+
unsigned int sub_list_size;
4212+
unsigned int *processed = data;
4213+
4214+
/*
4215+
* First, add all objects to the packing data, including the ones
4216+
* marked UNINTERESTING (translated to 'exclude') as they can be
4217+
* used as delta bases.
4218+
*/
4219+
for (size_t i = 0; i < oids->nr; i++) {
4220+
int exclude;
4221+
struct object_info oi = OBJECT_INFO_INIT;
4222+
struct object_id *oid = &oids->oid[i];
4223+
4224+
/* Skip objects that do not exist locally. */
4225+
if (exclude_promisor_objects &&
4226+
oid_object_info_extended(the_repository, oid, &oi,
4227+
OBJECT_INFO_FOR_PREFETCH) < 0)
4228+
continue;
4229+
4230+
exclude = !is_oid_interesting(the_repository, oid);
4231+
4232+
if (exclude && !thin)
4233+
continue;
4234+
4235+
add_object_entry(oid, type, path, exclude);
4236+
}
4237+
4238+
oe_end = to_pack.nr_objects;
4239+
4240+
/* We can skip delta calculations if it is a no-op. */
4241+
if (oe_end == oe_start || !window)
4242+
return 0;
4243+
4244+
sub_list_size = 0;
4245+
ALLOC_ARRAY(delta_list, oe_end - oe_start);
4246+
4247+
for (size_t i = 0; i < oe_end - oe_start; i++) {
4248+
struct object_entry *entry = to_pack.objects + oe_start + i;
4249+
4250+
if (!should_attempt_deltas(entry))
4251+
continue;
4252+
4253+
delta_list[sub_list_size++] = entry;
4254+
}
4255+
4256+
/*
4257+
* Find delta bases among this list of objects that all match the same
4258+
* path. This causes the delta compression to be interleaved in the
4259+
* object walk, which can lead to confusing progress indicators. This is
4260+
* also incompatible with threaded delta calculations. In the future,
4261+
* consider creating a list of regions in the full to_pack.objects array
4262+
* that could be picked up by the threaded delta computation.
4263+
*/
4264+
if (sub_list_size && window) {
4265+
QSORT(delta_list, sub_list_size, type_size_sort);
4266+
find_deltas(delta_list, &sub_list_size, window, depth, processed);
4267+
}
4268+
4269+
free(delta_list);
4270+
return 0;
4271+
}
4272+
4273+
static void get_object_list_path_walk(struct rev_info *revs)
4274+
{
4275+
struct path_walk_info info = PATH_WALK_INFO_INIT;
4276+
unsigned int processed = 0;
4277+
4278+
info.revs = revs;
4279+
info.path_fn = add_objects_by_path;
4280+
info.path_fn_data = &processed;
4281+
revs->tag_objects = 1;
4282+
4283+
/*
4284+
* Allow the --[no-]sparse option to be interesting here, if only
4285+
* for testing purposes. Paths with no interesting objects will not
4286+
* contribute to the resulting pack, but only create noisy preferred
4287+
* base objects.
4288+
*/
4289+
info.prune_all_uninteresting = sparse;
4290+
4291+
if (walk_objects_by_path(&info))
4292+
die(_("failed to pack objects via path-walk"));
4293+
}
4294+
41924295
static void get_object_list(struct rev_info *revs, int ac, const char **av)
41934296
{
41944297
struct setup_revision_opt s_r_opt = {
@@ -4235,7 +4338,7 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
42354338

42364339
warn_on_object_refname_ambiguity = save_warning;
42374340

4238-
if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4341+
if (use_bitmap_index && !path_walk && !get_object_list_from_bitmap(revs))
42394342
return;
42404343

42414344
if (use_delta_islands)
@@ -4244,15 +4347,19 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
42444347
if (write_bitmap_index)
42454348
mark_bitmap_preferred_tips();
42464349

4247-
if (prepare_revision_walk(revs))
4248-
die(_("revision walk setup failed"));
4249-
mark_edges_uninteresting(revs, show_edge, sparse);
4250-
42514350
if (!fn_show_object)
42524351
fn_show_object = show_object;
4253-
traverse_commit_list(revs,
4254-
show_commit, fn_show_object,
4255-
NULL);
4352+
4353+
if (path_walk) {
4354+
get_object_list_path_walk(revs);
4355+
} else {
4356+
if (prepare_revision_walk(revs))
4357+
die(_("revision walk setup failed"));
4358+
mark_edges_uninteresting(revs, show_edge, sparse);
4359+
traverse_commit_list(revs,
4360+
show_commit, fn_show_object,
4361+
NULL);
4362+
}
42564363

42574364
if (unpack_unreachable_expiration) {
42584365
revs->ignore_missing_links = 1;
@@ -4462,6 +4569,8 @@ int cmd_pack_objects(int argc,
44624569
N_("use the sparse reachability algorithm")),
44634570
OPT_BOOL(0, "thin", &thin,
44644571
N_("create thin packs")),
4572+
OPT_BOOL(0, "path-walk", &path_walk,
4573+
N_("use the path-walk API to walk objects when possible")),
44654574
OPT_BOOL(0, "shallow", &shallow,
44664575
N_("create packs suitable for shallow fetches")),
44674576
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
@@ -4547,7 +4656,27 @@ int cmd_pack_objects(int argc,
45474656
window = 0;
45484657

45494658
strvec_push(&rp, "pack-objects");
4550-
if (thin) {
4659+
4660+
if (path_walk && filter_options.choice) {
4661+
warning(_("cannot use --filter with --path-walk"));
4662+
path_walk = 0;
4663+
}
4664+
if (path_walk && use_delta_islands) {
4665+
warning(_("cannot use delta islands with --path-walk"));
4666+
path_walk = 0;
4667+
}
4668+
if (path_walk && shallow) {
4669+
warning(_("cannot use --shallow with --path-walk"));
4670+
path_walk = 0;
4671+
}
4672+
if (path_walk) {
4673+
strvec_push(&rp, "--boundary");
4674+
/*
4675+
* We must disable the bitmaps because we are removing
4676+
* the --objects / --objects-edge[-aggressive] options.
4677+
*/
4678+
use_bitmap_index = 0;
4679+
} else if (thin) {
45514680
use_internal_rev_list = 1;
45524681
strvec_push(&rp, shallow
45534682
? "--objects-edge-aggressive"

t/t5300-pack-object.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,21 @@ test_expect_success '--name-hash-version=2 and --write-bitmap-index are incompat
723723
! test_grep "currently, --write-bitmap-index requires --name-hash-version=1" err
724724
'
725725

726+
# Basic "repack everything" test
727+
test_expect_success '--path-walk pack everything' '
728+
git -C server rev-parse HEAD >in &&
729+
git -C server pack-objects --stdout --revs --path-walk <in >out.pack &&
730+
git -C server index-pack --stdin <out.pack
731+
'
732+
733+
# Basic "thin pack" test
734+
test_expect_success '--path-walk thin pack' '
735+
cat >in <<-EOF &&
736+
$(git -C server rev-parse HEAD)
737+
^$(git -C server rev-parse HEAD~2)
738+
EOF
739+
git -C server pack-objects --thin --stdout --revs --path-walk <in >out.pack &&
740+
git -C server index-pack --fix-thin --stdin <out.pack
741+
'
742+
726743
test_done

0 commit comments

Comments
 (0)