Skip to content

Commit 96c4f4a

Browse files
peffgitster
authored andcommitted
commit: allow associating auxiliary info on-demand
The "indegree" field in the commit object is only used while sorting a list of commits in topological order, and wasting memory otherwise. We would prefer to shrink the size of individual commit objects, which we may have to hold thousands of in-core. We could eject "indegree" field out from the commit object and represent it as a dynamic table based on the decoration infrastructure, but the decoration is meant for sparse annotation and is not a good match. Instead, let's try a different approach. - Assign an integer (commit->index) to each commit we keep in-core (reuse the space of "indegree" field for it); - When running the topological sort, allocate an array of integers in bulk (called "slab"), use the commit->index as an index into this array, and store the "indegree" information there. This does _not_ reduce the memory footprint of a commit object, but the commit->index can be used as the index to dynamically associate commits with other kinds of information as needed. Signed-off-by: Junio C Hamano <[email protected]>
1 parent a46221e commit 96c4f4a

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

commit.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static struct commit_extra_header *read_commit_extra_header_lines(const char *bu
1414
int save_commit_buffer = 1;
1515

1616
const char *commit_type = "commit";
17+
static int commit_count;
1718

1819
static struct commit *check_commit(struct object *obj,
1920
const unsigned char *sha1,
@@ -58,8 +59,11 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
5859
struct commit *lookup_commit(const unsigned char *sha1)
5960
{
6061
struct object *obj = lookup_object(sha1);
61-
if (!obj)
62-
return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
62+
if (!obj) {
63+
struct commit *c = alloc_commit_node();
64+
c->index = commit_count++;
65+
return create_object(sha1, OBJ_COMMIT, c);
66+
}
6367
if (!obj->type)
6468
obj->type = OBJ_COMMIT;
6569
return check_commit(obj, sha1, 0);
@@ -497,6 +501,36 @@ struct commit *pop_commit(struct commit_list **stack)
497501
return item;
498502
}
499503

504+
struct commit_slab {
505+
int *buf;
506+
int alloc;
507+
};
508+
509+
static void slab_init(struct commit_slab *s)
510+
{
511+
memset(s, 0, sizeof(*s));
512+
}
513+
514+
static void slab_clear(struct commit_slab *s)
515+
{
516+
free(s->buf);
517+
slab_init(s);
518+
}
519+
520+
static inline int *slab_at(struct commit_slab *s, const struct commit *c)
521+
{
522+
if (s->alloc <= c->index) {
523+
int new_alloc = alloc_nr(s->alloc);
524+
if (new_alloc <= c->index)
525+
new_alloc = c->index + 1;
526+
527+
s->buf = xrealloc(s->buf, new_alloc * sizeof(*s->buf));
528+
memset(s->buf + s->alloc, 0, new_alloc - s->alloc);
529+
s->alloc = new_alloc;
530+
}
531+
return s->buf + c->index;
532+
}
533+
500534
/*
501535
* Performs an in-place topological sort on the list supplied.
502536
*/
@@ -505,25 +539,29 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
505539
struct commit_list *next, *orig = *list;
506540
struct commit_list *work, **insert;
507541
struct commit_list **pptr;
542+
struct commit_slab indegree;
508543

509544
if (!orig)
510545
return;
511546
*list = NULL;
512547

548+
slab_init(&indegree);
549+
513550
/* Mark them and clear the indegree */
514551
for (next = orig; next; next = next->next) {
515552
struct commit *commit = next->item;
516-
commit->indegree = 1;
553+
*slab_at(&indegree, commit) = 1;
517554
}
518555

519556
/* update the indegree */
520557
for (next = orig; next; next = next->next) {
521558
struct commit_list * parents = next->item->parents;
522559
while (parents) {
523560
struct commit *parent = parents->item;
561+
int *pi = slab_at(&indegree, parent);
524562

525-
if (parent->indegree)
526-
parent->indegree++;
563+
if (*pi)
564+
(*pi)++;
527565
parents = parents->next;
528566
}
529567
}
@@ -540,7 +578,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
540578
for (next = orig; next; next = next->next) {
541579
struct commit *commit = next->item;
542580

543-
if (commit->indegree == 1)
581+
if (*slab_at(&indegree, commit) == 1)
544582
insert = &commit_list_insert(commit, insert)->next;
545583
}
546584

@@ -561,16 +599,17 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
561599
commit = work_item->item;
562600
for (parents = commit->parents; parents ; parents = parents->next) {
563601
struct commit *parent = parents->item;
602+
int *pi = slab_at(&indegree, parent);
564603

565-
if (!parent->indegree)
604+
if (!*pi)
566605
continue;
567606

568607
/*
569608
* parents are only enqueued for emission
570609
* when all their children have been emitted thereby
571610
* guaranteeing topological order.
572611
*/
573-
if (--parent->indegree == 1) {
612+
if (--(*pi) == 1) {
574613
if (!lifo)
575614
commit_list_insert_by_date(parent, &work);
576615
else
@@ -581,10 +620,12 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
581620
* work_item is a commit all of whose children
582621
* have already been emitted. we can emit it now.
583622
*/
584-
commit->indegree = 0;
623+
*slab_at(&indegree, commit) = 0;
585624
*pptr = work_item;
586625
pptr = &work_item->next;
587626
}
627+
628+
slab_clear(&indegree);
588629
}
589630

590631
/* merge-base stuff */

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct commit_list {
1414
struct commit {
1515
struct object object;
1616
void *util;
17-
unsigned int indegree;
17+
unsigned int index;
1818
unsigned long date;
1919
struct commit_list *parents;
2020
struct tree *tree;

0 commit comments

Comments
 (0)