Browse Source

commit-slab: avoid large realloc

Instead of using a single "slab" and keep reallocating it as we find
that we need to deal with commits with larger values of commit->index,
make a "slab" an array of many "slab_piece"s. Each access may need
two levels of indirections, but we only need to reallocate the first
level array of pointers when we have to grow the table this way.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Junio C Hamano 12 years ago
parent
commit
66eb375d3d
  1. 60
      commit.c

60
commit.c

@ -501,34 +501,56 @@ struct commit *pop_commit(struct commit_list **stack)
return item; return item;
} }


struct commit_slab_piece {
int buf;
};

struct commit_slab { struct commit_slab {
int *buf; int piece_size;
int alloc; int piece_count;
struct commit_slab_piece **piece;
}; };


static void slab_init(struct commit_slab *s) static void slab_init(struct commit_slab *s)
{ {
memset(s, 0, sizeof(*s)); /* allocate ~512kB at once, allowing for malloc overhead */
int size = (512*1024-32) / sizeof(struct commit_slab_piece);

s->piece_size = size;
s->piece_count = 0;
s->piece = NULL;
} }


static void slab_clear(struct commit_slab *s) static void slab_clear(struct commit_slab *s)
{ {
free(s->buf); int i;
slab_init(s);
for (i = 0; i < s->piece_count; i++)
free(s->piece[i]);
s->piece_count = 0;
free(s->piece);
s->piece = NULL;
} }


static inline int *slab_at(struct commit_slab *s, const struct commit *c) static inline struct commit_slab_piece *slab_at(struct commit_slab *s,
const struct commit *c)
{ {
if (s->alloc <= c->index) { int nth_piece, nth_slot;
int new_alloc = alloc_nr(s->alloc);
if (new_alloc <= c->index) nth_piece = c->index / s->piece_size;
new_alloc = c->index + 1; nth_slot = c->index % s->piece_size;

if (s->piece_count <= nth_piece) {
int i;


s->buf = xrealloc(s->buf, new_alloc * sizeof(*s->buf)); s->piece = xrealloc(s->piece, (nth_piece + 1) * sizeof(s->piece));
memset(s->buf + s->alloc, 0, new_alloc - s->alloc); for (i = s->piece_count; i <= nth_piece; i++)
s->alloc = new_alloc; s->piece[i] = NULL;
s->piece_count = nth_piece + 1;
} }
return s->buf + c->index; if (!s->piece[nth_piece])
s->piece[nth_piece] = xcalloc(s->piece_size, sizeof(**s->piece));
return &s->piece[nth_piece][nth_slot];
} }


/* /*
@ -550,7 +572,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
/* Mark them and clear the indegree */ /* Mark them and clear the indegree */
for (next = orig; next; next = next->next) { for (next = orig; next; next = next->next) {
struct commit *commit = next->item; struct commit *commit = next->item;
*slab_at(&indegree, commit) = 1; slab_at(&indegree, commit)->buf = 1;
} }


/* update the indegree */ /* update the indegree */
@ -558,7 +580,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
struct commit_list * parents = next->item->parents; struct commit_list * parents = next->item->parents;
while (parents) { while (parents) {
struct commit *parent = parents->item; struct commit *parent = parents->item;
int *pi = slab_at(&indegree, parent); int *pi = &slab_at(&indegree, parent)->buf;


if (*pi) if (*pi)
(*pi)++; (*pi)++;
@ -578,7 +600,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
for (next = orig; next; next = next->next) { for (next = orig; next; next = next->next) {
struct commit *commit = next->item; struct commit *commit = next->item;


if (*slab_at(&indegree, commit) == 1) if (slab_at(&indegree, commit)->buf == 1)
insert = &commit_list_insert(commit, insert)->next; insert = &commit_list_insert(commit, insert)->next;
} }


@ -599,7 +621,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
commit = work_item->item; commit = work_item->item;
for (parents = commit->parents; parents ; parents = parents->next) { for (parents = commit->parents; parents ; parents = parents->next) {
struct commit *parent = parents->item; struct commit *parent = parents->item;
int *pi = slab_at(&indegree, parent); int *pi = &slab_at(&indegree, parent)->buf;


if (!*pi) if (!*pi)
continue; continue;
@ -620,7 +642,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
* work_item is a commit all of whose children * work_item is a commit all of whose children
* have already been emitted. we can emit it now. * have already been emitted. we can emit it now.
*/ */
*slab_at(&indegree, commit) = 0; slab_at(&indegree, commit)->buf = 0;
*pptr = work_item; *pptr = work_item;
pptr = &work_item->next; pptr = &work_item->next;
} }

Loading…
Cancel
Save