git/t/t1419-exclude-refs.sh
Patrick Steinhardt 1869525066 refs/reftable: wire up support for exclude patterns
Exclude patterns can be used by reference backends to skip over blocks
of references that are uninteresting to the caller. Reference backends
do not have to wire up support for them, and all callers are expected to
behave as if the backend didn't support them. In fact, the only backend
that supports exclude patterns right now is the "packed" backend.

Exclude patterns can be quite an important performance optimization in
repositories that have loads of references. The patterns are set up in
case "transfer.hideRefs" and friends are configured during a fetch, so
handling these patterns becomes important once there are lots of hidden
refs in a served repository.

Now that we have properly re-seekable reftable iterators we can also
wire up support for these patterns in the "reftable" backend. Doing so
is conceptually simple: once we hit a reference whose prefix matches the
current exclude pattern we re-seek the iterator to the first reference
that doesn't match the pattern anymore. This schema only works for
trivial patterns that do not have any globbing characters in them, but
this restriction also applies do the "packed" backend.

This makes t1419 work with the "reftable" backend with some slight
modifications. Of course it also speeds up listing of references with
hidden refs. The following benchmark prints one reference with 1 million
hidden references:

    Benchmark 1: HEAD~
      Time (mean ± σ):      93.3 ms ±   2.1 ms    [User: 90.3 ms, System: 2.5 ms]
      Range (min … max):    89.8 ms …  97.2 ms    33 runs

    Benchmark 2: HEAD
      Time (mean ± σ):       4.2 ms ±   0.6 ms    [User: 2.2 ms, System: 1.8 ms]
      Range (min … max):     3.1 ms …   8.1 ms    765 runs

    Summary
      HEAD ran
       22.15 ± 3.19 times faster than HEAD~

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-09-16 13:57:19 -07:00

160 lines
3.8 KiB
Bash
Executable file

#!/bin/sh
test_description='test exclude_patterns functionality in main ref store'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
for_each_ref__exclude () {
GIT_TRACE2_PERF=1 test-tool ref-store main \
for-each-ref--exclude "$@" >actual.raw
cut -d ' ' -f 2 actual.raw
}
for_each_ref () {
git for-each-ref --format='%(refname)' "$@"
}
assert_jumps () {
local nr="$1"
local trace="$2"
case "$GIT_DEFAULT_REF_FORMAT" in
files)
grep -q "name:jumps_made value:$nr$" $trace;;
reftable)
grep -q "name:reseeks_made value:$nr$" $trace;;
*)
BUG "unhandled ref format $GIT_DEFAULT_REF_FORMAT";;
esac
}
assert_no_jumps () {
! assert_jumps ".*" "$1"
}
test_expect_success 'setup' '
test_commit --no-tag base &&
base="$(git rev-parse HEAD)" &&
for name in foo bar baz quux
do
for i in 1 2 3
do
echo "create refs/heads/$name/$i $base" || return 1
done || return 1
done >in &&
echo "delete refs/heads/main" >>in &&
git update-ref --stdin <in &&
git pack-refs --all
'
test_expect_success 'excluded region in middle' '
for_each_ref__exclude refs/heads refs/heads/foo >actual 2>perf &&
for_each_ref refs/heads/bar refs/heads/baz refs/heads/quux >expect &&
test_cmp expect actual &&
assert_jumps 1 perf
'
test_expect_success 'excluded region at beginning' '
for_each_ref__exclude refs/heads refs/heads/bar >actual 2>perf &&
for_each_ref refs/heads/baz refs/heads/foo refs/heads/quux >expect &&
test_cmp expect actual &&
assert_jumps 1 perf
'
test_expect_success 'excluded region at end' '
for_each_ref__exclude refs/heads refs/heads/quux >actual 2>perf &&
for_each_ref refs/heads/foo refs/heads/bar refs/heads/baz >expect &&
test_cmp expect actual &&
assert_jumps 1 perf
'
test_expect_success 'disjoint excluded regions' '
for_each_ref__exclude refs/heads refs/heads/bar refs/heads/quux >actual 2>perf &&
for_each_ref refs/heads/baz refs/heads/foo >expect &&
test_cmp expect actual &&
assert_jumps 2 perf
'
test_expect_success 'adjacent, non-overlapping excluded regions' '
for_each_ref__exclude refs/heads refs/heads/bar refs/heads/baz >actual 2>perf &&
for_each_ref refs/heads/foo refs/heads/quux >expect &&
test_cmp expect actual &&
case "$GIT_DEFAULT_REF_FORMAT" in
files)
assert_jumps 1 perf;;
reftable)
assert_jumps 2 perf;;
*)
BUG "unhandled ref format $GIT_DEFAULT_REF_FORMAT";;
esac
'
test_expect_success 'overlapping excluded regions' '
for_each_ref__exclude refs/heads refs/heads/ba refs/heads/baz >actual 2>perf &&
for_each_ref refs/heads/foo refs/heads/quux >expect &&
test_cmp expect actual &&
assert_jumps 1 perf
'
test_expect_success 'several overlapping excluded regions' '
for_each_ref__exclude refs/heads \
refs/heads/bar refs/heads/baz refs/heads/foo >actual 2>perf &&
for_each_ref refs/heads/quux >expect &&
test_cmp expect actual &&
case "$GIT_DEFAULT_REF_FORMAT" in
files)
assert_jumps 1 perf;;
reftable)
assert_jumps 3 perf;;
*)
BUG "unhandled ref format $GIT_DEFAULT_REF_FORMAT";;
esac
'
test_expect_success 'unordered excludes' '
for_each_ref__exclude refs/heads \
refs/heads/foo refs/heads/baz >actual 2>perf &&
for_each_ref refs/heads/bar refs/heads/quux >expect &&
test_cmp expect actual &&
case "$GIT_DEFAULT_REF_FORMAT" in
files)
assert_jumps 1 perf;;
reftable)
assert_jumps 2 perf;;
*)
BUG "unhandled ref format $GIT_DEFAULT_REF_FORMAT";;
esac
'
test_expect_success 'non-matching excluded section' '
for_each_ref__exclude refs/heads refs/heads/does/not/exist >actual 2>perf &&
for_each_ref >expect &&
test_cmp expect actual &&
assert_no_jumps perf
'
test_expect_success 'meta-characters are discarded' '
for_each_ref__exclude refs/heads "refs/heads/ba*" >actual 2>perf &&
for_each_ref >expect &&
test_cmp expect actual &&
assert_no_jumps perf
'
test_done