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

Re: [PATCH] commit: remove padding from struct commit

From
Kristofer Karlsson <krka@spotify.com>
Date
Nov 12, 2026, 21:35 UTC
Message-ID
<CAL71e4OS8MKZMY88C1SMs7ufSZ98-DNHNBc94cESdKpuXKbs7w@mail.gmail.com>
In-Reply-To
<xmqqpkw9j1ng.fsf@gitster.g>
On Thu, 12 Nov 2026 at 20:22, Junio C Hamano <gitster@pobox.com> wrote:
Show 18 quoted lines
>
> The comment before this structure says ...
>
> > diff --git a/commit.h b/commit.h
> > index 1061ed791b..66a5d245f2 100644
> > --- a/commit.h
> > +++ b/commit.h
> > @@ -26,6 +26,7 @@ struct commit_list {
> >   */
>
> /*
>  * The size of this struct matters in full repo walk operations like
>  * 'git clone' or 'git gc'. Consider using commit-slab to attach data
>  * to a commit instead of adding new fields here.
>  */
>
> but apparently it did not have much effect to prevent this from
> happening.

That does indeed appear to be true. I looked into the history of changes here for my related test prototype and found that the layout had changed several times. These are all measured from x86_64 builds of the respective commits, using gdb's "ptype /o":

                  size  padding
  2018              72        8
  2018 25f859fdf4   64        0   move "index" next to "graph_pos"
  2018 9d2c97016f   56        0   remove "util"
  2018 83073cc994   64        4   add "generation"
  2018 b45424181e   72        8   FLAG_BITS 27 -> 29
  2019 13eeedb5d1   80        4   SHA-256 grows object_id
  2020 c49c82aa4c   72        4   graph_pos/generation to a slab
  2020 23c4319f0d   72        8   FLAG_BITS 29 -> 28
  2021 cf0983213c   72        4   object_id gains "algo"
  2026 b4e8f60a3c   80        8   FLAG_BITS 28 -> 29
  now               72        0   this patch

While each individual change was good, I think it shows that at the very least, it's hard to reason about the total size. Adding a field may either reuse existing padding and thus come for free, or create additional padding and thus cost double.

Show 5 quoted lines
> I would think it is an effective way to force people to think struct
> size bloat of important and populous structure to always measure and
> warn.  There may be better ways to achieve the same that others may
> think of, but I very much appreciate your bringing it up, even only
> as a discussion starter.

I can send a test as a separate follow-up, so people can weigh in on the approach independently of this patch. One thing that couples them a bit is that the expected padding is more complex to express without this patch, since it depends on the alignment rules of each platform.

Another option I have tried is -Wpadded scoped to the struct via a pair of macros:

  BEGIN_NO_PADDING
  struct commit {
struct object object;
unsigned int index;
...
  };
  END_NO_PADDING

The macros would live in git-compat-util.h next to DISABLE_WARNING(), and expand to nothing on other compilers, which is fine, since we only need to catch it once in the CI builds.

This works with gcc and clang and is lighter than a unit test, but can only enforce zero padding, so it would not work for "struct object", which has unavoidable padding with the current FLAG_BITS. If "struct commit" ever ends up with padding we cannot avoid, we would have to drop the macros, but that would only lose the extra protection.

Show 5 quoted lines
> Obviously, ordering members based on size, not their logical
> relationships, has its downside.  The "object" being at offset 0 is
> an integral part of our in-core object design (fake inheritance in
> C), but the updated layout gives a false sense that somehow "index"
> may have a more important meaning than say "date" or "parents".

I agree that ordering fields for alignment rather than logical relationships makes the structure harder to read. Ideally the compiler could arrange them for us, if we could tell it that only "object" needs to come first.

It is also somewhat fiddly: growing "struct object" by 4 bytes can change the padding in "struct commit" without touching commit.h at all, which is what happened with b4e8f60a3c above.

Still, given how many instances of this structure we allocate, it may be worth optimizing the layout. The history above suggests that a comment alone may not be enough to catch accidental regressions.

Show 6 quoted lines
> We may want to have some comments next to "index" (which is a unique
> number given to all in-core commits while they are held in memory,
> to allow the commit slab subsystem to use it as an index into flat
> array of things) and "date" (which is the committer date taken from
> the underlying commit object, without any adjustment), which are two
> of these members whose meanings are not immediately obvious.

I can add short comments on "index" and "date" for a v2, along with a note that only "object" needs to come first.

Thanks for the review! Kristofer

Previous: Junio C HamanoNext: Oswald Buddenhagen
Message 5 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.