Skip to content

Commit 55f34c8

Browse files
committed
Merge branch 'jk/commit-info-slab'
Allow adding custom information to commit objects in order to represent unbound number of flag bits etc. * jk/commit-info-slab: commit-slab: introduce a macro to define a slab for new type commit-slab: avoid large realloc commit: allow associating auxiliary info on-demand
2 parents 7a3187e + a84b794 commit 55f34c8

File tree

3 files changed

+127
-10
lines changed

3 files changed

+127
-10
lines changed

commit-slab.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef COMMIT_SLAB_H
2+
#define COMMIT_SLAB_H
3+
4+
/*
5+
* define_commit_slab(slabname, elemtype) creates boilerplate code to define
6+
* a new struct (struct slabname) that is used to associate a piece of data
7+
* of elemtype to commits, and a few functions to use that struct.
8+
*
9+
* After including this header file, using:
10+
*
11+
* define_commit_slab(indegee, int);
12+
*
13+
* will let you call the following functions:
14+
*
15+
* - int *indegree_at(struct indegree *, struct commit *);
16+
*
17+
* This function locates the data associated with the given commit in
18+
* the indegree slab, and returns the pointer to it.
19+
*
20+
* - void init_indegree(struct indegree *);
21+
* void init_indegree_with_stride(struct indegree *, int);
22+
*
23+
* Initializes the indegree slab that associates an array of integers
24+
* to each commit. 'stride' specifies how big each array is. The slab
25+
* that id initialied by the variant without "_with_stride" associates
26+
* each commit with an array of one integer.
27+
*/
28+
29+
/* allocate ~512kB at once, allowing for malloc overhead */
30+
#ifndef COMMIT_SLAB_SIZE
31+
#define COMMIT_SLAB_SIZE (512*1024-32)
32+
#endif
33+
34+
#define define_commit_slab(slabname, elemtype) \
35+
\
36+
struct slabname { \
37+
unsigned slab_size; \
38+
unsigned stride; \
39+
unsigned slab_count; \
40+
elemtype **slab; \
41+
}; \
42+
static int stat_ ##slabname## realloc; \
43+
\
44+
static void init_ ##slabname## _with_stride(struct slabname *s, \
45+
unsigned stride) \
46+
{ \
47+
unsigned int elem_size; \
48+
if (!stride) \
49+
stride = 1; \
50+
s->stride = stride; \
51+
elem_size = sizeof(struct slabname) * stride; \
52+
s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
53+
s->slab_count = 0; \
54+
s->slab = NULL; \
55+
} \
56+
\
57+
static void init_ ##slabname(struct slabname *s) \
58+
{ \
59+
init_ ##slabname## _with_stride(s, 1); \
60+
} \
61+
\
62+
static void clear_ ##slabname(struct slabname *s) \
63+
{ \
64+
int i; \
65+
for (i = 0; i < s->slab_count; i++) \
66+
free(s->slab[i]); \
67+
s->slab_count = 0; \
68+
free(s->slab); \
69+
s->slab = NULL; \
70+
} \
71+
\
72+
static elemtype *slabname## _at(struct slabname *s, \
73+
const struct commit *c) \
74+
{ \
75+
int nth_slab, nth_slot, ix; \
76+
\
77+
ix = c->index * s->stride; \
78+
nth_slab = ix / s->slab_size; \
79+
nth_slot = ix % s->slab_size; \
80+
\
81+
if (s->slab_count <= nth_slab) { \
82+
int i; \
83+
s->slab = xrealloc(s->slab, \
84+
(nth_slab + 1) * sizeof(s->slab)); \
85+
stat_ ##slabname## realloc++; \
86+
for (i = s->slab_count; i <= nth_slab; i++) \
87+
s->slab[i] = NULL; \
88+
s->slab_count = nth_slab + 1; \
89+
} \
90+
if (!s->slab[nth_slab]) \
91+
s->slab[nth_slab] = xcalloc(s->slab_size, \
92+
sizeof(**s->slab)); \
93+
return &s->slab[nth_slab][nth_slot]; \
94+
} \
95+
\
96+
static int stat_ ##slabname## realloc
97+
98+
#endif /* COMMIT_SLAB_H */

