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

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

Previous: Derrick StoleeNext: Junio C Hamano
Message 9 of 12 in “commit: remove padding from struct commit”
  1. commit: remove padding from struct commitKristofer Karlsson via GitGitGadget, Nov 12, 2026
  2. Derrick StoleeNov 12, 2026
  3. Kristofer KarlssonNov 12, 2026
  4. Junio C HamanoNov 12, 2026
  5. Kristofer KarlssonNov 12, 2026
  6. Oswald BuddenhagenNov 13, 2026
  7. commit: remove padding from struct commitKristofer Karlsson via GitGitGadget, Nov 13, 2026
  8. Derrick StoleeNov 13, 2026
  9. Kristofer KarlssonNov 13, 2026
  10. Junio C HamanoNov 13, 2026
  11. Junio C HamanoNov 13, 2026
  12. commit: remove padding from struct commitKristofer Karlsson via GitGitGadget, Nov 15, 2026

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.