From: Jeff King <peff@peff.net>
To: "SZEDER Gábor" <szeder.dev@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [PATCH] commit-slab: clarify slabname##_peek()'s return value
Date: Tue, 10 Mar 2020 13:54:46 -0400 [thread overview]
Message-ID: <20200310175446.GB549010@coredump.intra.peff.net> (raw)
In-Reply-To: <20200310153049.3482-1-szeder.dev@gmail.com>
On Tue, Mar 10, 2020 at 04:30:49PM +0100, SZEDER Gábor wrote:
> Ever since 862e730ec1 (commit-slab: introduce slabname##_peek()
> function, 2015-05-14) the slabname##_peek() function is documented as:
>
> This function is similar to indegree_at(), but it will return NULL
> until a call to indegree_at() was made for the commit.
>
> This, however, is usually not the case. If indegree_at() allocates
> memory, then it will do so not only for the single commit it got as
> parameter, but it will allocate a whole new, ~512kB slab. Later on,
> if any other commit's 'index' field happens to point into an already
> allocated slab, then indegree_peek() for such a commit will return a
> valid non-NULL pointer, pointing to a zero-initialized location in the
> slab, even if no indegree_at() call has been made for that commit yet.
>
> Update slabname##_peek()'s documentation to clarify this.
Yeah, I agree the existing documentation is misleading. Your update
looks good to me.
I thought at first we might simply be able to say:
This function is similar to indegree_at(), but it will avoid
allocating new slab memory (so its result is suitable only for
reading, not writing).
But I think it's worth mentioning that the caller needs to handle both
NULL or a possible zero-initialized value, as your patch does.
I also wondered if we could make life easier for the caller by
collapsing these cases. I.e., always returning a zero-initialized value,
and never NULL. All of the callers do something like:
struct blame_origin *get_blame_suspects(struct commit *commit)
{
struct blame_origin **result;
result = blame_suspects_peek(&blame_suspects, commit);
return result ? *result : NULL;
}
all of which could be turned into a single blame_suspects_peek() call if
it just consistently returned a zero-initialized value (it's a little
confusing in this example because we're storing pointers, so the
zero-initialized value is _also_ NULL, but it's a different type).
But that would get a bit awkward, because peek() returns a pointer, not
a value (as it should, because the type we're storing may be a compound
type, which we generally avoid passing or returning by value). So we'd
actually need to return a pointer to a zero-initialized dummy value. Not
impossible, but getting a bit odd.
-Peff
next prev parent reply other threads:[~2020-03-10 17:54 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-10 15:30 [PATCH] commit-slab: clarify slabname##_peek()'s return value SZEDER Gábor
2020-03-10 17:54 ` Jeff King [this message]
2020-03-10 18:07 ` Jeff King
2020-03-11 16:07 ` Jeff King
2020-03-10 18:19 ` Junio C Hamano
2020-03-10 18:26 ` Junio C Hamano
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=20200310175446.GB549010@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=szeder.dev@gmail.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).