"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: findtwixt() speedup
To:
ori@eigenstate.org
Cc:
gameoftrees@openbsd.org
Date:
Wed, 13 Apr 2022 16:29:27 +0200

Download raw body.

Thread
On Mon, Apr 11, 2022 at 05:30:06PM -0400, ori@eigenstate.org wrote:
> Quoth Stefan Sperling <stsp@stsp.name>:
> > I have been looking for ways to speed up findtwixt().
> 
> You may be interested in:
> 
> https://git.9front.org/plan9front/plan9front/2e47badb88312c5c045a8042dc2ef80148e5ab47/commit.html
> 
> It also cleans up a few edge cases.

Thanks a lot for pointing this out!
I have implemented Michael's idea in the patch below.

This approach is indeed much better than what I came up with and results
in a similar speed improvement in this simple case test:
  gotadmin pack -a -x 0.68 main

One problem remains, which is also present in my previous patch:

We will potentially paint many commits as drop/skip during 'got send'
because every reference sent by the server needs to be traced through
history until a common part of the graph has been reached.
I don't see a way of avoiding this yet, except perhaps trying to make
smart guesses based on the timestamps of commits. But for correctness
it is necessary to rely on the graph structure only.

However, in practice this should only be an issue on the client side,
when sending to a server which is hosting a lot of branches and tags.
A server could enforce a limit on the number of references which can
be sent in a one batch to prevent the resulting overhead when a client
attempts to upload too many branches at once.

-----------------------------------------------
commit 67955a1c1de114d2ba12ee7552e3defc9bc2a21c (color-skip, noel/color-skip)
from: Stefan Sperling <stsp@stsp.name>
date: Wed Apr 13 14:02:15 2022 UTC
 
 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.
 
diff b7e7eabe1aac7bd2e278af7dd0b2e9a577514006 5675b775be799fc06bd0cb1dba27f4463b90322e
blob - b52856ef9a6f270f64d7a55d2f968ff3d9040061
blob + 2dd68b83aa81441b2112e65e2cf2b1a878c279f1
--- lib/pack_create.c
+++ 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;
@@ -1100,14 +1104,27 @@ 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 *
+paint_commit(struct got_object_qid *qid, int color)
+{
+	if (color < 0 || color >= nitems(findtwixt_colors))
+		return got_error(GOT_ERR_RANGE);
+
+	qid->data = (void *)&findtwixt_colors[color];
+	return NULL;
+}
+
+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)
 {
@@ -1119,77 +1136,14 @@ queue_commit_id(struct got_object_id_queue *ids, struc
 		return err;
 
 	STAILQ_INSERT_TAIL(ids, qid, entry);
-	qid->data = (void *)&findtwixt_colors[color];
-	return NULL;
+	return paint_commit(qid, color);
 }
 
-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)
-{
-	const struct got_error *err = NULL;
-	struct got_commit_object *commit;
-	const struct got_object_id_queue *parents;
-	struct got_object_id_queue ids;
-	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;
-}
-
 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 *
@@ -1197,11 +1151,14 @@ append_id(struct got_object_id *id, void *data, void *
 {
 	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;
 }
 
@@ -1237,18 +1194,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);
@@ -1257,71 +1216,94 @@ color_commits(int *ncolored, struct got_object_id_queu
 		}
 
 		qid = STAILQ_FIRST(ids);
-		qcolor = *((int *)qid->data);
+		STAILQ_REMOVE_HEAD(ids, entry);
+		nqueued--;
+		color = *((int *)qid->data);
+		if (color == COLOR_SKIP)
+			nskip--;
 
-		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;
-
-		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);
+		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;
-		} 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);
+				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;
-
-			err = got_object_open_as_commit(&commit, repo, qid->id);
-			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;
@@ -1337,7 +1319,7 @@ findtwixt(struct got_object_id ***res, int *nres, int 
 {
 	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);
@@ -1355,6 +1337,12 @@ findtwixt(struct got_object_id ***res, int *nres, int 
 		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)
@@ -1373,8 +1361,9 @@ findtwixt(struct got_object_id ***res, int *nres, int 
 			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;
 
@@ -1386,18 +1375,22 @@ findtwixt(struct got_object_id ***res, int *nres, int 
 			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;
 }