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

Re: [PATCH 15/16] commit-reach: make can_all_from_reach... linear

From
Derrick Stolee <stolee@gmail.com>
Date
Oct 15, 2018, 15:31 UTC
Message-ID
<0cd89b69-4b65-f961-a025-c74851f8c999@gmail.com>
In-Reply-To
<c141fb44-904f-e8b6-119f-7d2d6bcfd81a@web.de>
On 10/14/2018 10:29 AM, René Scharfe wrote:
Show 31 quoted lines
> It still has some repetition, converted code is a bit longer than the
> current one, and I don't know how to build a Coccinelle rule that would
> do that conversion.
>
> Looked for a possibility to at least leave QSORT call-sites alone by
> enhancing that macro, but didn't find any.  Found a few websites
> showing off mindblowing macros, thouhg, this one in particular:
>
> https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
>
> Anyway, drove the generative approach a bit further, and came up with
> the new DEFINE_SORT below.  I'm unsure about the name; perhaps it should
> be called DEFINE_SORT_BY_COMPARE_FUNCTION_BODY, but that's a bit long.
> It handles casts and const attributes behind the scenes and avoids
> repetition, but looks a bit weird, as it is placed where a function
> signature would go.
>
> Apart from that the macro is simple and doesn't use any tricks or
> added checks.  It just sets up boilerplate functions to offer type-safe
> sorting.
>
> diffcore-rename.c and refs/packed-backend.c receive special treatment in
> the patch because their compare functions are used outside of sorting as
> well.  I made them take typed pointers nevertheless and used them from
> DEFINE_SORT; the wrapper generated by that macro is supposed to be
> private.  Given that such reuse is rare and I think we don't need a way
> to make it public.
>
> What do y'all think about this direction?
>
> ---
[snip]
Show 23 quoted lines
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 5f2e90932f..491230fc57 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -1066,6 +1066,21 @@ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
>   		qsort(base, nmemb, size, compar);
>   }
>   
> +#define DECLARE_SORT(scope, name, elemtype) \
> +scope void name(elemtype, size_t)
> +
> +#define DEFINE_SORT(scope, name, elemtype, one, two)			\
> +static int name##_compare(const elemtype, const elemtype);		\
> +static int name##_compare_void(const void *a, const void *b)		\
> +{									\
> +	return name##_compare(a, b);					\
> +}									\
> +scope void name(elemtype base, size_t nmemb)				\
> +{									\
> +	QSORT(base, nmemb, name##_compare_void);			\
> +}									\
> +static int name##_compare(const elemtype one, const elemtype two)
> +

Since you were worried about the "private" name of the compare function, maybe split this macro into two: DEFINE_COMPARE and DEFINE_SORT. Then, if someone wants direct access to the compare function, they could use the DEFINE_COMPARE to ensure the typing is correct, and use QSORT as normal with name##_compare_void.

As I think about this, I think this is less of a problem than is worth this split. The commit-slab definitions generate a lot of methods using the "name##" convention, so perhaps we should just trust developers using the macros to look up the macro definition or similar examples. In that sense, including a conversion that consumes the compare function directly can be a signpost for future callers.

I would say that maybe the times where you need to do something special should be pulled out into their own patches, so we can call attention to them directly.

[snip]
Show 22 quoted lines
> diff --git a/midx.c b/midx.c
> index 713d6f9dde..4407db7949 100644
> --- a/midx.c
> +++ b/midx.c
> @@ -419,10 +419,8 @@ struct pack_pair {
>   	char *pack_name;
>   };
>   
> -static int pack_pair_compare(const void *_a, const void *_b)
> +DEFINE_SORT(static, sort_by_pack_name, struct pack_pair *, a, b)
>   {
> -	struct pack_pair *a = (struct pack_pair *)_a;
> -	struct pack_pair *b = (struct pack_pair *)_b;
>   	return strcmp(a->pack_name, b->pack_name);
>   }
>   
> @@ -438,7 +436,7 @@ static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *p
>   		pairs[i].pack_name = pack_names[i];
>   	}
>   
> -	QSORT(pairs, nr_packs, pack_pair_compare);
> +	sort_by_pack_name(pairs, nr_packs);
I like this "sort_by_" convention..
Show 21 quoted lines
>   
>   	for (i = 0; i < nr_packs; i++) {
>   		pack_names[i] = pairs[i].pack_name;
> @@ -455,10 +453,8 @@ struct pack_midx_entry {
>   	uint64_t offset;
>   };
>   
> -static int midx_oid_compare(const void *_a, const void *_b)
> +DEFINE_SORT(static, sort_midx, struct pack_midx_entry *, a, b)
>   {
> -	const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
> -	const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
>   	int cmp = oidcmp(&a->oid, &b->oid);
>   
>   	if (cmp)
> @@ -573,7 +569,7 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
>   			}
>   		}
>   
> -		QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
> +		sort_midx(entries_by_fanout, nr_fanout);
...but it isn't followed here. Perhaps "sort_by_oid"?

