git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Derrick Stolee <derrickstolee@github.com>
Cc: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>,
	"Victoria Dye" <vdye@github.com>
Subject: Re: [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec()
Date: Mon, 06 Feb 2023 10:55:36 -0800	[thread overview]
Message-ID: <xmqq357i8vyv.fsf@gitster.g> (raw)
In-Reply-To: <b1a3e684-26f9-0053-9446-751d73e83961@github.com> (Derrick Stolee's message of "Mon, 6 Feb 2023 10:27:53 -0500")

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.

Thanks.

  parent reply	other threads:[~2023-02-06 18:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-04 19:10 [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec() René Scharfe
2023-02-05 21:12 ` Ævar Arnfjörð Bjarmason
2023-02-06 15:27   ` Derrick Stolee
2023-02-06 16:18     ` Ævar Arnfjörð Bjarmason
2023-02-12 11:20       ` René Scharfe
2023-02-06 18:55     ` Junio C Hamano [this message]
2023-02-10 20:20       ` René Scharfe
2023-02-10 20:20   ` René Scharfe
2023-02-10 20:33     ` Junio C Hamano
2023-02-11  2:15       ` Jeff King
2023-02-11  2:46         ` Junio C Hamano
2023-02-10 20:20 ` [PATCH v2] " René Scharfe
2023-02-13 13:37   ` Derrick Stolee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq357i8vyv.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=avarab@gmail.com \
    --cc=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=vdye@github.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).