Skip to content

Commit 3f2c20a

Browse files
derrickstoleedscho
authored andcommitted
pack-objects: add --full-name-hash option
The pack_name_hash() method has not been materially changed since it was introduced in ce0bd64 (pack-objects: improve path grouping heuristics., 2006-06-05). The intention here is to group objects by path name, but also attempt to group similar file types together by making the most-significant digits of the hash be focused on the final characters. Here's the crux of the implementation: /* * This effectively just creates a sortable number from the * last sixteen non-whitespace characters. Last characters * count "most", so things that end in ".c" sort together. */ while ((c = *name++) != 0) { if (isspace(c)) continue; hash = (hash >> 2) + (c << 24); } As the comment mentions, this only cares about the last sixteen non-whitespace characters. This cause some filenames to collide more than others. Here are some examples that I've seen while investigating repositories that are growing more than they should be: * "/CHANGELOG.json" is 15 characters, and is created by the beachball [1] tool. Only the final character of the parent directory can differntiate different versions of this file, but also only the two most-significant digits. If that character is a letter, then this is always a collision. Similar issues occur with the similar "/CHANGELOG.md" path, though there is more opportunity for differences in the parent directory. * Localization files frequently have common filenames but differentiate via parent directories. In C#, the name "/strings.resx.lcl" is used for these localization files and they will all collide in name-hash. [1] https://github.com/microsoft/beachball I've come across many other examples where some internal tool uses a common name across multiple directories and is causing Git to repack poorly due to name-hash collisions. It is clear that the existing name-hash algorithm is optimized for repositories with short path names, but also is optimized for packing a single snapshot of a repository, not a repository with many versions of the same file. In my testing, this has proven out where the name-hash algorithm does a good job of finding peer files as delta bases when unable to use a historical version of that exact file. However, for repositories that have many versions of most files and directories, it is more important that the objects that appear at the same path are grouped together. Create a new pack_full_name_hash() method and a new --full-name-hash option for 'git pack-objects' to call that method instead. Add a simple pass-through for 'git repack --full-name-hash' for additional testing in the context of a full repack, where I expect this will be most effective. The hash algorithm is as simple as possible to be reasonably effective: for each character of the path string, add a multiple of that character and a large prime number (chosen arbitrarily, but intended to be large relative to the size of a uint32_t). Then, shift the current hash value to the right by 5, with overlap. The addition and shift parameters are standard mechanisms for creating hard-to-predict behaviors in the bits of the resulting hash. This is not meant to be cryptographic at all, but uniformly distributed across the possible hash values. This creates a hash that appears pseudorandom. There is no ability to consider similar file types as being close to each other. In a later change, a test-tool will be added so the effectiveness of this hash can be demonstrated directly. For now, let's consider how effective this mechanism is when repacking a repository with and without the --full-name-hash option. Specifically, let's use 'git repack -adf [--full-name-hash]' as our test. On the Git repository, we do not expect much difference. All path names are short. This is backed by our results: | Stage | Pack Size | Repack Time | |-----------------------|-----------|-------------| | After clone | 260 MB | N/A | | Standard Repack | 127MB | 106s | | With --full-name-hash | 126 MB | 99s | This example demonstrates how there is some natural overhead coming from the cloned copy because the server is hosting many forks and has not optimized for exactly this set of reachable objects. But the full repack has similar characteristics with and without --full-name-hash. However, we can test this in a repository that uses one of the problematic naming conventions above. The fluentui [2] repo uses beachball to generate CHANGELOG.json and CHANGELOG.md files, and these files have very poor delta characteristics when comparing against versions across parent directories. | Stage | Pack Size | Repack Time | |-----------------------|-----------|-------------| | After clone | 694 MB | N/A | | Standard Repack | 438 MB | 728s | | With --full-name-hash | 168 MB | 142s | [2] https://github.com/microsoft/fluentui In this example, we see significant gains in the compressed packfile size as well as the time taken to compute the packfile. Using a collection of repositories that use the beachball tool, I was able to make similar comparisions with dramatic results. While the fluentui repo is public, the others are private so cannot be shared for reproduction. The results are so significant that I find it important to share here: | Repo | Standard Repack | With --full-name-hash | |----------|-----------------|-----------------------| | fluentui | 438 MB | 168 MB | | Repo B | 6,255 MB | 829 MB | | Repo C | 37,737 MB | 7,125 MB | | Repo D | 130,049 MB | 6,190 MB | Future changes could include making --full-name-hash implied by a config value or even implied by default during a full repack. Signed-off-by: Derrick Stolee <[email protected]>
1 parent e346cc0 commit 3f2c20a

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

Documentation/git-pack-objects.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ SYNOPSIS
1515
[--revs [--unpacked | --all]] [--keep-pack=<pack-name>]
1616
[--cruft] [--cruft-expiration=<time>]
1717
[--stdout [--filter=<filter-spec>] | <base-name>]
18-
[--shallow] [--keep-true-parents] [--[no-]sparse] < <object-list>
18+
[--shallow] [--keep-true-parents] [--[no-]sparse]
19+
[--full-name-hash] < <object-list>
1920

2021

2122
DESCRIPTION

