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

Re: [PATCH 01/10] ivec: introduce the C side of ivec

From
Ezekiel Newren <ezekielnewren@gmail.com>
Date
Jan 21, 2026, 21:39 UTC
Message-ID
<CAH=ZcbAiGONrOyma7YjNKKLqNFoisU5LG=nGWjtOJ1wLfqX4cQ@mail.gmail.com>
In-Reply-To
<08318339-03c3-4068-92fa-7a711bd13da0@gmail.com>
On Tue, Jan 20, 2026 at 7:06 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 28 quoted lines
>
> Hi Ezekiel
>
> On 15/01/2026 15:55, Ezekiel Newren wrote:
> > On Thu, Jan 8, 2026 at 7:34 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
> >>> +void ivec_reserve(void *self_, size_t additional)
> >>> +{
> >>> +     struct IVec_c_void *self = self_;
> >>> +
> >>> +     size_t growby = 128;
> >>> +     if (self->capacity > growby)
> >>> +             growby = self->capacity;
> >>> +     if (additional > growby)
> >>> +             growby = additional;
> >>
> >> This growth strategy differs from both ALLOC_GROW() and
> >> XDL_ALLOC_GROW(), if there isn't a good reason for that we should
> >> perhaps just use ALLOC_GROW() here.
> >
> > XDL_ALLOW_GROW() can't be used because the pointer is always a void*
> > in this function.
>
> Oh right. I'm not sure that's not a reason to use a different growth
> strategy though. The minimum size of 128 elements is probably good for
> the xdiff code that creates arrays with one element per line but if this
> is supposed to be for general use it is going to waste space when we're
> allocating a lot of small arrays. ALLOC_GROW() uses alloc_nr() to
> calculate the new side so perhaps we could use that here?

If ivec_reserve() isn't suitable then ivec_reserve_exact() should be used instead.

Show 22 quoted lines
> >>> +void ivec_push(void *self_, const void *value)
> >>> +{
> >>> +     struct IVec_c_void *self = self_;
> >>> +     void *dst = NULL;
> >>> +
> >>> +     if (self->length == self->capacity)
> >>> +             ivec_reserve(self, 1);
> >>> +
> >>> +     dst = (uint8_t*)self->ptr + self->length * self->element_size;
> >>> +     memcpy(dst, value, self->element_size);
> >>
> >> If self->element_size was a compile time constant the compiler could
> >> easily optimize this call away. I'm not sure that is easy to achieve though.
> >
> > The problem is that I didn't want all of ivec to be macros that looked
> > like function calls. I wanted to minimize use of macros so that it was
> > easier to port and verify that the Rust implementation matches the
> > behavior of the C implementation.
>
> I think that's a reasonable concern. So is the plan to have a parallel
> rust implementation of these functions rather than call the C
> implementation from rust?

Yes, the Rust implementation will be independent of the C implementation, but will behave the same way. That's why I'm calling it an interoperable vec as opposed to a compatible vec. Rust can't call the C ivec functions and C can't call the Rust ivec functions, but they'll behave the same way.

Show 11 quoted lines
> >>> +void ivec_free(void *self_)
> >>
> >> Normally we'd call a like this that free the allocations and
> >> re-initializes the members ivec_clear()
> >
> > In Rust Vec.clear() means to set length to zero, but leaves the
> > allocation alone. The reason why I'm zeroing the struct is to help
> > avoid FFI issues. If not zero then what should the members be set to,
> > to indicate that using the struct is not valid anymore? In Rust an
> > object is freed when it goes out of scope and _cannot_ be accessed
> > afterward.

Maybe I should call this ivec_drop(). Though the notion of explicitly freeing an object in Rust is _almost_ nonsense. The way you free something in Rust is to let it go out of scope.

Show 25 quoted lines
> I'm aware that Vec::clear() has different semantics (it does what
> strbuf_reset() does). That's unfortunate but this function has different
> semantics to all the other *_free() functions in git. Our coding
> guidelines say
>
>   - There are several common idiomatic names for functions performing
>     specific tasks on a structure `S`:
>
>      - `S_init()` initializes a structure without allocating the
>        structure itself.
>
>      - `S_release()` releases a structure's contents without freeing the
>        structure.
>
>      - `S_clear()` is equivalent to `S_release()` followed by `S_init()`
>        such that the structure is directly usable after clearing it. When
>        `S_clear()` is provided, `S_init()` shall not allocate resources
>        that need to be released again.
>
>      - `S_free()` releases a structure's contents and frees the
>        structure.
>
> As we write more rust code and so wrap more of our existing structs
> we're going to be wrapping C code that uses the definitions above so I
> think we should do the same with struct IVec_*.

I disagree. IVec isn't a wrapper around an existing struct. ivec is meant to very closely mimic Rust's Vec while guaranteeing interoperability. For things like strbuf I haven't conceived of a solution for that yet. Making ivec diverge from Rust's Vec will result in POLA violations due to different behavior when refactoring an IVec<your_type_here> to Vec<your_type_here>.

Show 34 quoted lines
> >>> diff --git a/compat/ivec.h b/compat/ivec.h
> >>> new file mode 100644
> >>> index 0000000000..654a05c506
> >>> --- /dev/null
> >>> +++ b/compat/ivec.h
> >>> @@ -0,0 +1,52 @@
> >>> +#ifndef IVEC_H
> >>> +#define IVEC_H
> >>> +
> >>> +#include <git-compat-util.h>
> >>
> >> It would be nice to have some documentation in this header, see the
> >> examples in strvec.h and hashmap.h
> >>
> >>> +#define IVEC_INIT(variable) ivec_init(&(variable), sizeof(*(variable).ptr))
> >>
> >> This is a bit cumbersome to use compared to our usual *_INIT macros. I'm
> >> struggling to see how we can make it nicer though as DEFINE_IVEC_TYPE
> >> cannot define a per-type initializer macro and I we cannot initialize
> >> the element size without knowing the type.
> >
> > I don't see what's cumbersome about it. Maybe an example use case
> > would clarify things.
>
> It is cumbersome because it separates the initialization from the
> declaration. Normally our *_INIT macros are initializer lists so we can
> write
>
>         struct strbuf = STRBUF_INIT;
>
> which keeps the declaration and initialization together. Although
> they're on adjacent lines in your example in real code the
> initialization likely to be separated from the declaration by other
> variable declarations.

Ah I see what you mean now. I'll experiment with making IVEC_INIT() work like that. One wrinkle is that STRBUF_INIT is a single concrete type whereas IVEC_INIT() is meant for generic types.

Previous: Phillip WoodNext: Phillip Wood
Message 25 of 124 in “Xdiff cleanup part 3”
  1. 00/10 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Jan 2, 2026
  2. 01/10 ivec: introduce the C side of ivecEzekiel Newren via GitGitGadget, Jan 2, 2026
  3. Junio C HamanoJan 4, 2026
  4. Ezekiel NewrenJan 17, 2026
  5. Phillip WoodJan 8, 2026
  6. Ezekiel NewrenJan 15, 2026
  7. Phillip WoodJan 16, 2026
  8. René ScharfeJan 16, 2026
  9. Phillip WoodJan 17, 2026
  10. Ezekiel NewrenJan 17, 2026
  11. René ScharfeJan 18, 2026
  12. Ezekiel NewrenJan 17, 2026
  13. Ezekiel NewrenJan 17, 2026
  14. Phillip WoodJan 17, 2026
  15. Jeff KingJan 19, 2026
  16. Ezekiel NewrenJan 19, 2026
  17. Jeff KingJan 19, 2026
  18. D. Ben KnobleJan 20, 2026
  19. Ezekiel NewrenJan 21, 2026
  20. Jeff KingJan 21, 2026
  21. Junio C HamanoJan 21, 2026
  22. Ezekiel NewrenJan 21, 2026
  23. Phillip WoodJan 20, 2026
  24. Phillip WoodJan 20, 2026
  25. Ezekiel NewrenJan 21, 2026
  26. Phillip WoodJan 28, 2026
  27. René ScharfeJan 16, 2026
  28. Ezekiel NewrenJan 17, 2026
  29. René ScharfeJan 18, 2026
  30. 02/10 xdiff: make classic diff explicit by creating xdl_do_classic_diff()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  31. Phillip WoodJan 20, 2026
  32. Ezekiel NewrenJan 21, 2026
  33. 03/10 xdiff: don't waste time guessing the number of linesEzekiel Newren via GitGitGadget, Jan 2, 2026
  34. Phillip WoodJan 20, 2026
  35. Ezekiel NewrenJan 21, 2026
  36. Phillip WoodJan 22, 2026
  37. 04/10 xdiff: let patience and histogram benefit from xdl_trim_ends()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  38. Phillip WoodJan 20, 2026
  39. Phillip WoodJan 21, 2026
  40. 05/10 xdiff: use xdfenv_t in xdl_trim_ends() and xdl_cleanup_records()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  41. Phillip WoodJan 20, 2026
  42. 06/10 xdiff: cleanup xdl_trim_ends()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  43. Phillip WoodJan 20, 2026
  44. 07/10 xdiff: replace xdfile_t.dstart with xdfenv_t.delta_startEzekiel Newren via GitGitGadget, Jan 2, 2026
  45. Phillip WoodJan 20, 2026
  46. Phillip WoodJan 28, 2026
  47. 08/10 xdiff: replace xdfile_t.dend with xdfenv_t.delta_endEzekiel Newren via GitGitGadget, Jan 2, 2026
  48. 09/10 xdiff: remove dependence on xdlclassifier from xdl_cleanup_records()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  49. René ScharfeJan 16, 2026
  50. Ezekiel NewrenJan 17, 2026
  51. René ScharfeJan 18, 2026
  52. Phillip WoodJan 21, 2026
  53. 10/10 xdiff: move xdl_cleanup_records() from xprepare.c to xdiffi.cEzekiel Newren via GitGitGadget, Jan 2, 2026
  54. Phillip WoodJan 21, 2026
  55. Phillip WoodJan 28, 2026
  56. Junio C HamanoJan 4, 2026
  57. Yee Cheng ChinJan 4, 2026
  58. Phillip WoodJan 28, 2026
  59. Junio C HamanoMar 6, 2026
  60. Ezekiel NewrenMar 9, 2026
  61. Junio C HamanoMar 9, 2026
  62. 0/5 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 25, 2026
  63. 1/5 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 25, 2026
  64. 2/5 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 25, 2026
  65. 3/5 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 25, 2026
  66. 4/5 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 25, 2026
  67. 5/5 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 25, 2026
  68. Junio C HamanoMar 25, 2026
  69. SZEDER GáborMar 26, 2026
  70. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 27, 2026
  71. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 27, 2026
  72. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Mar 27, 2026
  73. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 27, 2026
  74. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 27, 2026
  75. Junio C HamanoMar 27, 2026
  76. Junio C HamanoMar 27, 2026
  77. Ezekiel NewrenMar 30, 2026
  78. Junio C HamanoMar 30, 2026
  79. Ezekiel NewrenMar 31, 2026
  80. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 27, 2026
  81. 6/6 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 27, 2026
  82. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 30, 2026
  83. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 30, 2026
  84. Ezekiel NewrenMar 30, 2026
  85. Junio C HamanoMar 30, 2026
  86. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Mar 30, 2026
  87. Junio C HamanoMar 30, 2026
  88. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 30, 2026
  89. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 30, 2026
  90. Phillip WoodMar 31, 2026
  91. Junio C HamanoMar 31, 2026
  92. Ezekiel NewrenApr 14, 2026
  93. Junio C HamanoApr 14, 2026
  94. Phillip WoodApr 15, 2026
  95. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 30, 2026
  96. Junio C HamanoMar 30, 2026
  97. Phillip WoodMar 31, 2026
  98. 6/6 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 30, 2026
  99. Phillip WoodMar 31, 2026
  100. Phillip WoodApr 1, 2026
  101. Junio C HamanoMar 30, 2026
  102. Phillip WoodMar 31, 2026
  103. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Apr 8, 2026
  104. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Apr 8, 2026
  105. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Apr 8, 2026
  106. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Apr 8, 2026
  107. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Apr 8, 2026
  108. Phillip WoodApr 14, 2026
  109. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Apr 8, 2026
  110. 6/6 xdiff/xdl_cleanup_records: put braces around the else clauseEzekiel Newren via GitGitGadget, Apr 8, 2026
  111. Junio C HamanoApr 8, 2026
  112. Phillip WoodApr 9, 2026
  113. Phillip WoodApr 14, 2026
  114. Junio C HamanoApr 14, 2026
  115. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Apr 29, 2026
  116. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Apr 29, 2026
  117. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Apr 29, 2026
  118. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Apr 29, 2026
  119. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Apr 29, 2026
  120. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Apr 29, 2026
  121. 6/6 xdiff/xdl_cleanup_records: make execution of action easier to followEzekiel Newren via GitGitGadget, Apr 29, 2026
  122. Phillip WoodApr 30, 2026
  123. Ezekiel NewrenApr 30, 2026
  124. Junio C HamanoMay 4, 2026

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.