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

Re: [PATCH v2] strvec: `strvec_splice()` to a statically initialized vector

From
karthik nayak <karthik.188@gmail.com>
Date
Dec 6, 2024, 11:33 UTC
Message-ID
<CAOLa=ZSCyvd+LF_8ebkV5+O+y-jL90jd6aVb3HAytiW-1CiCbQ@mail.gmail.com>
In-Reply-To
<8ea38d79-9e1e-4d69-b734-273ebddbe384@gmail.com>
Rubén Justo <rjusto@gmail.com> writes:
[snip]
Show 45 quoted lines
>> > Thanks.
>> >
>> >  strvec.c              | 10 ++++++----
>> >  t/unit-tests/strvec.c | 10 ++++++++++
>> >  2 files changed, 16 insertions(+), 4 deletions(-)
>> >
>> > diff --git a/strvec.c b/strvec.c
>> > index d1cf4e2496..087c020f5b 100644
>> > --- a/strvec.c
>> > +++ b/strvec.c
>> > @@ -61,16 +61,18 @@ void strvec_splice(struct strvec *array, size_t idx, size_t len,
>> >  {
>> >  	if (idx + len > array->nr)
>> >  		BUG("range outside of array boundary");
>> > -	if (replacement_len > len)
>> > +	if (replacement_len > len) {
>> > +		if (array->v == empty_strvec)
>> > +			array->v = NULL;
>> >  		ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
>> >  			   array->alloc);
>> > +	}
>> >  	for (size_t i = 0; i < len; i++)
>> >  		free((char *)array->v[idx + i]);
>> > -	if (replacement_len != len) {
>> > +	if ((replacement_len != len) && array->nr)
>> >  		memmove(array->v + idx + replacement_len, array->v + idx + len,
>> >  			(array->nr - idx - len + 1) * sizeof(char *));
>> > -		array->nr += (replacement_len - len);
>> > -	}
>> > +	array->nr += replacement_len - len;
>>
>> Why is this second block of changes needed? Will array-nr ever be 0 when
>> we reach here?
>
> I'm not sure I understand your questions.
>
> At that point, `array->nr` is the initial number of entries in the
> vector.  It can be 0 when `strvec_splice()` is applied to an empty
> vector.
>
> We are moving the line where we update "array->nr" outside the `if`
> block because we want to do it even when we are not moving existing
> entries.  Again, this happens when `strvec_splice()` is applied to an
> empty vector.
>

Ah. I was considering that ALLOC_GROW would update `array->nr`, but it doesn't. So you're right.

Show 5 quoted lines
> Finally, we don't mind too much (or value more the simplicity) of the
> now unconditional update of "array->nr" because a clever compiler will
> give us the third arm of the if: "else -> do nothing".  When
> `replacement_len == len` => "array->nr += 0" => do nothing.
>
Indeed.
Thanks
Previous: Rubén JustoNext: Rubén Justo
Message 20 of 21 in “strvec: `strvec_splice()` to a statically initialized vector”
  1. strvec: `strvec_splice()` to a statically initialized vectorRubén Justo, Nov 29, 2024
  2. Junio C HamanoDec 2, 2024
  3. Rubén JustoDec 2, 2024
  4. Patrick SteinhardtDec 2, 2024
  5. strvec: `strvec_splice()` to a statically initialized vectorRubén Justo, Dec 3, 2024
  6. Junio C HamanoDec 4, 2024
  7. Rubén JustoDec 4, 2024
  8. Junio C HamanoDec 4, 2024
  9. Rubén JustoDec 4, 2024
  10. Rubén JustoDec 4, 2024
  11. Junio C HamanoDec 4, 2024
  12. Junio C HamanoDec 9, 2024
  13. Junio C HamanoDec 9, 2024
  14. Junio C HamanoDec 9, 2024
  15. Jeff KingDec 9, 2024
  16. Junio C HamanoDec 9, 2024
  17. Rubén JustoDec 9, 2024
  18. karthik nayakDec 4, 2024
  19. Rubén JustoDec 4, 2024
  20. karthik nayakDec 6, 2024
  21. strvec: `strvec_splice()` to a statically initialized vectorRubén Justo, Dec 4, 2024

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.