builtin/pack-objects.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ struct configured_exclusion {
266266
static struct oidmap configured_exclusions;
267267

268268
static struct oidset excluded_by_config;
269+
static int use_full_name_hash;
270+
271+
static inline uint32_t pack_name_hash_fn(const char *name)
272+
{
273+
if (use_full_name_hash)
274+
return pack_full_name_hash(name);
275+
return pack_name_hash(name);
276+
}
269277

270278
/*
271279
* stats
@@ -1670,7 +1678,7 @@ static int add_object_entry(const struct object_id *oid, enum object_type type,
16701678
return 0;
16711679
}
16721680

1673-
create_object_entry(oid, type, pack_name_hash(name),
1681+
create_object_entry(oid, type, pack_name_hash_fn(name),
16741682
exclude, name && no_try_delta(name),
16751683
found_pack, found_offset);
16761684
return 1;
@@ -1884,7 +1892,7 @@ static void add_preferred_base_object(const char *name)
18841892
{
18851893
struct pbase_tree *it;
18861894
size_t cmplen;
1887-
unsigned hash = pack_name_hash(name);
1895+
unsigned hash = pack_name_hash_fn(name);
18881896

18891897
if (!num_preferred_base || check_pbase_path(hash))
18901898
return;
@@ -3394,7 +3402,7 @@ static void show_object_pack_hint(struct object *object, const char *name,
33943402
* here using a now in order to perhaps improve the delta selection
33953403
* process.
33963404
*/
3397-
oe->hash = pack_name_hash(name);
3405+
oe->hash = pack_name_hash_fn(name);
33983406
oe->no_try_delta = name && no_try_delta(name);
33993407

34003408
stdin_packs_hints_nr++;
@@ -3544,7 +3552,7 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
35443552
entry = packlist_find(&to_pack, oid);
35453553
if (entry) {
35463554
if (name) {
3547-
entry->hash = pack_name_hash(name);
3555+
entry->hash = pack_name_hash_fn(name);
35483556
entry->no_try_delta = no_try_delta(name);
35493557
}
35503558
} else {
@@ -3567,7 +3575,7 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
35673575
return;
35683576
}
35693577

3570-
entry = create_object_entry(oid, type, pack_name_hash(name),
3578+
entry = create_object_entry(oid, type, pack_name_hash_fn(name),
35713579
0, name && no_try_delta(name),
35723580
pack, offset);
35733581
}
@@ -4397,6 +4405,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
43974405
OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
43984406
N_("protocol"),
43994407
N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4408+
OPT_BOOL(0, "full-name-hash", &use_full_name_hash,
4409+
N_("optimize delta compression across identical path names over time")),
44004410
OPT_END(),
44014411
};
44024412

@@ -4544,6 +4554,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
45444554
if (pack_to_stdout || !rev_list_all)
45454555
write_bitmap_index = 0;
45464556

4557+
if (write_bitmap_index && use_full_name_hash)
4558+
die(_("currently, the --full-name-hash option is incompatible with --write-bitmap-index"));
4559+
45474560
if (use_delta_islands)
45484561
strvec_push(&rp, "--topo-order");
45494562

builtin/repack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct pack_objects_args {
5757
int no_reuse_object;
5858
int quiet;
5959
int local;
60+
int full_name_hash;
6061
struct list_objects_filter_options filter_options;
6162
};
6263

@@ -288,6 +289,8 @@ static void prepare_pack_objects(struct child_process *cmd,
288289
strvec_pushf(&cmd->args, "--no-reuse-delta");
289290
if (args->no_reuse_object)
290291
strvec_pushf(&cmd->args, "--no-reuse-object");
292+
if (args->full_name_hash)
293+
strvec_pushf(&cmd->args, "--full-name-hash");
291294
if (args->local)
292295
strvec_push(&cmd->args, "--local");
293296
if (args->quiet)
@@ -1157,6 +1160,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11571160
N_("pass --no-reuse-delta to git-pack-objects")),
11581161
OPT_BOOL('F', NULL, &po_args.no_reuse_object,
11591162
N_("pass --no-reuse-object to git-pack-objects")),
1163+
OPT_BOOL(0, "full-name-hash", &po_args.full_name_hash,
1164+
N_("pass --full-name-hash to git-pack-objects")),
11601165
OPT_NEGBIT('n', NULL, &run_update_server_info,
11611166
N_("do not run git-update-server-info"), 1),
11621167
OPT__QUIET(&po_args.quiet, N_("be quiet")),

pack-objects.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ static inline uint32_t pack_name_hash(const char *name)
207207
return hash;
208208
}
209209

210+
static inline uint32_t pack_full_name_hash(const char *name)
211+
{
212+
const uint32_t bigp = 1234572167U;
213+
uint32_t c, hash = bigp;
214+
215+
if (!name)
216+
return 0;
217+
218+
/*
219+
* Do the simplest thing that will resemble pseudo-randomness: add
220+
* random multiples of a large prime number with a binary shift.
221+
* The goal is not to be cryptographic, but to be generally
222+
* uniformly distributed.
223+
*/
224+
while ((c = *name++) != 0) {
225+
hash += c * bigp;
226+
hash = (hash >> 5) | (hash << 27);
227+
}
228+
return hash;
229+
}
230+
210231
static inline enum object_type oe_type(const struct object_entry *e)
211232
{
212233
return e->type_valid ? e->type_ : OBJ_BAD;

t/t5300-pack-object.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,19 @@ do
674674
'
675675
done
676676

677+
# The following test is not necessarily a permanent choice, but since we do not
678+
# have a "name hash version" bit in the .bitmap file format, we cannot write the
679+
# full-name hash values into the .bitmap file without risking breakage later.
680+
#
681+
# TODO: Make these compatible in the future and replace this test with the
682+
# expected behavior when both are specified.
683+
test_expect_success '--full-name-hash and --write-bitmap-index are incompatible' '
684+
test_must_fail git pack-objects base --all \
685+
--full-name-hash --write-bitmap-index 2>err &&
686+
grep incompatible err &&
687+
688+
# --stdout option silently removes --write-bitmap-index
689+
git pack-objects --stdout --all --full-name-hash --write-bitmap-index >out
690+
'
691+
677692
test_done

0 commit comments

Comments
 (0)