Skip to content

Commit e85e5dd

Browse files
pks-tgitster
authored andcommitted
refs/files: use transactions to delete references
In the `files_delete_refs()` callback function of the files backend we implement deletion of references. This is done in two steps: 1. We lock the packed-refs file and delete all references from it in a single transaction. 2. We delete all loose references via separate calls to `refs_delete_ref()`. These steps essentially duplicate the logic around locking and deletion order that we already have in the transactional interfaces, where we do know to lock and evict references from the packed-refs file. Despite the fact that we duplicate the logic, it's also less efficient than if we used a single generic transaction: - The transactional interface knows to skip locking of the packed refs in case they don't contain any of the refs which are about to be deleted. - We end up creating N+1 separate reference transactions, one for the packed-refs file and N for the individual loose references. Refactor the code to instead delete references via a single transaction. As we don't assert the expected old object ID this is equivalent to the previous behaviour, and we already do the same in the packed-refs backend. Despite the fact that the result is simpler to reason about, this change also results in improved performance. The following benchmarks have been executed in linux.git: ``` $ hyperfine -n '{rev}, packed={packed} refcount={refcount}' \ -L packed true,false -L refcount 1,1000 -L rev master,pks-ref-store-generic-delete-refs \ --setup 'git -C /home/pks/Development/git switch --detach {rev} && make -C /home/pks/Development/git -j17' \ --prepare 'printf "create refs/heads/new-branch-%d HEAD\n" $(seq {refcount}) | git -C /home/pks/Reproduction/linux.git update-ref --stdin && if test {packed} = true; then git pack-refs --all; fi' \ --warmup=10 \ '/home/pks/Development/git/bin-wrappers/git -C /home/pks/Reproduction/linux.git branch -d new-branch-{1..{refcount}}' Benchmark 1: master packed=true refcount=1 Time (mean ± σ): 7.8 ms ± 1.6 ms [User: 3.4 ms, System: 4.4 ms] Range (min … max): 5.5 ms … 11.0 ms 120 runs Benchmark 2: master packed=false refcount=1 Time (mean ± σ): 7.0 ms ± 1.1 ms [User: 3.2 ms, System: 3.8 ms] Range (min … max): 5.7 ms … 9.8 ms 180 runs Benchmark 3: master packed=true refcount=1000 Time (mean ± σ): 283.8 ms ± 5.2 ms [User: 45.7 ms, System: 231.5 ms] Range (min … max): 276.7 ms … 291.6 ms 10 runs Benchmark 4: master packed=false refcount=1000 Time (mean ± σ): 284.4 ms ± 5.3 ms [User: 44.2 ms, System: 233.6 ms] Range (min … max): 277.1 ms … 293.3 ms 10 runs Benchmark 5: generic-delete-refs packed=true refcount=1 Time (mean ± σ): 6.2 ms ± 1.8 ms [User: 2.3 ms, System: 3.9 ms] Range (min … max): 4.1 ms … 12.2 ms 142 runs Benchmark 6: generic-delete-refs packed=false refcount=1 Time (mean ± σ): 7.1 ms ± 1.4 ms [User: 2.8 ms, System: 4.3 ms] Range (min … max): 4.2 ms … 10.8 ms 157 runs Benchmark 7: generic-delete-refs packed=true refcount=1000 Time (mean ± σ): 198.9 ms ± 1.7 ms [User: 29.5 ms, System: 165.7 ms] Range (min … max): 196.1 ms … 201.4 ms 10 runs Benchmark 8: generic-delete-refs packed=false refcount=1000 Time (mean ± σ): 199.7 ms ± 7.8 ms [User: 32.2 ms, System: 163.2 ms] Range (min … max): 193.8 ms … 220.7 ms 10 runs Summary generic-delete-refs packed=true refcount=1 ran 1.14 ± 0.37 times faster than master packed=false refcount=1 1.15 ± 0.40 times faster than generic-delete-refs packed=false refcount=1 1.26 ± 0.44 times faster than master packed=true refcount=1 32.24 ± 9.17 times faster than generic-delete-refs packed=true refcount=1000 32.36 ± 9.29 times faster than generic-delete-refs packed=false refcount=1000 46.00 ± 13.10 times faster than master packed=true refcount=1000 46.10 ± 13.13 times faster than master packed=false refcount=1000 ``` Especially in the case where we have many references we can see a clear performance speedup of nearly 30%. This is in contrast to the stated objecive in a27dcf8 (refs: make delete_refs() virtual, 2016-09-04), where the virtual `delete_refs()` function was introduced with the intent to speed things up rather than making things slower. So it seems like we have outlived the need for a virtual function. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b02c764 commit e85e5dd

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

