Skip to content

Commit ab341dd

Browse files
committed
test-tool: add helper for name-hash values
Add a new test-tool helper, name-hash, to output the value of the name-hash algorithms for the input list of strings, one per line. Since the name-hash values can be stored in the .bitmap files, it is important that these hash functions do not change across Git versions. Add a simple test to t5310-pack-bitmaps.sh to provide some testing of the current values. Due to how these functions are implemented, it would be difficult to change them without disturbing these values. Create a performance test that uses test_size to demonstrate how collisions occur for these hash algorithms. This test helps inform someone as to the behavior of the name-hash algorithms for their repo based on the paths at HEAD. My copy of the Git repository shows modest statistics around the collisions of the default name-hash algorithm: Test this tree ----------------------------------------------------------------- 5314.1: paths at head 4.5K 5314.2: number of distinct name-hashes 4.1K 5314.3: number of distinct full-name-hashes 4.5K 5314.4: maximum multiplicity of name-hashes 13 5314.5: maximum multiplicity of fullname-hashes 1 Here, the maximum collision multiplicity is 13, but around 10% of paths have a collision with another path. In a more interesting example, the microsoft/fluentui [1] repo had these statistics at time of committing: Test this tree ----------------------------------------------------------------- 5314.1: paths at head 19.6K 5314.2: number of distinct name-hashes 8.2K 5314.3: number of distinct full-name-hashes 19.6K 5314.4: maximum multiplicity of name-hashes 279 5314.5: maximum multiplicity of fullname-hashes 1 [1] https://github.com/microsoft/fluentui That demonstrates that of the nearly twenty thousand path names, they are assigned around eight thousand distinct values. 279 paths are assigned to a single value, leading the packing algorithm to sort objects from those paths together, by size. In this repository, no collisions occur for the full-name-hash algorithm. In a more extreme example, an internal monorepo had a much worse collision rate: Test this tree ----------------------------------------------------------------- 5314.1: paths at head 221.6K 5314.2: number of distinct name-hashes 72.0K 5314.3: number of distinct full-name-hashes 221.6K 5314.4: maximum multiplicity of name-hashes 14.4K 5314.5: maximum multiplicity of fullname-hashes 2 Even in this repository with many more paths at HEAD, the collision rate was low and the maximum number of paths being grouped into a single bucket by the full-path-name algorithm was two. Signed-off-by: Derrick Stolee <[email protected]>
1 parent b8a055c commit ab341dd

6 files changed

+94
-0
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
816816
TEST_BUILTINS_OBJS += test-match-trees.o
817817
TEST_BUILTINS_OBJS += test-mergesort.o
818818
TEST_BUILTINS_OBJS += test-mktemp.o
819+
TEST_BUILTINS_OBJS += test-name-hash.o
819820
TEST_BUILTINS_OBJS += test-online-cpus.o
820821
TEST_BUILTINS_OBJS += test-pack-mtimes.o
821822
TEST_BUILTINS_OBJS += test-parse-options.o

t/helper/test-name-hash.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* test-name-hash.c: Read a list of paths over stdin and report on their
3+
* name-hash and full name-hash.
4+
*/
5+
6+
#include "test-tool.h"
7+
#include "git-compat-util.h"
8+
#include "pack-objects.h"
9+
#include "strbuf.h"
10+
11+
int cmd__name_hash(int argc UNUSED, const char **argv UNUSED)
12+
{
13+
struct strbuf line = STRBUF_INIT;
14+
15+
while (!strbuf_getline(&line, stdin)) {
16+
uint32_t name_hash = pack_name_hash(line.buf);
17+
uint32_t full_hash = pack_full_name_hash(line.buf);
18+
19+
printf("%10"PRIu32"\t%10"PRIu32"\t%s\n", name_hash, full_hash, line.buf);
20+
}
21+
22+
strbuf_release(&line);
23+
return 0;
24+
}

t/helper/test-tool.c

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static struct test_cmd cmds[] = {
4444
{ "match-trees", cmd__match_trees },
4545
{ "mergesort", cmd__mergesort },
4646
{ "mktemp", cmd__mktemp },
47+
{ "name-hash", cmd__name_hash },
4748
{ "online-cpus", cmd__online_cpus },
4849
{ "pack-mtimes", cmd__pack_mtimes },
4950
{ "parse-options", cmd__parse_options },

t/helper/test-tool.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int cmd__lazy_init_name_hash(int argc, const char **argv);
3737
int cmd__match_trees(int argc, const char **argv);
3838
int cmd__mergesort(int argc, const char **argv);
3939
int cmd__mktemp(int argc, const char **argv);
40+
int cmd__name_hash(int argc, const char **argv);
4041
int cmd__online_cpus(int argc, const char **argv);
4142
int cmd__pack_mtimes(int argc, const char **argv);
4243
int cmd__parse_options(int argc, const char **argv);

t/perf/p5314-name-hash.sh

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
test_description='Tests pack performance using bitmaps'
4+
. ./perf-lib.sh
5+
6+
GIT_TEST_PASSING_SANITIZE_LEAK=0
7+
export GIT_TEST_PASSING_SANITIZE_LEAK
8+
9+
test_perf_large_repo
10+
11+
test_size 'paths at head' '
12+
git ls-tree -r --name-only HEAD >path-list &&
13+
wc -l <path-list
14+
'
15+
16+
test_size 'number of distinct name-hashes' '
17+
cat path-list | test-tool name-hash >name-hashes &&
18+
cat name-hashes | awk "{ print \$1; }" | sort -n | uniq -c >name-hash-count &&
19+
wc -l <name-hash-count
20+
'
21+
22+
test_size 'number of distinct full-name-hashes' '
23+
cat name-hashes | awk "{ print \$2; }" | sort -n | uniq -c >full-name-hash-count &&
24+
wc -l <full-name-hash-count
25+
'
26+
27+
test_size 'maximum multiplicity of name-hashes' '
28+
cat name-hash-count | \
29+
sort -nr | \
30+
head -n 1 | \
31+
awk "{ print \$1; }"
32+
'
33+
34+
test_size 'maximum multiplicity of fullname-hashes' '
35+
cat full-name-hash-count | \
36+
sort -nr | \
37+
head -n 1 | \
38+
awk "{ print \$1; }"
39+
'
40+
41+
test_done

t/t5310-pack-bitmaps.sh

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ has_any () {
2727
grep -Ff "$1" "$2"
2828
}
2929

30+
# Since name-hash values are stored in the .bitmap files, add a test
31+
# that checks that the name-hash calculations are stable across versions.
32+
# Not exhaustive, but these hashing algorithms would be hard to change
33+
# without causing deviations here.
34+
test_expect_success 'name-hash value stability' '
35+
cat >names <<-\EOF &&
36+
first
37+
second
38+
third
39+
one-long-enough-for-collisions
40+
two-long-enough-for-collisions
41+
EOF
42+
43+
test-tool name-hash <names >out &&
44+
45+
cat >expect <<-\EOF &&
46+
2582249472 3109209818 first
47+
2289942528 3781118409 second
48+
2300837888 3028707182 third
49+
2544516325 3241327563 one-long-enough-for-collisions
50+
2544516325 4207880830 two-long-enough-for-collisions
51+
EOF
52+
53+
test_cmp expect out
54+
'
55+
3056
test_bitmap_cases () {
3157
writeLookupTable=false
3258
for i in "$@"

0 commit comments

Comments
 (0)