Re: [PATCH v2] commit: remove padding from struct commit
- From
Kristofer Karlsson <krka@spotify.com>
- Date
- Nov 13, 2026, 15:41 UTC
- Message-ID
- <CAL71e4MZTKK3wmf4TM6DcZdc0-Bt91XaPTHy54rbYUmhNpWcbQ@mail.gmail.com>
- In-Reply-To
- <29490124-5020-42a1-978f-0f5283ba9e11@gmail.com>
On Fri, 13 Nov 2026 at 15:57, Derrick Stolee <stolee@gmail.com> wrote:
Show 16 quoted lines
>
> On 11/13/2026 8:30 AM, Kristofer Karlsson via GitGitGadget wrote:
> > From: Kristofer Karlsson <krka@spotify.com>
>
> > struct commit {
> > + /*
> > + * Must be the first member, so that a "struct object *" can be
> > + * cast to a "struct commit *".
> > + */
> > struct object object;
>
> I think your comment is backwards: We cast a commit into an object
> and it works. We can't cast an arbitrary object to a commit.
>
> But maybe that direction _does_ happen if we check the object type
> and see that it's actually a commit underneath.Hm, I suppose both directions do happen:
1. From object to commit requires knowing the type first to be
safe, and relies on "object" being the first member.
2. From commit to object could use &commit->object instead, which
is what most of the code does, and is why I only mentioned the
other direction.But looking more closely, there are also 24 plain casts to "struct object *", next to about 100 uses of &x->object (and about 110 casts the other way). The casts fall into two groups.
Some cast the result of a lookup function, e.g. in tag.c:
item->tagged = (struct object *)lookup_commit(r, &oid);
Here the cast is arguably the right tool, since lookup_commit() can return NULL and the cast passes that through, while taking &lookup_commit(r, &oid)->object would be undefined behaviour.
The rest cast a commit we already have, e.g. in bundle.c:
object = (struct object *)commit;
where &commit->object, as used in e.g. revision.c:
if (get_name_decoration(&commit->object))
would do the same without relying on the layout. I am not sure it is worth unifying these, but it might be worth settling on one style for new development.
> Perhaps instead of "taken from the commit object" you could say > "as specified by the commit object" because we may actually load > the date from the commit-graph file instead of parsing a commit > object.
Good point. "commit object" is also a bit ambiguous in a comment for "struct commit", so I could instead say "The committer date of this commit, without any adjustment.", which covers both cases.
Thanks! Kristofer