refs/files-backend.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,49 +1268,51 @@ static int files_pack_refs(struct ref_store *ref_store,
12681268
static int files_delete_refs(struct ref_store *ref_store, const char *msg,
12691269
struct string_list *refnames, unsigned int flags)
12701270
{
1271-
struct files_ref_store *refs =
1272-
files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1271+
struct ref_transaction *transaction;
12731272
struct strbuf err = STRBUF_INIT;
1274-
int i, result = 0;
1273+
struct string_list_item *item;
1274+
int ret = 0, failures = 0;
12751275

12761276
if (!refnames->nr)
12771277
return 0;
12781278

1279-
if (packed_refs_lock(refs->packed_ref_store, 0, &err))
1280-
goto error;
1281-
1282-
if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
1283-
packed_refs_unlock(refs->packed_ref_store);
1284-
goto error;
1279+
/*
1280+
* Since we don't check the references' old_oids, the
1281+
* individual updates can't fail, so we can pack all of the
1282+
* updates into a single transaction.
1283+
*/
1284+
transaction = ref_store_transaction_begin(ref_store, &err);
1285+
if (!transaction) {
1286+
ret = error("%s", err.buf);
1287+
goto out;
12851288
}
12861289

1287-
packed_refs_unlock(refs->packed_ref_store);
1288-
1289-
for (i = 0; i < refnames->nr; i++) {
1290-
const char *refname = refnames->items[i].string;
1291-
1292-
if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
1293-
result |= error(_("could not remove reference %s"), refname);
1290+
for_each_string_list_item(item, refnames) {
1291+
ret = ref_transaction_delete(transaction, item->string,
1292+
NULL, flags, msg, &err);
1293+
if (ret) {
1294+
warning(_("could not delete reference %s: %s"),
1295+
item->string, err.buf);
1296+
strbuf_reset(&err);
1297+
failures = 1;
1298+
}
12941299
}
12951300

1296-
strbuf_release(&err);
1297-
return result;
1298-
1299-
error:
1300-
/*
1301-
* If we failed to rewrite the packed-refs file, then it is
1302-
* unsafe to try to remove loose refs, because doing so might
1303-
* expose an obsolete packed value for a reference that might
1304-
* even point at an object that has been garbage collected.
1305-
*/
1306-
if (refnames->nr == 1)
1307-
error(_("could not delete reference %s: %s"),
1308-
refnames->items[0].string, err.buf);
1309-
else
1310-
error(_("could not delete references: %s"), err.buf);
1301+
ret = ref_transaction_commit(transaction, &err);
1302+
if (ret) {
1303+
if (refnames->nr == 1)
1304+
error(_("could not delete reference %s: %s"),
1305+
refnames->items[0].string, err.buf);
1306+
else
1307+
error(_("could not delete references: %s"), err.buf);
1308+
}
13111309

1310+
out:
1311+
if (!ret && failures)
1312+
ret = -1;
1313+
ref_transaction_free(transaction);
13121314
strbuf_release(&err);
1313-
return -1;
1315+
return ret;
13141316
}
13151317

13161318
/*

0 commit comments

Comments
 (0)