Thanks, -Stolee

Previous: René ScharfeNext: René Scharfe
Message 25 of 118 in “Consolidate reachability logic”
  1. 00/16 Consolidate reachability logicDerrick Stolee via GitGitGadget, Jul 16, 2018
  2. 04/16 upload-pack: make reachable() more genericDerrick Stolee via GitGitGadget, Jun 19, 2018
  3. 05/16 upload-pack: refactor ok_to_give_up()Derrick Stolee via GitGitGadget, Jun 19, 2018
  4. 01/16 commit-reach: move walk methods from commit.cDerrick Stolee via GitGitGadget, Jun 25, 2018
  5. Stefan BellerJul 16, 2018
  6. Jonathan TanJul 16, 2018
  7. 02/16 commit-reach: move ref_newer from remote.cDerrick Stolee via GitGitGadget, Jun 25, 2018
  8. Stefan BellerJul 16, 2018
  9. 03/16 commit-reach: move commit_contains from ref-filterDerrick Stolee via GitGitGadget, Jun 25, 2018
  10. Stefan BellerJul 16, 2018
  11. 15/16 commit-reach: make can_all_from_reach... linearDerrick Stolee via GitGitGadget, Jun 28, 2018
  12. Stefan BellerJul 16, 2018
  13. Jonathan TanJul 17, 2018
  14. René ScharfeOct 1, 2018
  15. Derrick StoleeOct 1, 2018
  16. René ScharfeOct 1, 2018
  17. René ScharfeOct 4, 2018
  18. Derrick StoleeOct 5, 2018
  19. Jeff KingOct 5, 2018
  20. René ScharfeOct 5, 2018
  21. Jeff KingOct 5, 2018
  22. René ScharfeOct 5, 2018
  23. Jeff KingOct 5, 2018
  24. René ScharfeOct 14, 2018
  25. Derrick StoleeOct 15, 2018
  26. René ScharfeOct 15, 2018
  27. Junio C HamanoOct 16, 2018
  28. Jeff KingOct 17, 2018
  29. Jonathan NiederNov 18, 2020
  30. Jeff KingNov 18, 2020
  31. René ScharfeNov 18, 2020
  32. Ævar Arnfjörð BjarmasonOct 5, 2018
  33. Jeff KingOct 5, 2018
  34. Ævar Arnfjörð BjarmasonOct 5, 2018
  35. Jeff KingOct 5, 2018
  36. 06/16 upload-pack: generalize commit date cutoffDerrick Stolee via GitGitGadget, Jul 12, 2018
  37. Stefan BellerJul 16, 2018
  38. Derrick StoleeJul 18, 2018
  39. 07/16 commit-reach: move can_all_from_reach_with_flagsDerrick Stolee via GitGitGadget, Jul 12, 2018
  40. Jonathan TanJul 16, 2018
  41. 08/16 test-reach: create new test tool for ref_newerDerrick Stolee via GitGitGadget, Jul 13, 2018
  42. Jonathan TanJul 16, 2018
  43. Derrick StoleeJul 18, 2018
  44. 09/16 test-reach: test in_merge_basesDerrick Stolee via GitGitGadget, Jul 13, 2018
  45. 10/16 test-reach: test is_descendant_ofDerrick Stolee via GitGitGadget, Jul 13, 2018
  46. 11/16 test-reach: test get_merge_bases_manyDerrick Stolee via GitGitGadget, Jul 13, 2018
  47. Stefan BellerJul 16, 2018
  48. Jonathan TanJul 16, 2018
  49. 12/16 test-reach: test reduce_headsDerrick Stolee via GitGitGadget, Jul 13, 2018
  50. Stefan BellerJul 16, 2018
  51. Eric SunshineJul 16, 2018
  52. 13/16 test-reach: test can_all_from_reach_with_flagsDerrick Stolee via GitGitGadget, Jul 13, 2018
  53. Stefan BellerJul 16, 2018
  54. Derrick StoleeJul 18, 2018
  55. Jonathan TanJul 17, 2018
  56. 14/16 commit-reach: replace ref_newer logicDerrick Stolee via GitGitGadget, Jul 13, 2018
  57. Stefan BellerJul 16, 2018
  58. 16/16 commit-reach: use can_all_from_reachDerrick Stolee via GitGitGadget, Jul 13, 2018
  59. Stefan BellerJul 16, 2018
  60. Ramsay JonesJul 16, 2018
  61. Jeff KingJul 16, 2018
  62. Eric SunshineJul 16, 2018
  63. Jeff KingJul 16, 2018
  64. Eric SunshineJul 16, 2018
  65. Johannes SchindelinJul 18, 2018
  66. Johannes SchindelinJul 18, 2018
  67. Jeff KingJul 18, 2018
  68. Johannes SchindelinJul 19, 2018
  69. Stefan BellerJul 16, 2018
  70. Eric SunshineJul 16, 2018
  71. Derrick StoleeJul 16, 2018
  72. Johannes SchindelinJul 18, 2018
  73. Duy NguyenJul 18, 2018
  74. Junio C HamanoJul 18, 2018
  75. Derrick StoleeJul 18, 2018
  76. Johannes SchindelinJul 19, 2018
  77. Johannes SchindelinJul 19, 2018
  78. 00/18 Consolidate reachability logicDerrick Stolee, Jul 20, 2018
  79. 02/18 commit.h: remove method declarationsDerrick Stolee, Jul 20, 2018
  80. 03/18 commit-reach: move ref_newer from remote.cDerrick Stolee, Jul 20, 2018
  81. 04/18 commit-reach: move commit_contains from ref-filterDerrick Stolee, Jul 20, 2018
  82. Jonathan NiederAug 28, 2018
  83. Derrick StoleeAug 28, 2018
  84. commit-reach: correct accidental #include of C fileJonathan Nieder, Aug 28, 2018
  85. Derrick StoleeAug 28, 2018
  86. 05/18 upload-pack: make reachable() more genericDerrick Stolee, Jul 20, 2018
  87. 07/18 upload-pack: generalize commit date cutoffDerrick Stolee, Jul 20, 2018
  88. 06/18 upload-pack: refactor ok_to_give_up()Derrick Stolee, Jul 20, 2018
  89. 09/18 test-reach: create new test tool for ref_newerDerrick Stolee, Jul 20, 2018
  90. 08/18 commit-reach: move can_all_from_reach_with_flagsDerrick Stolee, Jul 20, 2018
  91. 11/18 test-reach: test is_descendant_ofDerrick Stolee, Jul 20, 2018
  92. 12/18 test-reach: test get_merge_bases_manyDerrick Stolee, Jul 20, 2018
  93. 10/18 test-reach: test in_merge_basesDerrick Stolee, Jul 20, 2018
  94. 14/18 test-reach: test can_all_from_reach_with_flagsDerrick Stolee, Jul 20, 2018
  95. 15/18 test-reach: test commit_containsDerrick Stolee, Jul 20, 2018
  96. Jonathan TanJul 23, 2018
  97. Junio C HamanoJul 25, 2018
  98. Derrick StoleeJul 25, 2018
  99. 16/18 commit-reach: replace ref_newer logicDerrick Stolee, Jul 20, 2018
  100. 01/18 commit-reach: move walk methods from commit.cDerrick Stolee, Jul 20, 2018
  101. 18/18 commit-reach: use can_all_from_reachDerrick Stolee, Jul 20, 2018
  102. 17/18 commit-reach: make can_all_from_reach... linearDerrick Stolee, Jul 20, 2018
  103. Jonathan TanJul 23, 2018
  104. Derrick StoleeAug 1, 2018
  105. Jeff KingSep 12, 2018
  106. Jeff KingSep 12, 2018
  107. Derrick StoleeSep 12, 2018
  108. 13/18 test-reach: test reduce_headsDerrick Stolee, Jul 20, 2018
  109. Stefan BellerJul 20, 2018
  110. Derrick StoleeJul 20, 2018
  111. Stefan BellerJul 20, 2018
  112. Derrick StoleeAug 1, 2018
  113. Derrick StoleeJul 20, 2018
  114. Eric SunshineJul 20, 2018
  115. Derrick StoleeJul 20, 2018
  116. Duy NguyenJul 20, 2018
  117. Derrick StoleeJul 20, 2018
  118. Junio C HamanoJul 20, 2018

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.