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

[PATCH 07/10] commit: use DEFINE_LIST_SORT

From
René Scharfe <l.s.r@web.de>
Date
Jul 16, 2022, 16:59 UTC
Message-ID
<396b927e-5bbd-1b7d-669e-8e22740b5295@web.de>
In-Reply-To
<4d7cd286-398e-215c-f2bd-aa7e8207be4f@web.de>

Use DEFINE_LIST_SORT to build a typed sort function for commit_list entries instead of calling llist_mergesort(). This gets rid of the next pointer accessor functions and their calling overhead at the cost of a slightly increased object text size.

Before: __TEXT __DATA __OBJC others dec hex 18795 92 0 104654 123541 1e295 commit.o

With this patch: __TEXT __DATA __OBJC others dec hex 18963 92 0 106094 125149 1e8dd commit.o

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 commit.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
diff --git a/commit.c b/commit.c
index 1fb1b2ea90..0db461f973 100644
--- a/commit.c
+++ b/commit.c
@@ -642,10 +642,11 @@ struct commit_list * commit_list_insert_by_date(struct commit *item, struct comm
 	return commit_list_insert(item, pp);
 }

-static int commit_list_compare_by_date(const void *a, const void *b)
+static int commit_list_compare_by_date(const struct commit_list *a,
+				       const struct commit_list *b)
 {
-	timestamp_t a_date = ((const struct commit_list *)a)->item->date;
-	timestamp_t b_date = ((const struct commit_list *)b)->item->date;
+	timestamp_t a_date = a->item->date;
+	timestamp_t b_date = b->item->date;
 	if (a_date < b_date)
 		return 1;
 	if (a_date > b_date)
@@ -653,20 +654,11 @@ static int commit_list_compare_by_date(const void *a, const void *b)
 	return 0;
 }

-static void *commit_list_get_next(const void *a)
-{
-	return ((const struct commit_list *)a)->next;
-}
-
-static void commit_list_set_next(void *a, void *next)
-{
-	((struct commit_list *)a)->next = next;
-}
+DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);

 void commit_list_sort_by_date(struct commit_list **list)
 {
-	*list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
-				commit_list_compare_by_date);
+	commit_list_sort(list, commit_list_compare_by_date);
 }

 struct commit *pop_most_recent_commit(struct commit_list **list,
--
2.37.1
Previous: René ScharfeNext: René Scharfe
Message 8 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.