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
René Scharfe <l.s.r@web.de>
Date
Nov 18, 2020, 17:47 UTC
Message-ID
<c236ca17-0f9e-a900-8a50-f467eaf6b485@web.de>
In-Reply-To
<20201118065437.GA16996@coredump.intra.peff.net>
Am 18.11.20 um 07:54 schrieb Jeff King:
Show 12 quoted lines
> On Tue, Nov 17, 2020 at 06:16:05PM -0800, Jonathan Nieder wrote:
>
>> Since this came up in [1], I took a glance at this.
>>
>> I also think it looks reasonable, though it's possible to do better if
>> we're willing to (1) cast between pointers to function with different
>> signatures, which is portable in practice but I don't believe the C
>> standard speaks to and (2) conditionally make use of gcc extensions,
>> for typechecking.
>
> The C standard definitely is not OK with calling a function through a
> wrong declaration or cast. I won't find chapter and verse

http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf is a draft for C99, and says under 6.3 Conversions, 6.3.2.3 Pointers, paragraph 8:

   If a converted pointer is used to call a function whose type is not
   compatible with the pointed-to type, the behavior is undefined.
Show 18 quoted lines
> Now in practice you're probably fine as long as the number and sizes of
> the parameters are the same between the function definition and what the
> caller casts to. And so if we're talking about casting individual
> parameters between a void parameter and another pointer, that would
> usually be fine (in practice; the standard only says that void can store
> the type of anything, so it _could_ be larger than some other pointers.
> I don't know of any modern systems where this is true, though).
>
> Which is all a roundabout way of saying that yes, I think this kind of
> cast is probably OK in practice.
>
> I _think_ the ccan type-checking macro you pointed to would catch this
> sufficiently on systems with typeof() that it would also protect systems
> with different calling conventions. But I admit it's pretty dense.
>
> So I dunno. The nice thing is that this puts the ugliness all inside of
> QSORT(), which becomes magically type-safe. But it involves importing a
> lot of tricky bits under the hood.

A generic and type-safe QSORT would be nice, but if it calls a function via a converted pointer then it's technically relying on undefined behavior unless I misunderstand the standard. I prefer occasional mistakes (that are caught by ASan or USan or when the sort order actually matters) to guaranteed undefined behavior that happens to work, until it doesn't.

> The downside of René's patch is that it hides the declaration of the
> comparison function (and the typesafe wrapper) inside a macro. But the
> resulting code is (IMHO) pretty easy to comprehend.

I tried some more variants back then, before I dropped the ball eventually when RL distracted me. I think my favorite one was a DEFINE_SORT macro that took the name of a typed comparison function -- the code looks more like normal C.

Handling arrays of pointers was a bit tricky, and I had to introduce DEFINE_PTR_SORT and DEFINE_CONST_PTR_SORT for them, but they allowed to use the same comparison functions -- they consistently took element pointers. Just one rule to follow, and the compiler would yell when a mismatched macro was used.

And at that point it occurred to me that comparison functions would ideally take two elements, not pointers. All the pointer mangling could be done in generated code. GCC and Clang inline it appropriately, so this convenience would be free -- but other compilers don't, and that would make sorting more expensive on those platforms. Dead end.

The last one I looked at was a dumbed-down version, but I think this requires some weird comparison function signatures in some cases (patch below, basically untested).

I can understand now why monomorphization approaches like https://github.com/attractivechaos/klib/blob/master/ksort.h seem attractive (no pun intended)..

René
---
 git-compat-util.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index adfea06897..8d871a8e33 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1124,6 +1124,23 @@ int git_qsort_s(void *base, size_t nmemb, size_t size,
 		BUG("qsort_s() failed");			\
 } while (0)

+#define DECLARE_COMPARE(scope, compar)					\
+scope int compar##__void(const void *, const void *)
+
+#define DEFINE_COMPARE(scope, compar)					\
+scope int compar##__void(const void *a_void_ptr,			\
+			 const void *b_void_ptr)			\
+{									\
+	return compar(a_void_ptr, b_void_ptr);				\
+}									\
+DECLARE_COMPARE(scope, compar)
+
+#define GET_COMPARE(base, compar)					\
+(0 && compar((base), (base)) ? NULL : compar##__void)
+
+#define SORT_ARRAY(base, n, compar)					\
+QSORT((base), (n), GET_COMPARE((base), compar))
+
 #ifndef REG_STARTEND
 #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
 #endif
--
2.29.2
Previous: Jeff KingNext: Ævar Arnfjörð Bjarmason
Message 31 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.