test-mergesort: use DEFINE_LIST_SORT_DEBUG

Define a typed sort function using DEFINE_LIST_SORT_DEBUG for the mergesort sanity check instead of using llist_mergesort(). This gets rid of the next pointer accessor functions and improves the performance at the cost of slightly bigger object text. Before: Benchmark 1: t/helper/test-tool mergesort test Time (mean ± σ): 108.4 ms ± 0.2 ms [User: 106.7 ms, System: 1.2 ms] Range (min … max): 108.0 ms … 108.8 ms 27 runs __TEXT __DATA __OBJC others dec hex 6251 276 0 23172 29699 7403 t/helper/test-mergesort.o With this patch: Benchmark 1: t/helper/test-tool mergesort test Time (mean ± σ): 94.0 ms ± 0.2 ms [User: 92.4 ms, System: 1.1 ms] Range (min … max): 93.7 ms … 94.5 ms 31 runs __TEXT __DATA __OBJC others dec hex 6407 276 0 24701 31384 7a98 t/helper/test-mergesort.o Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 16, 2022 at 18:56 UTC f00a039839243c382682a3a5a757e1e6f88abd3d
2 files changed +5 -16
t/helper/test-mergesort.c
+4 -15
@@ -273,21 +273,11 @@ struct number {
273 struct number *next;
274 };
275
276 -static void *get_next_number(const void *a)
277 -{
278 - stats.get_next++;
279 - return ((const struct number *)a)->next;
280 -}
281 -
282 -static void set_next_number(void *a, void *b)
283 -{
284 - stats.set_next++;
285 - ((struct number *)a)->next = b;
286 -}
276 +DEFINE_LIST_SORT_DEBUG(static, sort_numbers, struct number, next,
277 + stats.get_next++, stats.set_next++);
278
288 -static int compare_numbers(const void *av, const void *bv)
279 +static int compare_numbers(const struct number *an, const struct number *bn)
280 {
290 - const struct number *an = av, *bn = bv;
281 int a = an->value, b = bn->value;
282 stats.compare++;
283 return (a > b) - (a < b);
@@ -325,8 +315,7 @@ static int test(const struct dist *dist, const struct mode *mode, int n, int m)
315 *tail = NULL;
316
317 stats.get_next = stats.set_next = stats.compare = 0;
328 - list = llist_mergesort(list, get_next_number, set_next_number,
329 - compare_numbers);
318 + sort_numbers(&list, compare_numbers);
319
320 QSORT(arr, n, compare_ints);
321 for (i = 0, curr = list; i < n && curr; i++, curr = curr->next) {
t/t0071-sort.sh
+1 -1
@@ -5,7 +5,7 @@ test_description='verify sort functions'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 -test_expect_success 'llist_mergesort()' '
8 +test_expect_success 'DEFINE_LIST_SORT_DEBUG' '
9 test-tool mergesort test
10 '
11