diff options
| author | lhchavez <lhchavez@lhchavez.com> | 2021-01-05 17:20:27 -0800 |
|---|---|---|
| committer | lhchavez <lhchavez@lhchavez.com> | 2021-03-10 07:09:47 -0800 |
| commit | 248606ebb0906076367fcfce9574f522f818c26f (patch) | |
| tree | a3d349ff8cc13ca5fc3a432c9967703f26a63328 /src/odb.c | |
| parent | 4f4b1139d23a7b38cceb9d83acbfaf73151f522f (diff) | |
| download | libgit2-248606ebb0906076367fcfce9574f522f818c26f.tar.gz | |
commit-graph: Use the commit-graph in revwalks
This change makes revwalks a bit faster by using the `commit-graph` file
(if present). This is thanks to the `commit-graph` allow much faster
parsing of the commit information by requiring near-zero I/O (aside from
reading a few dozen bytes off of a `mmap(2)`-ed file) for each commit,
instead of having to read the ODB, inflate the commit, and parse it.
This is done by modifying `git_commit_list_parse()` and letting it use
the ODB-owned commit-graph file.
Part of: #5757
Diffstat (limited to 'src/odb.c')
| -rw-r--r-- | src/odb.c | 74 |
1 files changed, 74 insertions, 0 deletions
@@ -465,6 +465,13 @@ int git_odb_new(git_odb **out) git__free(db); return -1; } + if (git_buf_init(&db->objects_dir, 0) < 0) { + git_vector_free(&db->backends); + git_cache_dispose(&db->own_cache); + git_mutex_free(&db->lock); + git__free(db); + return -1; + } *out = db; GIT_REFCOUNT_INC(db); @@ -612,6 +619,17 @@ int git_odb__add_default_backends( git_mutex_unlock(&db->lock); #endif + if (git_mutex_lock(&db->lock) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); + return -1; + } + if (git_buf_len(&db->objects_dir) == 0 && git_buf_sets(&db->objects_dir, objects_dir) < 0) { + git_mutex_unlock(&db->lock); + git_odb_free(db); + return -1; + } + git_mutex_unlock(&db->lock); + /* add the loose object backend */ if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 || add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0) @@ -742,6 +760,8 @@ static void odb_free(git_odb *db) if (locked) git_mutex_unlock(&db->lock); + git_buf_dispose(&db->objects_dir); + git_commit_graph_free(db->cgraph); git_vector_free(&db->backends); git_cache_dispose(&db->own_cache); git_mutex_free(&db->lock); @@ -786,6 +806,53 @@ static int odb_exists_1( return (int)found; } +int git_odb__get_commit_graph(git_commit_graph_file **out, git_odb *db) +{ + int error = 0; + + if ((error = git_mutex_lock(&db->lock)) < 0) { + git_error_set(GIT_ERROR_ODB, "failed to acquire the db lock"); + return error; + } + if (!db->cgraph_checked) { + git_buf commit_graph_path = GIT_BUF_INIT; + git_commit_graph_file *cgraph = NULL; + + /* We only check once, no matter the result. */ + db->cgraph_checked = 1; + + if (git_buf_len(&db->objects_dir) == 0) { + /* + * This odb was not opened with an objects directory + * associated. Skip opening the commit graph. + */ + goto done; + } + + if ((error = git_buf_joinpath( + &commit_graph_path, + git_buf_cstr(&db->objects_dir), + "info/commit-graph")) + < 0) { + git_buf_dispose(&commit_graph_path); + goto done; + } + /* Best effort */ + error = git_commit_graph_open(&cgraph, git_buf_cstr(&commit_graph_path)); + git_buf_dispose(&commit_graph_path); + + if (error < 0) + goto done; + + db->cgraph = cgraph; + } + +done: + *out = db->cgraph; + git_mutex_unlock(&db->lock); + return 0; +} + static int odb_freshen_1( git_odb *db, const git_oid *id, @@ -1695,6 +1762,13 @@ int git_odb_refresh(struct git_odb *db) } } } + if (db->cgraph && git_commit_graph_needs_refresh(db->cgraph, NULL)) { + /* We just free the commit graph. The next time it is requested, it will be re-loaded. */ + git_commit_graph_free(db->cgraph); + db->cgraph = NULL; + } + /* Force a lazy re-check next time it is needed. */ + db->cgraph_checked = 0; git_mutex_unlock(&db->lock); return 0; |
