@matheusbsbs / matheus-bagliano / commits / 23c17d4a4a
Fork 0

Simplify topo-sort logic

.. by not using quite so much indirection. This currently grows the "struct commit" a bit, which could be avoided by using a union for "util" and "indegree" (the topo-sort used to use "util" anyway, so you cannot use them together), but for now the goal of this was to simplify, not optimize. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Linus Torvalds committed Nov 2, 2007 at 13:32 UTC 23c17d4a4a0e1fc9a5fa347f1fc6be3cf477e543
4 files changed +55 -126
commit.c
+52 -98
@@ -9,22 +9,6 @@
9
10 int save_commit_buffer = 1;
11
12 -struct sort_node
13 -{
14 - /*
15 - * the number of children of the associated commit
16 - * that also occur in the list being sorted.
17 - */
18 - unsigned int indegree;
19 -
20 - /*
21 - * reference to original list item that we will re-use
22 - * on output.
23 - */
24 - struct commit_list * list_item;
25 -
26 -};
27 -
12 const char *commit_type = "commit";
13
14 static struct cmt_fmt_map {
@@ -1149,69 +1133,38 @@ struct commit *pop_commit(struct commit_list **stack)
1133 return item;
1134 }
1135
1152 -void topo_sort_default_setter(struct commit *c, void *data)
1153 -{
1154 - c->util = data;
1155 -}
1156 -
1157 -void *topo_sort_default_getter(struct commit *c)
1158 -{
1159 - return c->util;
1160 -}
1161 -
1136 /*
1137 * Performs an in-place topological sort on the list supplied.
1138 */
1139 void sort_in_topological_order(struct commit_list ** list, int lifo)
1140 {
1167 - sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
1168 - topo_sort_default_getter);
1169 -}
1170 -
1171 -void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1172 - topo_sort_set_fn_t setter,
1173 - topo_sort_get_fn_t getter)
1174 -{
1175 - struct commit_list * next = *list;
1176 - struct commit_list * work = NULL, **insert;
1177 - struct commit_list ** pptr = list;
1178 - struct sort_node * nodes;
1179 - struct sort_node * next_nodes;
1180 - int count = 0;
1181 -
1182 - /* determine the size of the list */
1183 - while (next) {
1184 - next = next->next;
1185 - count++;
1186 - }
1141 + struct commit_list *next, *orig = *list;
1142 + struct commit_list *work, **insert;
1143 + struct commit_list **pptr;
1144
1188 - if (!count)
1145 + if (!orig)
1146 return;
1190 - /* allocate an array to help sort the list */
1191 - nodes = xcalloc(count, sizeof(*nodes));
1192 - /* link the list to the array */
1193 - next_nodes = nodes;
1194 - next=*list;
1195 - while (next) {
1196 - next_nodes->list_item = next;
1197 - setter(next->item, next_nodes);
1198 - next_nodes++;
1199 - next = next->next;
1147 + *list = NULL;
1148 +
1149 + /* Mark them and clear the indegree */
1150 + for (next = orig; next; next = next->next) {
1151 + struct commit *commit = next->item;
1152 + commit->object.flags |= TOPOSORT;
1153 + commit->indegree = 0;
1154 }
1155 +
1156 /* update the indegree */
1202 - next=*list;
1203 - while (next) {
1157 + for (next = orig; next; next = next->next) {
1158 struct commit_list * parents = next->item->parents;
1159 while (parents) {
1206 - struct commit * parent=parents->item;
1207 - struct sort_node * pn = (struct sort_node *) getter(parent);
1160 + struct commit *parent = parents->item;
1161
1209 - if (pn)
1210 - pn->indegree++;
1211 - parents=parents->next;
1162 + if (parent->object.flags & TOPOSORT)
1163 + parent->indegree++;
1164 + parents = parents->next;
1165 }
1213 - next=next->next;
1166 }
1167 +
1168 /*
1169 * find the tips
1170 *
@@ -1219,55 +1172,56 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1172 *
1173 * the tips serve as a starting set for the work queue.
1174 */
1222 - next=*list;
1175 + work = NULL;
1176 insert = &work;
1224 - while (next) {
1225 - struct sort_node * node = (struct sort_node *) getter(next->item);
1177 + for (next = orig; next; next = next->next) {
1178 + struct commit *commit = next->item;
1179
1227 - if (node->indegree == 0) {
1228 - insert = &commit_list_insert(next->item, insert)->next;
1229 - }
1230 - next=next->next;
1180 + if (!commit->indegree)
1181 + insert = &commit_list_insert(commit, insert)->next;
1182 }
1183
1184 /* process the list in topological order */
1185 if (!lifo)
1186 sort_by_date(&work);
1187 +
1188 + pptr = list;
1189 + *list = NULL;
1190 while (work) {
1237 - struct commit * work_item = pop_commit(&work);
1238 - struct sort_node * work_node = (struct sort_node *) getter(work_item);
1239 - struct commit_list * parents = work_item->parents;
1191 + struct commit *commit;
1192 + struct commit_list *parents, *work_item;
1193
1241 - while (parents) {
1242 - struct commit * parent=parents->item;
1243 - struct sort_node * pn = (struct sort_node *) getter(parent);
1244 -
1245 - if (pn) {
1246 - /*
1247 - * parents are only enqueued for emission
1248 - * when all their children have been emitted thereby
1249 - * guaranteeing topological order.
1250 - */
1251 - pn->indegree--;
1252 - if (!pn->indegree) {
1253 - if (!lifo)
1254 - insert_by_date(parent, &work);
1255 - else
1256 - commit_list_insert(parent, &work);
1257 - }
1194 + work_item = work;
1195 + work = work_item->next;
1196 + work_item->next = NULL;
1197 +
1198 + commit = work_item->item;
1199 + for (parents = commit->parents; parents ; parents = parents->next) {
1200 + struct commit *parent=parents->item;
1201 +
1202 + if (!(parent->object.flags & TOPOSORT))
1203 + continue;
1204 +
1205 + /*
1206 + * parents are only enqueued for emission
1207 + * when all their children have been emitted thereby
1208 + * guaranteeing topological order.
1209 + */
1210 + if (!--parent->indegree) {
1211 + if (!lifo)
1212 + insert_by_date(parent, &work);
1213 + else
1214 + commit_list_insert(parent, &work);
1215 }
1259 - parents=parents->next;
1216 }
1217 /*
1218 * work_item is a commit all of whose children
1219 * have already been emitted. we can emit it now.
1220 */
1265 - *pptr = work_node->list_item;
1266 - pptr = &(*pptr)->next;
1267 - *pptr = NULL;
1268 - setter(work_item, NULL);
1221 + commit->object.flags &= ~TOPOSORT;
1222 + *pptr = work_item;
1223 + pptr = &work_item->next;
1224 }
1270 - free(nodes);
1225 }
1226
1227 /* merge-base stuff */
commit.h
+1 -19
@@ -14,6 +14,7 @@ struct commit_list {
14 struct commit {
15 struct object object;
16 void *util;
17 + unsigned int indegree;
18 unsigned long date;
19 struct commit_list *parents;
20 struct tree *tree;
@@ -84,31 +85,12 @@ void clear_commit_marks(struct commit *commit, unsigned int mark);
85 /*
86 * Performs an in-place topological sort of list supplied.
87 *
87 - * Pre-conditions for sort_in_topological_order:
88 - * all commits in input list and all parents of those
89 - * commits must have object.util == NULL
90 - *
91 - * Pre-conditions for sort_in_topological_order_fn:
92 - * all commits in input list and all parents of those
93 - * commits must have getter(commit) == NULL
94 - *
95 - * Post-conditions:
88 * invariant of resulting list is:
89 * a reachable from b => ord(b) < ord(a)
90 * in addition, when lifo == 0, commits on parallel tracks are
91 * sorted in the dates order.
92 */
101 -
102 -typedef void (*topo_sort_set_fn_t)(struct commit*, void *data);
103 -typedef void* (*topo_sort_get_fn_t)(struct commit*);
104 -
105 -void topo_sort_default_setter(struct commit *c, void *data);
106 -void *topo_sort_default_getter(struct commit *c);
107 -
93 void sort_in_topological_order(struct commit_list ** list, int lifo);
109 -void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
110 - topo_sort_set_fn_t setter,
111 - topo_sort_get_fn_t getter);
94
95 struct commit_graft {
96 unsigned char sha1[20];
revision.c
+1 -6
@@ -677,9 +677,6 @@ void init_revisions(struct rev_info *revs, const char *prefix)
677 revs->prune_fn = NULL;
678 revs->prune_data = NULL;
679
680 - revs->topo_setter = topo_sort_default_setter;
681 - revs->topo_getter = topo_sort_default_getter;
682 -
680 revs->commit_format = CMIT_FMT_DEFAULT;
681
682 diff_setup(&revs->diffopt);
@@ -1303,9 +1300,7 @@ int prepare_revision_walk(struct rev_info *revs)
1300 if (limit_list(revs) < 0)
1301 return -1;
1302 if (revs->topo_order)
1306 - sort_in_topological_order_fn(&revs->commits, revs->lifo,
1307 - revs->topo_setter,
1308 - revs->topo_getter);
1303 + sort_in_topological_order(&revs->commits, revs->lifo);
1304 return 0;
1305 }
1306
revision.h
+1 -3
@@ -10,6 +10,7 @@
10 #define CHILD_SHOWN (1u<<6)
11 #define ADDED (1u<<7) /* Parents already parsed and added? */
12 #define SYMMETRIC_LEFT (1u<<8)
13 +#define TOPOSORT (1u<<9) /* In the active toposort list.. */
14
15 struct rev_info;
16 struct log_info;
@@ -96,9 +97,6 @@ struct rev_info {
97 struct diff_options diffopt;
98 struct diff_options pruning;
99
99 - topo_sort_set_fn_t topo_setter;
100 - topo_sort_get_fn_t topo_getter;
101 -
100 struct reflog_walk_info *reflog_info;
101 };
102