Commit 434025f31270ab0b50063ed81909d2c32c9f3e98

Stefan Sperling 2018-09-16T19:22:12

eliminate redundant cache search in got_object_open_as_commit()

diff --git a/lib/object.c b/lib/object.c
index 5d6b4ff..ef9404c 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -271,6 +271,51 @@ got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
 	return got_object_open(obj, repo, &id);
 }
 
+static const struct got_error *
+open_commit(struct got_commit_object **commit,
+    struct got_repository *repo, struct got_object *obj, int check_cache)
+{
+	const struct got_error *err = NULL;
+
+	if (check_cache) {
+		*commit = got_repo_get_cached_commit(repo, &obj->id);
+		if (*commit != NULL) {
+			(*commit)->refcnt++;
+			return NULL;
+		}
+	} else
+		*commit = NULL;
+
+	if (obj->type != GOT_OBJ_TYPE_COMMIT)
+		return got_error(GOT_ERR_OBJ_TYPE);
+
+	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+		struct got_pack *pack;
+		pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+		if (pack == NULL) {
+			err = got_repo_cache_pack(&pack, repo,
+			    obj->path_packfile, NULL);
+			if (err)
+				return err;
+		}
+		err = got_object_read_packed_commit_privsep(commit, obj, pack);
+	} else {
+		int fd;
+		err = open_loose_object(&fd, obj, repo);
+		if (err)
+			return err;
+		err = got_object_read_commit_privsep(commit, obj, fd, repo);
+		close(fd);
+	}
+
+	if (err == NULL) {
+		(*commit)->refcnt++;
+		err = got_repo_cache_commit(repo, &obj->id, *commit);
+	}
+
+	return err;
+}
+
 const struct got_error *
 got_object_open_as_commit(struct got_commit_object **commit,
     struct got_repository *repo, struct got_object_id *id)
@@ -292,13 +337,20 @@ got_object_open_as_commit(struct got_commit_object **commit,
 		goto done;
 	}
 
-	err = got_object_commit_open(commit, repo, obj);
+	err = open_commit(commit, repo, obj, 0);
 done:
 	got_object_close(obj);
 	return err;
 }
 
 const struct got_error *
+got_object_commit_open(struct got_commit_object **commit,
+    struct got_repository *repo, struct got_object *obj)
+{
+	return open_commit(commit, repo, obj, 1);
+}
+
+const struct got_error *
 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
 {
 	const struct got_error *err = NULL;
@@ -319,48 +371,6 @@ got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
 }
 
 const struct got_error *
-got_object_commit_open(struct got_commit_object **commit,
-    struct got_repository *repo, struct got_object *obj)
-{
-	const struct got_error *err = NULL;
-
-	*commit = got_repo_get_cached_commit(repo, &obj->id);
-	if (*commit != NULL) {
-		(*commit)->refcnt++;
-		return NULL;
-	}
-
-	if (obj->type != GOT_OBJ_TYPE_COMMIT)
-		return got_error(GOT_ERR_OBJ_TYPE);
-
-	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
-		struct got_pack *pack;
-		pack = got_repo_get_cached_pack(repo, obj->path_packfile);
-		if (pack == NULL) {
-			err = got_repo_cache_pack(&pack, repo,
-			    obj->path_packfile, NULL);
-			if (err)
-				return err;
-		}
-		err = got_object_read_packed_commit_privsep(commit, obj, pack);
-	} else {
-		int fd;
-		err = open_loose_object(&fd, obj, repo);
-		if (err)
-			return err;
-		err = got_object_read_commit_privsep(commit, obj, fd, repo);
-		close(fd);
-	}
-
-	if (err == NULL) {
-		(*commit)->refcnt++;
-		err = got_repo_cache_commit(repo, &obj->id, *commit);
-	}
-
-	return err;
-}
-
-const struct got_error *
 got_object_tree_open(struct got_tree_object **tree,
     struct got_repository *repo, struct got_object *obj)
 {