git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 04/10] test-mergesort: use DEFINE_LIST_SORT_DEBUG

From
René Scharfe <l.s.r@web.de>
Date
Jul 16, 2022, 16:56 UTC
Message-ID
<dda728d4-eedc-c4c6-034e-0a5a03ed9059@web.de>
In-Reply-To
<4d7cd286-398e-215c-f2bd-aa7e8207be4f@web.de>

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>
---
 t/helper/test-mergesort.c | 19 ++++---------------
 t/t0071-sort.sh           |  2 +-
 2 files changed, 5 insertions(+), 16 deletions(-)
diff --git a/t/helper/test-mergesort.c b/t/helper/test-mergesort.c
index ebf68f7de8..93d15d59a1 100644
--- a/t/helper/test-mergesort.c
+++ b/t/helper/test-mergesort.c
@@ -273,21 +273,11 @@ struct number {
 	struct number *next;
 };

-static void *get_next_number(const void *a)
-{
-	stats.get_next++;
-	return ((const struct number *)a)->next;
-}
-
-static void set_next_number(void *a, void *b)
-{
-	stats.set_next++;
-	((struct number *)a)->next = b;
-}
+DEFINE_LIST_SORT_DEBUG(static, sort_numbers, struct number, next,
+		       stats.get_next++, stats.set_next++);

-static int compare_numbers(const void *av, const void *bv)
+static int compare_numbers(const struct number *an, const struct number *bn)
 {
-	const struct number *an = av, *bn = bv;
 	int a = an->value, b = bn->value;
 	stats.compare++;
 	return (a > b) - (a < b);
@@ -325,8 +315,7 @@ static int test(const struct dist *dist, const struct mode *mode, int n, int m)
 	*tail = NULL;

 	stats.get_next = stats.set_next = stats.compare = 0;
-	list = llist_mergesort(list, get_next_number, set_next_number,
-			       compare_numbers);
+	sort_numbers(&list, compare_numbers);

 	QSORT(arr, n, compare_ints);
 	for (i = 0, curr = list; i < n && curr; i++, curr = curr->next) {
diff --git a/t/t0071-sort.sh b/t/t0071-sort.sh
index 6f9a501c72..ba8ad1d1ca 100755
--- a/t/t0071-sort.sh
+++ b/t/t0071-sort.sh
@@ -5,7 +5,7 @@ test_description='verify sort functions'
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh

-test_expect_success 'llist_mergesort()' '
+test_expect_success 'DEFINE_LIST_SORT_DEBUG' '
 	test-tool mergesort test
 '

--
2.37.1
Previous: René ScharfeNext: René Scharfe
Message 5 of 15 in “typed sort of linked lists”
  1. 00/10 typed sort of linked listsRené Scharfe, Jul 16, 2022
  2. 01/10 mergesort: unify ranks loopsRené Scharfe, Jul 16, 2022
  3. 02/10 mergesort: tighten merge loopRené Scharfe, Jul 16, 2022
  4. 03/10 mergesort: add macros for typed sort of linked listsRené Scharfe, Jul 16, 2022
  5. 04/10 test-mergesort: use DEFINE_LIST_SORT_DEBUGRené Scharfe, Jul 16, 2022
  6. 05/10 test-mergesort: use DEFINE_LIST_SORTRené Scharfe, Jul 16, 2022
  7. 06/10 blame: use DEFINE_LIST_SORTRené Scharfe, Jul 16, 2022
  8. 07/10 commit: use DEFINE_LIST_SORTRené Scharfe, Jul 16, 2022
  9. 08/10 fetch-pack: use DEFINE_LIST_SORTRené Scharfe, Jul 16, 2022
  10. 09/10 packfile: use DEFINE_LIST_SORTRené Scharfe, Jul 16, 2022
  11. 10/10 mergesort: remove llist_mergesort()René Scharfe, Jul 16, 2022
  12. Junio C HamanoJul 17, 2022
  13. Junio C HamanoJul 25, 2022
  14. Derrick StoleeJul 25, 2022
  15. Junio C HamanoJul 25, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.