From a5795633892fec01b27d87cd8fb61c20935933c0 Mon Sep 17 00:00:00 2001 From: Kristofer Karlsson Date: Thu, 12 Nov 2026 15:39:19 +0100 Subject: [PATCH] commit: remove padding from struct commit On 64-bit platforms, "struct commit" is 80 bytes, 8 of which are padding: - 4 bytes between .object and .date - 4 bytes of tail padding after .index Reduce memory usage by moving .index into the hole after .object. On 64-bit platforms this shrinks the struct to 72 bytes and eliminates the padding, resulting in 10% less memory for every commit allocated, with no change in behaviour. On platforms where 8-byte members only need 4-byte alignment (e.g. i386) both layouts are already free of padding, so the size stays the same there. Since the order of the members no longer reflects their logical grouping or importance, document that only "object" has to come first, and describe "index" and "date", whose meanings are not obvious from their names. Measuring peak RSS of "git rev-list --all" over an internal monorepo with ~4.5 million commits (three runs each): peak RSS time before ~849 MiB 12.8-13.7s after ~817 MiB 12.8-13.0s The ~32 MiB saved is close to the expected 8 bytes * 4.5M commits (~34 MiB); running time is unchanged within noise. Signed-off-by: Kristofer Karlsson --- commit.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/commit.h b/commit.h index 1061ed791bcad6..1bff712c854efd 100644 --- a/commit.h +++ b/commit.h @@ -23,10 +23,24 @@ 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. + * + * The order of the members is partly chosen to keep the memory layout + * compact (avoiding alignment padding), and does not necessarily + * reflect their importance or grouping. */ struct commit { + /* + * Must be the first member, so that a "struct commit *" can be + * cast to a "struct object *". + */ struct object object; + + /* Index into the commit-slab arrays, unique per in-core commit. */ + unsigned int index; + + /* The committer date of this commit, without any adjustment. */ timestamp_t date; + struct commit_list *parents; /* @@ -35,7 +49,6 @@ struct commit { * or get_commit_tree_oid(). */ struct tree *maybe_tree; - unsigned int index; }; extern int save_commit_buffer;