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

Re: [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec()

From
René Scharfe <l.s.r@web.de>
Date
Feb 10, 2023, 20:20 UTC
Message-ID
<e6785722-da0d-4b7c-fcd2-06e76b1f1757@web.de>
In-Reply-To
<xmqq357i8vyv.fsf@gitster.g>
Am 06.02.23 um 19:55 schrieb Junio C Hamano:
Show 35 quoted lines
> Derrick Stolee <derrickstolee@github.com> writes:
>
>>>> -	int base_path_len = tree_path->len;
>>>> +	size_t base_path_len = tree_path->len;
>>>> ...
>>>>  				strbuf_setlen(tree_path, base_path_len);
>>>> -				strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
>>>> +				strbuf_grow(tree_path, entry.pathlen + 1);
>>>>  				strbuf_add(tree_path, entry.path, entry.pathlen);
>>>> ...
>>>
>>> The size_t conversion is trivially correct.
>>
>> I agree, and thanks for finding and fixing this issue.
>> ...
>>> One wonders if (even for this index-related code) we really need such
>>> careful management of growth, and could instead do with:
>>>
>>> 	strbuf_setlen(tree_path, base_path_len);
>>> 	strbuf_add(tree_path, entry.path, entry.pathlen);
>>> 	strbuf_addch(tree_path, '/');
>>
>> This would be my preferred way to go here.
>
> Yup.  _setlen() is still very useful to truncate an existing
> contents in a strbuf, but we should look at each use of _grow() with
> suspicion that it might be an mistaken attempt to "optimize" where
> there is not a room for optimization.
>
> There may be cases where tight and exact allocation is desirable but
> this is not such a codepath.  tree_path will not be overly long that
> we want to avoid extra allocation for giving us slack to prepare for
> reallocation.  And tree_path is passed to recusive calls to further
> be grown, which is exactly why we would want to use ALLOW_GROW() kind
> of allocation with slack to amortize the allocation cost.
Right you are.
René
Previous: Junio C HamanoNext: René Scharfe
Message 7 of 13 in “cache-tree: fix strbuf growth in prime_cache_tree_rec()”
  1. cache-tree: fix strbuf growth in prime_cache_tree_rec()René Scharfe, Feb 4, 2023
  2. Ævar Arnfjörð BjarmasonFeb 5, 2023
  3. Derrick StoleeFeb 6, 2023
  4. Ævar Arnfjörð BjarmasonFeb 6, 2023
  5. René ScharfeFeb 12, 2023
  6. Junio C HamanoFeb 6, 2023
  7. René ScharfeFeb 10, 2023
  8. René ScharfeFeb 10, 2023
  9. Junio C HamanoFeb 10, 2023
  10. Jeff KingFeb 11, 2023
  11. Junio C HamanoFeb 11, 2023
  12. cache-tree: fix strbuf growth in prime_cache_tree_rec()René Scharfe, Feb 10, 2023
  13. Derrick StoleeFeb 13, 2023

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.