The struct commit is used in many contexts. However, members `generation` and `graph_pos` are only used for commit-graph related operations and otherwise waste memory. As they are often accessed together, let's introduce struct commit_graph_data and move them to a commit_graph_data slab. Signed-off-by: Abhishek Kumar <abhishekkumar8222@xxxxxxxxx> --- commit-graph.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ commit-graph.h | 10 ++++++++++ 2 files changed, 59 insertions(+) diff --git a/commit-graph.c b/commit-graph.c index e3420ddcbf..7d887a6a2c 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -87,6 +87,55 @@ static int commit_pos_cmp(const void *va, const void *vb) commit_pos_at(&commit_pos, b); } +define_commit_slab(commit_graph_data_slab, struct commit_graph_data); +static struct commit_graph_data_slab commit_graph_data_slab = + COMMIT_SLAB_INIT(1, commit_graph_data_slab); + +uint32_t commit_graph_position(const struct commit *c) +{ + struct commit_graph_data *data = + commit_graph_data_slab_peek(&commit_graph_data_slab, c); + + return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH; +} + +uint32_t commit_graph_generation(const struct commit *c) +{ + struct commit_graph_data *data = + commit_graph_data_slab_peek(&commit_graph_data_slab, c); + + if (!data) + return GENERATION_NUMBER_INFINITY; + if (data->graph_pos == COMMIT_NOT_FROM_GRAPH) + return GENERATION_NUMBER_INFINITY; + + return data->generation; +} + +static struct commit_graph_data *commit_graph_data_at(const struct commit *c) +{ + uint32_t i = commit_graph_data_slab.slab_count, j; + uint32_t slab_size = commit_graph_data_slab.slab_size; + struct commit_graph_data *data = + commit_graph_data_slab_at(&commit_graph_data_slab, c); + + /* + * commit-slab initializes elements with zero, overwrite this with + * COMMIT_NOT_FROM_GRAPH for graph_pos. + * + * We avoid the cost of initializing `generation` as generation + * number would be GENERATION_NUMBER_INFINITY if graph position + * is COMMIT_NOT_FROM_GRAPH. + */ + for (; i < commit_graph_data_slab.slab_count; i++) { + for (j = 0; j < slab_size; j++) { + commit_graph_data_slab.slab[i][j].graph_pos = COMMIT_NOT_FROM_GRAPH; + } + } + + return data; +} + static int commit_gen_cmp(const void *va, const void *vb) { const struct commit *a = *(const struct commit **)va; diff --git a/commit-graph.h b/commit-graph.h index 4212766a4f..9d22f98f44 100644 --- a/commit-graph.h +++ b/commit-graph.h @@ -137,4 +137,14 @@ void free_commit_graph(struct commit_graph *); */ void disable_commit_graph(struct repository *r); +struct commit_graph_data { + uint32_t graph_pos; + uint32_t generation; +}; + +/* + * Commits should be parsed before accessing generation, graph positions. + */ +uint32_t commit_graph_generation(const struct commit *); +uint32_t commit_graph_position(const struct commit *); #endif -- 2.27.0