speed up initial stage of packing by adding a "skip" commit color The skip color marks boundary commits and their ancestors. Boundary commits are reachable both via references which we want to exclude from the pack, and via references which we want to include in the pack. We continue processing commit history up to the point we are left with only skip commits on the queue. This can speed up findtwixt() significantly and avoids wrong results produced by the old algorithm which made no distinction between "drop" and "skip". This idea was first implemented by Michael Forney for git9: https://git.9front.org/plan9front/plan9front/2e47badb88312c5c045a8042dc2ef80148e5ab47/commit.html Michael's log message for git9 is reproduced below: git/query: refactor graph painting algorithm (findtwixt, lca) We now keep track of 3 sets during traversal: - keep: commits we've reached from head commits - drop: commits we've reached from tail commits - skip: ancestors of commits in both 'keep' and 'drop' Commits in 'keep' and/or 'drop' may be added later to the 'skip' set if we discover later that they are part of a common subgraph of the head and tail commits. From these sets we can calculate the commits we are interested in: lca commits are those in 'keep' and 'drop', but not in 'skip'. findtwixt commits are those in 'keep', but not in 'drop' or 'skip'. The "LCA" commit returned is a common ancestor such that there are no other common ancestors that can reach that commit. Although there can be multiple commits that meet this criteria, where one is technically lower on the commit-graph than the other, these cases only happen in complex merge arrangements and any choice is likely a decent merge base. Repainting is now done in paint() directly. When we find a boundary commit, we switch our paint color to 'skip'. 'skip' painting does not stop when it hits another color; we continue until we are left with only 'skip' commits on the queue. This fixes several mishandled cases in the current algorithm: 1. If we hit the common subgraph from tail commits first (if the tail commit was newer than the head commit), we ended up traversing the entire commit graph. This is because we couldn't distinguish between 'drop' commits that were part of the common subgraph, and those that were still looking for it. 2. If we traversed through an initial part of the common subgraph from head commits before reaching it from tail commits, these commits were returned from findtwixt even though they were also reachable from tail commits. 3. In the same case as 2, we might end up choosing an incorrect commit as the LCA, which is an ancestor of the real LCA.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
diff --git a/lib/pack_create.c b/lib/pack_create.c
index 4e9a9b9..fa7ea77 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -61,6 +61,10 @@
#define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
#endif
+#ifndef nitems
+#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
struct got_pack_meta {
struct got_object_id id;
char *path;
@@ -1108,96 +1112,46 @@ enum findtwixt_color {
COLOR_KEEP = 0,
COLOR_DROP,
COLOR_BLANK,
+ COLOR_SKIP,
};
+
static const int findtwixt_colors[] = {
COLOR_KEEP,
COLOR_DROP,
- COLOR_BLANK
+ COLOR_BLANK,
+ COLOR_SKIP,
};
static const struct got_error *
-queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
- int color, struct got_repository *repo)
+paint_commit(struct got_object_qid *qid, int color)
{
- const struct got_error *err;
- struct got_object_qid *qid;
+ if (color < 0 || color >= nitems(findtwixt_colors))
+ return got_error(GOT_ERR_RANGE);
- err = got_object_qid_alloc(&qid, id);
- if (err)
- return err;
-
- STAILQ_INSERT_TAIL(ids, qid, entry);
qid->data = (void *)&findtwixt_colors[color];
return NULL;
}
static const struct got_error *
-drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
- struct got_object_id *id, struct got_repository *repo,
- got_cancel_cb cancel_cb, void *cancel_arg)
+queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
+ int color, struct got_repository *repo)
{
- const struct got_error *err = NULL;
- struct got_commit_object *commit;
- const struct got_object_id_queue *parents;
- struct got_object_id_queue ids;
+ const struct got_error *err;
struct got_object_qid *qid;
- STAILQ_INIT(&ids);
-
err = got_object_qid_alloc(&qid, id);
if (err)
return err;
- STAILQ_INSERT_HEAD(&ids, qid, entry);
-
- while (!STAILQ_EMPTY(&ids)) {
- if (cancel_cb) {
- err = (*cancel_cb)(cancel_arg);
- if (err)
- break;
- }
-
- qid = STAILQ_FIRST(&ids);
- STAILQ_REMOVE_HEAD(&ids, entry);
- if (got_object_idset_contains(drop, qid->id)) {
- got_object_qid_free(qid);
- continue;
- }
-
- err = got_object_idset_add(drop, qid->id, NULL);
- if (err) {
- got_object_qid_free(qid);
- break;
- }
-
- if (!got_object_idset_contains(keep, qid->id)) {
- got_object_qid_free(qid);
- continue;
- }
-
- err = got_object_open_as_commit(&commit, repo, qid->id);
- got_object_qid_free(qid);
- if (err)
- break;
-
- parents = got_object_commit_get_parent_ids(commit);
- if (parents) {
- err = got_object_id_queue_copy(parents, &ids);
- if (err) {
- got_object_commit_close(commit);
- break;
- }
- }
- got_object_commit_close(commit);
- }
-
- got_object_id_queue_free(&ids);
- return err;
+ STAILQ_INSERT_TAIL(ids, qid, entry);
+ return paint_commit(qid, color);
}
struct append_id_arg {
struct got_object_id **array;
int idx;
+ struct got_object_idset *drop;
+ struct got_object_idset *skip;
};
static const struct got_error *
@@ -1205,11 +1159,14 @@ append_id(struct got_object_id *id, void *data, void *arg)
{
struct append_id_arg *a = arg;
- a->array[a->idx] = got_object_id_dup(id);
+ if (got_object_idset_contains(a->skip, id) ||
+ got_object_idset_contains(a->drop, id))
+ return NULL;
+
+ a->array[++a->idx] = got_object_id_dup(id);
if (a->array[a->idx] == NULL)
return got_error_from_errno("got_object_id_dup");
- a->idx++;
return NULL;
}
@@ -1245,18 +1202,20 @@ done:
}
static const struct got_error *
-color_commits(int *ncolored, struct got_object_id_queue *ids,
+paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
struct got_object_idset *keep, struct got_object_idset *drop,
- struct got_repository *repo,
+ struct got_object_idset *skip, struct got_repository *repo,
got_pack_progress_cb progress_cb, void *progress_arg,
struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_commit_object *commit = NULL;
+ const struct got_object_id_queue *parents;
struct got_object_qid *qid;
+ int nqueued = nids, nskip = 0;
- while (!STAILQ_EMPTY(ids)) {
- int qcolor, ncolor;
+ while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
+ int color;
if (cancel_cb) {
err = cancel_cb(cancel_arg);
@@ -1265,71 +1224,94 @@ color_commits(int *ncolored, struct got_object_id_queue *ids,
}
qid = STAILQ_FIRST(ids);
- qcolor = *((int *)qid->data);
-
- if (got_object_idset_contains(drop, qid->id))
- ncolor = COLOR_DROP;
- else if (got_object_idset_contains(keep, qid->id))
- ncolor = COLOR_KEEP;
- else
- ncolor = COLOR_BLANK;
-
- (*ncolored)++;
- err = report_progress(progress_cb, progress_arg, rl,
- *ncolored, 0, 0, 0L, 0, 0, 0, 0);
- if (err)
- break;
+ STAILQ_REMOVE_HEAD(ids, entry);
+ nqueued--;
+ color = *((int *)qid->data);
+ if (color == COLOR_SKIP)
+ nskip--;
- if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
- qcolor == COLOR_KEEP)) {
- STAILQ_REMOVE_HEAD(ids, entry);
+ if (got_object_idset_contains(skip, qid->id)) {
got_object_qid_free(qid);
continue;
}
- if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
- err = drop_commit(keep, drop, qid->id, repo,
- cancel_cb, cancel_arg);
- if (err)
- break;
- } else if (ncolor == COLOR_BLANK) {
- struct got_commit_object *commit;
- const struct got_object_id_queue *parents;
- struct got_object_qid *pid;
-
- if (qcolor == COLOR_KEEP)
- err = got_object_idset_add(keep, qid->id, NULL);
- else
- err = got_object_idset_add(drop, qid->id, NULL);
+ switch (color) {
+ case COLOR_KEEP:
+ if (got_object_idset_contains(keep, qid->id)) {
+ got_object_qid_free(qid);
+ continue;
+ }
+ if (got_object_idset_contains(drop, qid->id)) {
+ err = paint_commit(qid, COLOR_SKIP);
+ if (err)
+ goto done;
+ nskip++;
+ } else
+ (*ncolored)++;
+ err = got_object_idset_add(keep, qid->id, NULL);
if (err)
- break;
-
- err = got_object_open_as_commit(&commit, repo, qid->id);
+ goto done;
+ break;
+ case COLOR_DROP:
+ if (got_object_idset_contains(drop, qid->id)) {
+ got_object_qid_free(qid);
+ continue;
+ }
+ if (got_object_idset_contains(keep, qid->id)) {
+ err = paint_commit(qid, COLOR_SKIP);
+ if (err)
+ goto done;
+ nskip++;
+ } else
+ (*ncolored)++;
+ err = got_object_idset_add(drop, qid->id, NULL);
if (err)
- break;
-
- parents = got_object_commit_get_parent_ids(commit);
- if (parents) {
- STAILQ_FOREACH(pid, parents, entry) {
- err = queue_commit_id(ids, pid->id,
- qcolor, repo);
- if (err)
- break;
- }
+ goto done;
+ break;
+ case COLOR_SKIP:
+ if (!got_object_idset_contains(skip, qid->id)) {
+ err = got_object_idset_add(skip, qid->id, NULL);
+ if (err)
+ goto done;
}
- got_object_commit_close(commit);
- commit = NULL;
- } else {
+ break;
+ default:
/* should not happen */
err = got_error_fmt(GOT_ERR_NOT_IMPL,
- "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
+ "%s invalid commit color %d", __func__, color);
+ goto done;
+ }
+
+ err = report_progress(progress_cb, progress_arg, rl,
+ *ncolored, 0, 0, 0L, 0, 0, 0, 0);
+ if (err)
+ break;
+
+
+ err = got_object_open_as_commit(&commit, repo, qid->id);
+ if (err)
break;
+
+ parents = got_object_commit_get_parent_ids(commit);
+ if (parents) {
+ struct got_object_qid *pid;
+ color = *((int *)qid->data);
+ STAILQ_FOREACH(pid, parents, entry) {
+ err = queue_commit_id(ids, pid->id, color,
+ repo);
+ if (err)
+ break;
+ nqueued++;
+ if (color == COLOR_SKIP)
+ nskip++;
+ }
}
- STAILQ_REMOVE_HEAD(ids, entry);
+ got_object_commit_close(commit);
+ commit = NULL;
got_object_qid_free(qid);
}
-
+done:
if (commit)
got_object_commit_close(commit);
return err;
@@ -1345,7 +1327,7 @@ findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
{
const struct got_error *err = NULL;
struct got_object_id_queue ids;
- struct got_object_idset *keep, *drop;
+ struct got_object_idset *keep, *drop, *skip = NULL;
int i, nkeep;
STAILQ_INIT(&ids);
@@ -1363,6 +1345,12 @@ findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
goto done;
}
+ skip = got_object_idset_alloc();
+ if (skip == NULL) {
+ err = got_error_from_errno("got_object_idset_alloc");
+ goto done;
+ }
+
for (i = 0; i < nhead; i++) {
struct got_object_id *id = head[i];
if (id == NULL)
@@ -1381,8 +1369,9 @@ findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
goto done;
}
- err = color_commits(ncolored, &ids, keep, drop, repo,
- progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
+ err = paint_commits(ncolored, &ids, nhead + ntail,
+ keep, drop, skip, repo, progress_cb, progress_arg, rl,
+ cancel_cb, cancel_arg);
if (err)
goto done;
@@ -1394,18 +1383,22 @@ findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
err = got_error_from_errno("calloc");
goto done;
}
- arg.idx = 0;
+ arg.idx = -1;
+ arg.skip = skip;
+ arg.drop = drop;
err = got_object_idset_for_each(keep, append_id, &arg);
if (err) {
free(arg.array);
goto done;
}
*res = arg.array;
- *nres = nkeep;
+ *nres = arg.idx + 1;
}
done:
got_object_idset_free(keep);
got_object_idset_free(drop);
+ if (skip)
+ got_object_idset_free(skip);
got_object_id_queue_free(&ids);
return err;
}