commit.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#include "notes.h"
99
#include "gpg-interface.h"
1010
#include "mergesort.h"
11+
#include "commit-slab.h"
1112

1213
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
1314

1415
int save_commit_buffer = 1;
1516

1617
const char *commit_type = "commit";
18+
static int commit_count;
1719

1820
static struct commit *check_commit(struct object *obj,
1921
const unsigned char *sha1,
@@ -58,8 +60,11 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
5860
struct commit *lookup_commit(const unsigned char *sha1)
5961
{
6062
struct object *obj = lookup_object(sha1);
61-
if (!obj)
62-
return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
63+
if (!obj) {
64+
struct commit *c = alloc_commit_node();
65+
c->index = commit_count++;
66+
return create_object(sha1, OBJ_COMMIT, c);
67+
}
6368
if (!obj->type)
6469
obj->type = OBJ_COMMIT;
6570
return check_commit(obj, sha1, 0);
@@ -506,6 +511,13 @@ struct commit *pop_commit(struct commit_list **stack)
506511
return item;
507512
}
508513

514+
/*
515+
* Topological sort support
516+
*/
517+
518+
/* count number of children that have not been emitted */
519+
define_commit_slab(indegree_slab, int);
520+
509521
/*
510522
* Performs an in-place topological sort on the list supplied.
511523
*/
@@ -514,25 +526,29 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
514526
struct commit_list *next, *orig = *list;
515527
struct commit_list *work, **insert;
516528
struct commit_list **pptr;
529+
struct indegree_slab indegree;
517530

518531
if (!orig)
519532
return;
520533
*list = NULL;
521534

535+
init_indegree_slab(&indegree);
536+
522537
/* Mark them and clear the indegree */
523538
for (next = orig; next; next = next->next) {
524539
struct commit *commit = next->item;
525-
commit->indegree = 1;
540+
*(indegree_slab_at(&indegree, commit)) = 1;
526541
}
527542

528543
/* update the indegree */
529544
for (next = orig; next; next = next->next) {
530545
struct commit_list * parents = next->item->parents;
531546
while (parents) {
532547
struct commit *parent = parents->item;
548+
int *pi = indegree_slab_at(&indegree, parent);
533549

534-
if (parent->indegree)
535-
parent->indegree++;
550+
if (*pi)
551+
(*pi)++;
536552
parents = parents->next;
537553
}
538554
}
@@ -549,7 +565,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
549565
for (next = orig; next; next = next->next) {
550566
struct commit *commit = next->item;
551567

552-
if (commit->indegree == 1)
568+
if (*(indegree_slab_at(&indegree, commit)) == 1)
553569
insert = &commit_list_insert(commit, insert)->next;
554570
}
555571

@@ -570,16 +586,17 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
570586
commit = work_item->item;
571587
for (parents = commit->parents; parents ; parents = parents->next) {
572588
struct commit *parent = parents->item;
589+
int *pi = indegree_slab_at(&indegree, parent);
573590

574-
if (!parent->indegree)
591+
if (!*pi)
575592
continue;
576593

577594
/*
578595
* parents are only enqueued for emission
579596
* when all their children have been emitted thereby
580597
* guaranteeing topological order.
581598
*/
582-
if (--parent->indegree == 1) {
599+
if (--(*pi) == 1) {
583600
if (!lifo)
584601
commit_list_insert_by_date(parent, &work);
585602
else
@@ -590,10 +607,12 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
590607
* work_item is a commit all of whose children
591608
* have already been emitted. we can emit it now.
592609
*/
593-
commit->indegree = 0;
610+
*(indegree_slab_at(&indegree, commit)) = 0;
594611
*pptr = work_item;
595612
pptr = &work_item->next;
596613
}
614+
615+
clear_indegree_slab(&indegree);
597616
}
598617

599618
/* merge-base stuff */

commit.h

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

0 commit comments

Comments
 (0)