{"thread":{"id":"66837","subject":"[PATCH] commit: remove padding from struct commit","startedAt":"2026-11-12T16:14:04Z","lastAt":"2026-11-15T12:47:42Z","messageCount":12,"participants":["Kristofer Karlsson via GitGitGadget","Derrick Stolee","Junio C Hamano","Kristofer Karlsson","Oswald Buddenhagen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558301","messageId":"pull.2309.git.1794500044740.gitgitgadget@gmail.com","threadId":"66837","inReplyTo":null,"subject":"[PATCH] commit: remove padding from struct commit","fromName":"Kristofer Karlsson via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-12T16:14:04Z","receivedAt":"2026-11-12T16:14:04Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"From: Kristofer Karlsson <krka@spotify.com>\n\nOn 64-bit platforms, \"struct commit\" is 80 bytes, 8 of which are\npadding:\n - 4 bytes between .object and .date\n - 4 bytes of tail padding after .index\n\nReduce memory usage by moving .index into the hole after .object.\nOn 64-bit platforms this shrinks the struct to 72 bytes and\neliminates the padding, resulting in 10% less memory for every commit\nallocated, with no change in behaviour.\n\nOn platforms where 8-byte members only need 4-byte alignment (e.g.\ni386) both layouts are already free of padding, so the size stays the\nsame there.\n\nMeasuring peak RSS of \"git rev-list --all\" over an internal monorepo\nwith ~4.5 million commits (three runs each):\n\n             peak RSS     time\n  before     ~849 MiB   12.8-13.7s\n  after      ~817 MiB   12.8-13.0s\n\nThe ~32 MiB saved is close to the expected 8 bytes * 4.5M commits\n(~34 MiB); running time is unchanged within noise.\n\nSigned-off-by: Kristofer Karlsson <krka@spotify.com>\n---\n    commit: remove padding from struct commit\n    \n    I stumbled on this while rabbit-holing on an unrelated change to the\n    commit walk, by looking at the layout with gdb's \"ptype /o\".\n    \n    Digging into the history showed that the size and padding of \"struct\n    commit\" have changed several times, sometimes undoing earlier\n    optimizations.\n    \n    I also prototyped a regression test that compares the size of \"struct\n    commit\" and \"struct object\" against the sum of their members' sizes,\n    plus any expected padding. I left it out of this patch because I am not\n    sure whether:\n    \n     1. such a test is worth the cost of updating it whenever members are\n        added or removed, or\n    \n     2. a unit test is the right place for it.\n    \n    I am happy to send it as a follow-up if there is interest.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2309%2Fspkrka%2Fcommit-index-hole-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2309/spkrka/commit-index-hole-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2309\n\n commit.h | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/commit.h b/commit.h\nindex 1061ed791b..66a5d245f2 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -26,6 +26,7 @@ struct commit_list {\n  */\n struct commit {\n \tstruct object object;\n+\tunsigned int index;\n \ttimestamp_t date;\n \tstruct commit_list *parents;\n \n@@ -35,7 +36,6 @@ struct commit {\n \t * or get_commit_tree_oid().\n \t */\n \tstruct tree *maybe_tree;\n-\tunsigned int index;\n };\n \n extern int save_commit_buffer;\n\nbase-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2\n-- \ngitgitgadget\n\n"},{"id":"558325","messageId":"60c1d61e-777d-4c5a-b5a1-10c6530aa0d5@gmail.com","threadId":"66837","inReplyTo":"pull.2309.git.1794500044740.gitgitgadget@gmail.com","subject":"Re: [PATCH] commit: remove padding from struct commit","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2026-11-12T18:00:18Z","receivedAt":"2026-11-12T18:00:18Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 11/12/2026 11:14 AM, Kristofer Karlsson via GitGitGadget wrote:\n> From: Kristofer Karlsson <krka@spotify.com>\n> \n> On 64-bit platforms, \"struct commit\" is 80 bytes, 8 of which are\n> padding:\n>  - 4 bytes between .object and .date\n>  - 4 bytes of tail padding after .index\n\nGood find.\n\n> Reduce memory usage by moving .index into the hole after .object.\n> On 64-bit platforms this shrinks the struct to 72 bytes and\n> eliminates the padding, resulting in 10% less memory for every commit\n> allocated, with no change in behaviour.\n> \n> On platforms where 8-byte members only need 4-byte alignment (e.g.\n> i386) both layouts are already free of padding, so the size stays the\n> same there.\n> \n> Measuring peak RSS of \"git rev-list --all\" over an internal monorepo\n> with ~4.5 million commits (three runs each):\n> \n>              peak RSS     time\n>   before     ~849 MiB   12.8-13.7s\n>   after      ~817 MiB   12.8-13.0s\n> \n> The ~32 MiB saved is close to the expected 8 bytes * 4.5M commits\n> (~34 MiB); running time is unchanged within noise.\n\nThanks for measuring the memory load improvement. And of course this\nwon't have any impact on 32-bit machines as the struct will use\n4-byte alignment by default.\n\n>  struct commit {\n>  \tstruct object object;\n> +\tunsigned int index;\n>  \ttimestamp_t date;\n>  \tstruct commit_list *parents;\n>  \n> @@ -35,7 +36,6 @@ struct commit {\n>  \t * or get_commit_tree_oid().\n>  \t */\n>  \tstruct tree *maybe_tree;\n> -\tunsigned int index;\n>  };\nThis certainly looks like it should be obviously correct. I\nbelieve that the only order-critical part is the object\nstruct needing to be first so we can \"cast\" a commit to an\nobject.\n\nThanks,\n-Stolee\n\n\n"},{"id":"558329","messageId":"xmqqpkw9j1ng.fsf@gitster.g","threadId":"66837","inReplyTo":"pull.2309.git.1794500044740.gitgitgadget@gmail.com","subject":"Re: [PATCH] commit: remove padding from struct commit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-12T19:22:11Z","receivedAt":"2026-11-12T19:22:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Kristofer Karlsson via GitGitGadget\" <gitgitgadget@gmail.com>\nwrites:\n\n> From: Kristofer Karlsson <krka@spotify.com>\n>\n> On 64-bit platforms, \"struct commit\" is 80 bytes, 8 of which are\n> padding:\n>  - 4 bytes between .object and .date\n>  - 4 bytes of tail padding after .index\n> ...\n> Measuring peak RSS of \"git rev-list --all\" over an internal monorepo\n> with ~4.5 million commits (three runs each):\n>\n>              peak RSS     time\n>   before     ~849 MiB   12.8-13.7s\n>   after      ~817 MiB   12.8-13.0s\n>\n> The ~32 MiB saved is close to the expected 8 bytes * 4.5M commits\n> (~34 MiB); running time is unchanged within noise.\n>\n> Signed-off-by: Kristofer Karlsson <krka@spotify.com>\n> ---\n>     I also prototyped a regression test that compares the size of \"struct\n>     commit\" and \"struct object\" against the sum of their members' sizes,\n>     plus any expected padding. I left it out of this patch because I am not\n>     sure whether:\n>     \n>      1. such a test is worth the cost of updating it whenever members are\n>         added or removed, or\n>     \n>      2. a unit test is the right place for it.\n>     \n>     I am happy to send it as a follow-up if there is interest.\n\nThe comment before this structure says ...\n\n> diff --git a/commit.h b/commit.h\n> index 1061ed791b..66a5d245f2 100644\n> --- a/commit.h\n> +++ b/commit.h\n> @@ -26,6 +26,7 @@ struct commit_list {\n>   */\n\n/*\n * The size of this struct matters in full repo walk operations like\n * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n * to a commit instead of adding new fields here.\n */\n\nbut apparently it did not have much effect to prevent this from\nhappening.\n\nI would think it is an effective way to force people to think struct\nsize bloat of important and populous structure to always measure and\nwarn.  There may be better ways to achieve the same that others may\nthink of, but I very much appreciate your bringing it up, even only\nas a discussion starter.\n\n>  struct commit {\n>  \tstruct object object;\n> +\tunsigned int index;\n>  \ttimestamp_t date;\n>  \tstruct commit_list *parents;\n>  \n> @@ -35,7 +36,6 @@ struct commit {\n>  \t * or get_commit_tree_oid().\n>  \t */\n>  \tstruct tree *maybe_tree;\n> -\tunsigned int index;\n>  };\n\nObviously, ordering members based on size, not their logical\nrelationships, has its downside.  The \"object\" being at offset 0 is\nan integral part of our in-core object design (fake inheritance in\nC), but the updated layout gives a false sense that somehow \"index\"\nmay have a more important meaning than say \"date\" or \"parents\".\n\nWe may want to have some comments next to \"index\" (which is a unique\nnumber given to all in-core commits while they are held in memory,\nto allow the commit slab subsystem to use it as an index into flat\narray of things) and \"date\" (which is the committer date taken from\nthe underlying commit object, without any adjustment), which are two\nof these members whose meanings are not immediately obvious.\n\n"},{"id":"558347","messageId":"CAL71e4OS8MKZMY88C1SMs7ufSZ98-DNHNBc94cESdKpuXKbs7w@mail.gmail.com","threadId":"66837","inReplyTo":"xmqqpkw9j1ng.fsf@gitster.g","subject":"Re: [PATCH] commit: remove padding from struct commit","fromName":"Kristofer Karlsson","fromEmail":"krka@spotify.com","sentAt":"2026-11-12T21:35:42Z","receivedAt":"2026-11-12T21:35:42Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"On Thu, 12 Nov 2026 at 20:22, Junio C Hamano <gitster@pobox.com> wrote:\n>\n> The comment before this structure says ...\n>\n> > diff --git a/commit.h b/commit.h\n> > index 1061ed791b..66a5d245f2 100644\n> > --- a/commit.h\n> > +++ b/commit.h\n> > @@ -26,6 +26,7 @@ struct commit_list {\n> >   */\n>\n> /*\n>  * The size of this struct matters in full repo walk operations like\n>  * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n>  * to a commit instead of adding new fields here.\n>  */\n>\n> but apparently it did not have much effect to prevent this from\n> happening.\n\nThat does indeed appear to be true.  I looked into the history of\nchanges here for my related test prototype and found that the\nlayout had changed several times.  These are all measured from\nx86_64 builds of the respective commits, using gdb's \"ptype /o\":\n\n                  size  padding\n  2018              72        8\n  2018 25f859fdf4   64        0   move \"index\" next to \"graph_pos\"\n  2018 9d2c97016f   56        0   remove \"util\"\n  2018 83073cc994   64        4   add \"generation\"\n  2018 b45424181e   72        8   FLAG_BITS 27 -> 29\n  2019 13eeedb5d1   80        4   SHA-256 grows object_id\n  2020 c49c82aa4c   72        4   graph_pos/generation to a slab\n  2020 23c4319f0d   72        8   FLAG_BITS 29 -> 28\n  2021 cf0983213c   72        4   object_id gains \"algo\"\n  2026 b4e8f60a3c   80        8   FLAG_BITS 28 -> 29\n  now               72        0   this patch\n\nWhile each individual change was good, I think it shows that at the\nvery least, it's hard to reason about the total size.  Adding a field\nmay either reuse existing padding and thus come for free, or create\nadditional padding and thus cost double.\n\n> I would think it is an effective way to force people to think struct\n> size bloat of important and populous structure to always measure and\n> warn.  There may be better ways to achieve the same that others may\n> think of, but I very much appreciate your bringing it up, even only\n> as a discussion starter.\n\nI can send a test as a separate follow-up, so people can weigh in\non the approach independently of this patch.  One thing that couples\nthem a bit is that the expected padding is more complex to express\nwithout this patch, since it depends on the alignment rules of each\nplatform.\n\nAnother option I have tried is -Wpadded scoped to the struct via\na pair of macros:\n\n  BEGIN_NO_PADDING\n  struct commit {\nstruct object object;\nunsigned int index;\n...\n  };\n  END_NO_PADDING\n\nThe macros would live in git-compat-util.h next to\nDISABLE_WARNING(), and expand to nothing on other compilers, which\nis fine, since we only need to catch it once in the CI builds.\n\nThis works with gcc and clang and is lighter than a unit test, but\ncan only enforce zero padding, so it would not work for \"struct\nobject\", which has unavoidable padding with the current FLAG_BITS.\nIf \"struct commit\" ever ends up with padding we cannot avoid, we\nwould have to drop the macros, but that would only lose the extra\nprotection.\n\n> Obviously, ordering members based on size, not their logical\n> relationships, has its downside.  The \"object\" being at offset 0 is\n> an integral part of our in-core object design (fake inheritance in\n> C), but the updated layout gives a false sense that somehow \"index\"\n> may have a more important meaning than say \"date\" or \"parents\".\n\nI agree that ordering fields for alignment rather than logical\nrelationships makes the structure harder to read.  Ideally the\ncompiler could arrange them for us, if we could tell it that only\n\"object\" needs to come first.\n\nIt is also somewhat fiddly: growing \"struct object\" by 4 bytes can\nchange the padding in \"struct commit\" without touching commit.h at\nall, which is what happened with b4e8f60a3c above.\n\nStill, given how many instances of this structure we allocate, it\nmay be worth optimizing the layout.  The history above suggests that\na comment alone may not be enough to catch accidental regressions.\n\n> We may want to have some comments next to \"index\" (which is a unique\n> number given to all in-core commits while they are held in memory,\n> to allow the commit slab subsystem to use it as an index into flat\n> array of things) and \"date\" (which is the committer date taken from\n> the underlying commit object, without any adjustment), which are two\n> of these members whose meanings are not immediately obvious.\n\nI can add short comments on \"index\" and \"date\" for a v2, along\nwith a note that only \"object\" needs to come first.\n\nThanks for the review!\nKristofer\n\n"},{"id":"558348","messageId":"CAL71e4MP86eaiMrP_eignRSL_Zc58qX-hXAc=GD=w9st71DN4w@mail.gmail.com","threadId":"66837","inReplyTo":"60c1d61e-777d-4c5a-b5a1-10c6530aa0d5@gmail.com","subject":"Re: [PATCH] commit: remove padding from struct commit","fromName":"Kristofer Karlsson","fromEmail":"krka@spotify.com","sentAt":"2026-11-12T21:46:21Z","receivedAt":"2026-11-12T21:46:21Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"On Thu, 12 Nov 2026 at 19:00, Derrick Stolee <stolee@gmail.com> wrote:\n>\n> >  struct commit {\n> >       struct object object;\n> > +     unsigned int index;\n> >       timestamp_t date;\n> >       struct commit_list *parents;\n> >\n> > @@ -35,7 +36,6 @@ struct commit {\n> >        * or get_commit_tree_oid().\n> >        */\n> >       struct tree *maybe_tree;\n> > -     unsigned int index;\n> >  };\n> This certainly looks like it should be obviously correct. I\n> believe that the only order-critical part is the object\n> struct needing to be first so we can \"cast\" a commit to an\n> object.\n\nAgreed, and it is an important invariant, since nothing enforces\nit at compile time.  Out of curiosity I tried moving \"object\" to\nthe end of the struct: it builds without any warnings, and then\nfails in a variety of ways at runtime:\n\n  git cat-file -t HEAD     works (does not use struct commit)\n  git log                  fatal: HEAD is unknown object\n  git log HEAD~5           error: object ... is a (null), not a commit\n  git commit               segfault\n\nIf we want the compiler to enforce it as well, something like\n\n  (void)BUILD_ASSERT_OR_ZERO(offsetof(struct commit, object) == 0);\n\nin init_commit_node() could work, and the same could be done for\ntrees, blobs and tags.\n\nI added some more detailed followup in the parallel reply to Junio.\n\nThanks for the review!\nKristofer\n\n"},{"id":"558388","messageId":"pull.2309.v2.git.1794576606249.gitgitgadget@gmail.com","threadId":"66837","inReplyTo":"pull.2309.git.1794500044740.gitgitgadget@gmail.com","subject":"[PATCH v2] commit: remove padding from struct commit","fromName":"Kristofer Karlsson via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-13T13:30:06Z","receivedAt":"2026-11-13T13:30:06Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"From: Kristofer Karlsson <krka@spotify.com>\n\nOn 64-bit platforms, \"struct commit\" is 80 bytes, 8 of which are\npadding:\n - 4 bytes between .object and .date\n - 4 bytes of tail padding after .index\n\nReduce memory usage by moving .index into the hole after .object.\nOn 64-bit platforms this shrinks the struct to 72 bytes and\neliminates the padding, resulting in 10% less memory for every commit\nallocated, with no change in behaviour.\n\nOn platforms where 8-byte members only need 4-byte alignment (e.g.\ni386) both layouts are already free of padding, so the size stays the\nsame there.\n\nSince the order of the members no longer reflects their importance,\ndocument that only \"object\" has to come first, and describe \"index\"\nand \"date\", whose meanings are not obvious from their names.\n\nMeasuring peak RSS of \"git rev-list --all\" over an internal monorepo\nwith ~4.5 million commits (three runs each):\n\n             peak RSS     time\n  before     ~849 MiB   12.8-13.7s\n  after      ~817 MiB   12.8-13.0s\n\nThe ~32 MiB saved is close to the expected 8 bytes * 4.5M commits\n(~34 MiB); running time is unchanged within noise.\n\nSigned-off-by: Kristofer Karlsson <krka@spotify.com>\n---\n    commit: remove padding from struct commit\n    \n    I stumbled on this while rabbit-holing on an unrelated change to the\n    commit walk, by looking at the layout with gdb's \"ptype /o\".\n    \n    Changes since v1:\n    \n     * Document that \"object\" must be the first member, and that the order\n       of the other members is partly chosen for a compact memory layout\n       rather than reflecting their importance (Junio).\n    \n     * Add comments describing \"index\" and \"date\" (Junio).\n    \n    A regression check for the padding (unit test or a scoped -Wpadded) and\n    a build-time assertion that \"object\" comes first are discussed in the v1\n    thread; I will send those separately.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2309%2Fspkrka%2Fcommit-index-hole-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2309/spkrka/commit-index-hole-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/2309\n\nRange-diff vs v1:\n\n 1:  20de8db00d ! 1:  f8f0dbcf46 commit: remove padding from struct commit\n     @@ Commit message\n          i386) both layouts are already free of padding, so the size stays the\n          same there.\n      \n     +    Since the order of the members no longer reflects their importance,\n     +    document that only \"object\" has to come first, and describe \"index\"\n     +    and \"date\", whose meanings are not obvious from their names.\n     +\n          Measuring peak RSS of \"git rev-list --all\" over an internal monorepo\n          with ~4.5 million commits (three runs each):\n      \n     @@ Commit message\n      \n       ## commit.h ##\n      @@ commit.h: struct commit_list {\n     +  * The size of this struct matters in full repo walk operations like\n     +  * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n     +  * to a commit instead of adding new fields here.\n     ++ *\n     ++ * The order of the members is partly chosen to keep the memory layout\n     ++ * compact (avoiding alignment padding), and does not necessarily\n     ++ * reflect their importance or grouping.\n        */\n       struct commit {\n     ++\t/*\n     ++\t * Must be the first member, so that a \"struct object *\" can be\n     ++\t * cast to a \"struct commit *\".\n     ++\t */\n       \tstruct object object;\n     ++\n     ++\t/*\n     ++\t * Index into the commit-slab arrays: unique per in-core commit,\n     ++\t * and only meaningful within the current process.\n     ++\t */\n      +\tunsigned int index;\n     ++\n     ++\t/*\n     ++\t * The committer date taken from the commit object, without any\n     ++\t * adjustment.\n     ++\t */\n       \ttimestamp_t date;\n     ++\n       \tstruct commit_list *parents;\n       \n     + \t/*\n      @@ commit.h: struct commit {\n       \t * or get_commit_tree_oid().\n       \t */\n\n\n commit.h | 21 ++++++++++++++++++++-\n 1 file changed, 20 insertions(+), 1 deletion(-)\n\ndiff --git a/commit.h b/commit.h\nindex 1061ed791b..d43cb1e46d 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -23,10 +23,30 @@ struct commit_list {\n  * The size of this struct matters in full repo walk operations like\n  * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n  * to a commit instead of adding new fields here.\n+ *\n+ * The order of the members is partly chosen to keep the memory layout\n+ * compact (avoiding alignment padding), and does not necessarily\n+ * reflect their importance or grouping.\n  */\n struct commit {\n+\t/*\n+\t * Must be the first member, so that a \"struct object *\" can be\n+\t * cast to a \"struct commit *\".\n+\t */\n \tstruct object object;\n+\n+\t/*\n+\t * Index into the commit-slab arrays: unique per in-core commit,\n+\t * and only meaningful within the current process.\n+\t */\n+\tunsigned int index;\n+\n+\t/*\n+\t * The committer date taken from the commit object, without any\n+\t * adjustment.\n+\t */\n \ttimestamp_t date;\n+\n \tstruct commit_list *parents;\n \n \t/*\n@@ -35,7 +55,6 @@ struct commit {\n \t * or get_commit_tree_oid().\n \t */\n \tstruct tree *maybe_tree;\n-\tunsigned int index;\n };\n \n extern int save_commit_buffer;\n\nbase-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2\n-- \ngitgitgadget\n\n"},{"id":"558402","messageId":"29490124-5020-42a1-978f-0f5283ba9e11@gmail.com","threadId":"66837","inReplyTo":"pull.2309.v2.git.1794576606249.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] commit: remove padding from struct commit","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2026-11-13T14:57:29Z","receivedAt":"2026-11-13T14:57:29Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 11/13/2026 8:30 AM, Kristofer Karlsson via GitGitGadget wrote:\n> From: Kristofer Karlsson <krka@spotify.com>\n\n>  struct commit {\n> +\t/*\n> +\t * Must be the first member, so that a \"struct object *\" can be\n> +\t * cast to a \"struct commit *\".\n> +\t */\n>  \tstruct object object;\n\nI think your comment is backwards: We cast a commit into an object\nand it works. We can't cast an arbitrary object to a commit.\n\nBut maybe that direction _does_ happen if we check the object type\nand see that it's actually a commit underneath.\n\n> +\n> +\t/*\n> +\t * Index into the commit-slab arrays: unique per in-core commit,\n> +\t * and only meaningful within the current process.\n> +\t */\n> +\tunsigned int index;\n> +\n> +\t/*\n> +\t * The committer date taken from the commit object, without any\n> +\t * adjustment.\n> +\t */\n>  \ttimestamp_t date;\n> +\n\nPerhaps instead of \"taken from the commit object\" you could say\n\"as specified by the commit object\" because we may actually load\nthe date from the commit-graph file instead of parsing a commit\nobject.\n\nMy comments are only nitpicks that _might_ improve the comments.\nBut they might not, so do what you will.\n\nThanks,\n-Stolee\n\n\n"},{"id":"558403","messageId":"avctUapIbXrL8q_e@ugly.lan","threadId":"66837","inReplyTo":"CAL71e4OS8MKZMY88C1SMs7ufSZ98-DNHNBc94cESdKpuXKbs7w@mail.gmail.com","subject":"Re: [PATCH] commit: remove padding from struct commit","fromName":"Oswald Buddenhagen","fromEmail":"oswald.buddenhagen@gmx.de","sentAt":"2026-11-13T15:31:29Z","receivedAt":"2026-11-13T15:31:29Z","isPatch":true,"sender":{"key":"oswald.buddenhagen@gmx.de","avatar":"https://avatars.githubusercontent.com/u/812380?v=4"},"body":"On Thu, Nov 12, 2026 at 10:35:42PM +0100, Kristofer Karlsson wrote:\n>If \"struct commit\" ever ends up with padding we cannot avoid,\n\nthis is a non-concern - one can always introduce (arch-dependent) dummy members to make the compiler happy. not beautiful, but justifiable.\n\n\n"},{"id":"558404","messageId":"CAL71e4MZTKK3wmf4TM6DcZdc0-Bt91XaPTHy54rbYUmhNpWcbQ@mail.gmail.com","threadId":"66837","inReplyTo":"29490124-5020-42a1-978f-0f5283ba9e11@gmail.com","subject":"Re: [PATCH v2] commit: remove padding from struct commit","fromName":"Kristofer Karlsson","fromEmail":"krka@spotify.com","sentAt":"2026-11-13T15:41:41Z","receivedAt":"2026-11-13T15:41:41Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"On Fri, 13 Nov 2026 at 15:57, Derrick Stolee <stolee@gmail.com> wrote:\n>\n> On 11/13/2026 8:30 AM, Kristofer Karlsson via GitGitGadget wrote:\n> > From: Kristofer Karlsson <krka@spotify.com>\n>\n> >  struct commit {\n> > +     /*\n> > +      * Must be the first member, so that a \"struct object *\" can be\n> > +      * cast to a \"struct commit *\".\n> > +      */\n> >       struct object object;\n>\n> I think your comment is backwards: We cast a commit into an object\n> and it works. We can't cast an arbitrary object to a commit.\n>\n> But maybe that direction _does_ happen if we check the object type\n> and see that it's actually a commit underneath.\n\nHm, I suppose both directions do happen:\n\n  1. From object to commit requires knowing the type first to be\n     safe, and relies on \"object\" being the first member.\n  2. From commit to object could use &commit->object instead, which\n     is what most of the code does, and is why I only mentioned the\n     other direction.\n\nBut looking more closely, there are also 24 plain casts to \"struct\nobject *\", next to about 100 uses of &x->object (and about 110\ncasts the other way).  The casts fall into two groups.\n\nSome cast the result of a lookup function, e.g. in tag.c:\n\n    item->tagged = (struct object *)lookup_commit(r, &oid);\n\nHere the cast is arguably the right tool, since lookup_commit() can\nreturn NULL and the cast passes that through, while taking\n&lookup_commit(r, &oid)->object would be undefined behaviour.\n\nThe rest cast a commit we already have, e.g. in bundle.c:\n\n    object = (struct object *)commit;\n\nwhere &commit->object, as used in e.g. revision.c:\n\n    if (get_name_decoration(&commit->object))\n\nwould do the same without relying on the layout.  I am not sure it\nis worth unifying these, but it might be worth settling on one style\nfor new development.\n\n> Perhaps instead of \"taken from the commit object\" you could say\n> \"as specified by the commit object\" because we may actually load\n> the date from the commit-graph file instead of parsing a commit\n> object.\n\nGood point.  \"commit object\" is also a bit ambiguous in a comment\nfor \"struct commit\", so I could instead say \"The committer date\nof this commit, without any adjustment.\", which covers both cases.\n\nThanks!\nKristofer\n\n"},{"id":"558406","messageId":"xmqqbj7sfzbz.fsf@gitster.g","threadId":"66837","inReplyTo":"CAL71e4MZTKK3wmf4TM6DcZdc0-Bt91XaPTHy54rbYUmhNpWcbQ@mail.gmail.com","subject":"Re: [PATCH v2] commit: remove padding from struct commit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-13T16:52:48Z","receivedAt":"2026-11-13T16:52:48Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kristofer Karlsson <krka@spotify.com> writes:\n\n> On Fri, 13 Nov 2026 at 15:57, Derrick Stolee <stolee@gmail.com> wrote:\n>>\n>> On 11/13/2026 8:30 AM, Kristofer Karlsson via GitGitGadget wrote:\n>> > From: Kristofer Karlsson <krka@spotify.com>\n>>\n>> >  struct commit {\n>> > +     /*\n>> > +      * Must be the first member, so that a \"struct object *\" can be\n>> > +      * cast to a \"struct commit *\".\n>> > +      */\n>> >       struct object object;\n>>\n>> I think your comment is backwards: We cast a commit into an object\n>> and it works. We can't cast an arbitrary object to a commit.\n\nI had the exact reaction.  In our \"cheap C inheritance\" pattern\ndowncasting commit to object can be done unconditionally, but\nupcasting needs to be done with out of band knowledge (because these\nstruct types do not have \"type\" signature).  So\n\n    ... so that a \"struct commit *\" can be cast to a \"struct object\n    *\".  The code casts in other direction only when the object is\n    known to be a commit.\n\n"},{"id":"558408","messageId":"xmqq7bigfydc.fsf@gitster.g","threadId":"66837","inReplyTo":"pull.2309.v2.git.1794576606249.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] commit: remove padding from struct commit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-13T17:13:35Z","receivedAt":"2026-11-13T17:13:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Kristofer Karlsson via GitGitGadget\" <gitgitgadget@gmail.com>\nwrites:\n\n> Since the order of the members no longer reflects their importance,\n\nMicronit: \"no longer reflects logical grouping or impotance\" or\nsomething, just like you already have in the comment in the header\nfile.\n\n> diff --git a/commit.h b/commit.h\n> index 1061ed791b..d43cb1e46d 100644\n> --- a/commit.h\n> +++ b/commit.h\n> @@ -23,10 +23,30 @@ struct commit_list {\n>   * The size of this struct matters in full repo walk operations like\n>   * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n>   * to a commit instead of adding new fields here.\n> + *\n> + * The order of the members is partly chosen to keep the memory layout\n> + * compact (avoiding alignment padding), and does not necessarily\n> + * reflect their importance or grouping.\n>   */\n>  struct commit {\n> +\t/*\n> +\t * Must be the first member, so that a \"struct object *\" can be\n> +\t * cast to a \"struct commit *\".\n> +\t */\n\nEither append \"... when we know it is a commit\", swap the order, or\ndo both.  My preference is the second one which would leave the\nshortest result.\n\n>  \tstruct object object;\n> +\n> +\t/*\n> +\t * Index into the commit-slab arrays: unique per in-core commit,\n> +\t * and only meaningful within the current process.\n> +\t */\n> +\tunsigned int index;\n\nNice.  It is on a rather longer side and \"and only meaningful...\"\nmay be redundant after saying \"in-core\", but nothing is wrong.\n\nThanks.  Will queue.\n\n"},{"id":"558501","messageId":"pull.2309.v3.git.1794746862501.gitgitgadget@gmail.com","threadId":"66837","inReplyTo":"pull.2309.git.1794500044740.gitgitgadget@gmail.com","subject":"[PATCH v3] commit: remove padding from struct commit","fromName":"Kristofer Karlsson via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-15T12:47:42Z","receivedAt":"2026-11-15T12:47:42Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"From: Kristofer Karlsson <krka@spotify.com>\n\nOn 64-bit platforms, \"struct commit\" is 80 bytes, 8 of which are\npadding:\n - 4 bytes between .object and .date\n - 4 bytes of tail padding after .index\n\nReduce memory usage by moving .index into the hole after .object.\nOn 64-bit platforms this shrinks the struct to 72 bytes and\neliminates the padding, resulting in 10% less memory for every commit\nallocated, with no change in behaviour.\n\nOn platforms where 8-byte members only need 4-byte alignment (e.g.\ni386) both layouts are already free of padding, so the size stays the\nsame there.\n\nSince the order of the members no longer reflects their logical\ngrouping or importance, document that only \"object\" has to come\nfirst, and describe \"index\" and \"date\", whose meanings are not\nobvious from their names.\n\nMeasuring peak RSS of \"git rev-list --all\" over an internal monorepo\nwith ~4.5 million commits (three runs each):\n\n             peak RSS     time\n  before     ~849 MiB   12.8-13.7s\n  after      ~817 MiB   12.8-13.0s\n\nThe ~32 MiB saved is close to the expected 8 bytes * 4.5M commits\n(~34 MiB); running time is unchanged within noise.\n\nSigned-off-by: Kristofer Karlsson <krka@spotify.com>\n---\n    commit: remove padding from struct commit\n    \n    I stumbled on this while rabbit-holing on an unrelated change to the\n    commit walk, by looking at the layout with gdb's \"ptype /o\".\n    \n    Changes since v2:\n    \n     * Swap the direction in the comment on \"object\": a \"struct commit *\"\n       can be cast to a \"struct object *\" (Junio, Stolee).\n    \n     * Say \"committer date of this commit\" instead of \"taken from the commit\n       object\", since it may also come from the commit-graph (Stolee).\n    \n     * Shorten the comment on \"index\" (Junio).\n    \n     * Mention logical grouping in the commit message as well (Junio).\n    \n    Changes since v1:\n    \n     * Document that \"object\" must be the first member, and that the order\n       of the other members is partly chosen for a compact memory layout\n       rather than reflecting their importance (Junio).\n    \n     * Add comments describing \"index\" and \"date\" (Junio).\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2309%2Fspkrka%2Fcommit-index-hole-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2309/spkrka/commit-index-hole-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/2309\n\nRange-diff vs v2:\n\n 1:  f8f0dbcf46 ! 1:  a579563389 commit: remove padding from struct commit\n     @@ Commit message\n          i386) both layouts are already free of padding, so the size stays the\n          same there.\n      \n     -    Since the order of the members no longer reflects their importance,\n     -    document that only \"object\" has to come first, and describe \"index\"\n     -    and \"date\", whose meanings are not obvious from their names.\n     +    Since the order of the members no longer reflects their logical\n     +    grouping or importance, document that only \"object\" has to come\n     +    first, and describe \"index\" and \"date\", whose meanings are not\n     +    obvious from their names.\n      \n          Measuring peak RSS of \"git rev-list --all\" over an internal monorepo\n          with ~4.5 million commits (three runs each):\n     @@ commit.h: struct commit_list {\n        */\n       struct commit {\n      +\t/*\n     -+\t * Must be the first member, so that a \"struct object *\" can be\n     -+\t * cast to a \"struct commit *\".\n     ++\t * Must be the first member, so that a \"struct commit *\" can be\n     ++\t * cast to a \"struct object *\".\n      +\t */\n       \tstruct object object;\n      +\n     -+\t/*\n     -+\t * Index into the commit-slab arrays: unique per in-core commit,\n     -+\t * and only meaningful within the current process.\n     -+\t */\n     ++\t/* Index into the commit-slab arrays, unique per in-core commit. */\n      +\tunsigned int index;\n      +\n     -+\t/*\n     -+\t * The committer date taken from the commit object, without any\n     -+\t * adjustment.\n     -+\t */\n     ++\t/* The committer date of this commit, without any adjustment. */\n       \ttimestamp_t date;\n      +\n       \tstruct commit_list *parents;\n\n\n commit.h | 15 ++++++++++++++-\n 1 file changed, 14 insertions(+), 1 deletion(-)\n\ndiff --git a/commit.h b/commit.h\nindex 1061ed791b..1bff712c85 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -23,10 +23,24 @@ struct commit_list {\n  * The size of this struct matters in full repo walk operations like\n  * 'git clone' or 'git gc'. Consider using commit-slab to attach data\n  * to a commit instead of adding new fields here.\n+ *\n+ * The order of the members is partly chosen to keep the memory layout\n+ * compact (avoiding alignment padding), and does not necessarily\n+ * reflect their importance or grouping.\n  */\n struct commit {\n+\t/*\n+\t * Must be the first member, so that a \"struct commit *\" can be\n+\t * cast to a \"struct object *\".\n+\t */\n \tstruct object object;\n+\n+\t/* Index into the commit-slab arrays, unique per in-core commit. */\n+\tunsigned int index;\n+\n+\t/* The committer date of this commit, without any adjustment. */\n \ttimestamp_t date;\n+\n \tstruct commit_list *parents;\n \n \t/*\n@@ -35,7 +49,6 @@ struct commit {\n \t * or get_commit_tree_oid().\n \t */\n \tstruct tree *maybe_tree;\n-\tunsigned int index;\n };\n \n extern int save_commit_buffer;\n\nbase-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2\n-- \ngitgitgadget\n\n"}]}