{"thread":{"id":"62467","subject":"[PATCH 00/10] refs: optimize ref format migrations","startedAt":"2024-11-08T09:35:04Z","lastAt":"2024-11-25T05:52:47Z","messageCount":42,"participants":["Patrick Steinhardt","karthik nayak","Junio C Hamano","Christian Couder","Kristoffer Haugsbakk"],"isPatch":true,"patchVersion":1,"patchTotal":10},"messages":[{"id":"506841","messageId":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":null,"subject":"[PATCH 00/10] refs: optimize ref format migrations","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:40Z","receivedAt":"2024-11-08T09:35:04Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nI have recently learned that ref format migrations can take a\nsignificant amount of time in the order of minutes when migrating\nmillions of refs. This is probably not entirely surprising: the initial\nfocus for the logic to migrate ref backends was mostly focussed on\ngetting the basic feature working, and I didn't yet invest any time into\noptimizing the code path at all. But I was still mildly surprised that\nthe migration of a couple million refs was taking minutes to finish.\n\nThis patch series thus optimizes how we migrate ref formats. This is\nmostly done by expanding upon the \"initial transaction\" semantics that\nwe already use for git-clone(1). These semantics allow us to assume that\nthe ref backend is completely empty and that there are no concurrent\nwriters, and thus we are free to perform certain optimizations that\nwouldn't have otherwise been possible. On the one hand this allows us to\ndrop needless collision checks. On the other hand, it also allows us to\nwrite regular refs directly into the \"packed-refs\" file when migrating\nfrom the \"reftable\" backend to the \"files\" backend.\n\nThis leads to some significant speedups. Migrating 1 million refs from\n\"files\" to \"reftable\":\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = origin/master)\n      Time (mean ± σ):      4.580 s ±  0.062 s    [User: 1.818 s, System: 2.746 s]\n      Range (min … max):    4.534 s …  4.743 s    10 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations)\n      Time (mean ± σ):     767.7 ms ±   9.5 ms    [User: 629.2 ms, System: 126.1 ms]\n      Range (min … max):   755.8 ms … 786.9 ms    10 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n        5.97 ± 0.11 times faster than migrate files:reftable (refcount = 1000000, revision = origin/master)\n\nAnd migrating from \"reftable\" to \"files:\n\n    Benchmark 1: migrate reftable:files (refcount = 1000000, revision = origin/master)\n      Time (mean ± σ):     35.409 s ±  0.302 s    [User: 5.061 s, System: 29.244 s]\n      Range (min … max):   35.055 s … 35.898 s    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations)\n      Time (mean ± σ):     855.9 ms ±  61.5 ms    [User: 646.7 ms, System: 187.1 ms]\n      Range (min … max):   830.0 ms … 1030.3 ms    10 runs\n\n    Summary\n      migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n       41.37 ± 2.99 times faster than migrate reftable:files (refcount = 1000000, revision = origin/master)\n\nThanks!\n\nPatrick\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\nPatrick Steinhardt (10):\n      refs: allow passing flags when setting up a transaction\n      refs/files: move logic to commit initial transaction\n      refs: introduce \"initial\" transaction flag\n      refs/files: support symbolic and root refs in initial transaction\n      refs: use \"initial\" transaction semantics to migrate refs\n      refs: skip collision checks in initial transactions\n      refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG`\n      reftable/writer: optimize allocations by using a scratch buffer\n      reftable/block: rename `block_writer::buf` variable\n      reftable/block: optimize allocations by using scratch buffer\n\n branch.c                  |   2 +-\n builtin/clone.c           |   4 +-\n builtin/fast-import.c     |   4 +-\n builtin/fetch.c           |   4 +-\n builtin/receive-pack.c    |   4 +-\n builtin/replace.c         |   2 +-\n builtin/tag.c             |   2 +-\n builtin/update-ref.c      |   4 +-\n refs.c                    |  70 ++++++-------\n refs.h                    |  45 +++++----\n refs/debug.c              |  13 ---\n refs/files-backend.c      | 244 +++++++++++++++++++++++++---------------------\n refs/packed-backend.c     |   8 --\n refs/refs-internal.h      |   2 +-\n refs/reftable-backend.c   |  14 +--\n reftable/block.c          |  33 +++----\n reftable/block.h          |   9 +-\n reftable/writer.c         |  23 +++--\n reftable/writer.h         |   1 +\n sequencer.c               |   6 +-\n t/helper/test-ref-store.c |   2 +-\n t/t1460-refs-migrate.sh   |   2 +-\n walker.c                  |   2 +-\n 23 files changed, 247 insertions(+), 253 deletions(-)\n---\nbase-commit: facbe4f633e4ad31e641f64617bc88074c659959\nchange-id: 20241108-pks-refs-optimize-migrations-6d0ceee4abb7\n\nBest regards,\n-- \nPatrick Steinhardt <ps@pks.im>\n\n"},{"id":"506842","messageId":"20241108-pks-refs-optimize-migrations-v1-1-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 01/10] refs: allow passing flags when setting up a transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:41Z","receivedAt":"2024-11-08T09:35:05Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Allow passing flags when setting up a transaction such that the\nbehaviour of the transaction itself can be altered. Adapt callers\naccordingly.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n branch.c               |  2 +-\n builtin/clone.c        |  2 +-\n builtin/fast-import.c  |  4 ++--\n builtin/fetch.c        |  4 ++--\n builtin/receive-pack.c |  4 ++--\n builtin/replace.c      |  2 +-\n builtin/tag.c          |  2 +-\n builtin/update-ref.c   |  4 ++--\n refs.c                 | 12 +++++++-----\n refs.h                 |  3 ++-\n refs/files-backend.c   | 11 +++++++----\n refs/refs-internal.h   |  1 +\n sequencer.c            |  6 +++---\n walker.c               |  2 +-\n 14 files changed, 33 insertions(+), 26 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 44977ad0aadbd40c878a0475ef2df2a20798936b..ebaa870c018747358255d3f150d136f0df447d5d 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -627,7 +627,7 @@ void create_branch(struct repository *r,\n \telse\n \t\tmsg = xstrfmt(\"branch: Created from %s\", start_name);\n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t\tref_transaction_update(transaction, ref.buf,\n \t\t\t\t\t&oid, forcing ? NULL : null_oid(),\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 59fcb317a68a77eee3ca96a60720c556e044c369..d963cc6eb5181e1af5bb29c07c3ee2fa24ad04ca 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -574,7 +574,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \tstruct strbuf err = STRBUF_INIT;\n \n \tt = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t&err);\n+\t\t\t\t\t0, &err);\n \tif (!t)\n \t\tdie(\"%s\", err.buf);\n \ndiff --git a/builtin/fast-import.c b/builtin/fast-import.c\nindex 76d5c20f141f42da988dddae0e549144d1379031..db82c37a06f05d25e0a8c34cbec055e6f9717191 100644\n--- a/builtin/fast-import.c\n+++ b/builtin/fast-import.c\n@@ -1634,7 +1634,7 @@ static int update_branch(struct branch *b)\n \t\t}\n \t}\n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, b->name, &b->oid, &old_oid,\n \t\t\t\t   NULL, NULL, 0, msg, &err) ||\n@@ -1669,7 +1669,7 @@ static void dump_tags(void)\n \tstruct ref_transaction *transaction;\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction) {\n \t\tfailure |= error(\"%s\", err.buf);\n \t\tgoto cleanup;\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex d9027e4dc9245a32a87c47d89f9a29fcfd42534c..68d291c7dd0a4fe805e3b006cc68b185481fba6b 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -669,7 +669,7 @@ static int s_update_ref(const char *action,\n \t */\n \tif (!transaction) {\n \t\ttransaction = our_transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t\t\t    &err);\n+\t\t\t\t\t\t\t\t\t    0, &err);\n \t\tif (!transaction) {\n \t\t\tret = STORE_REF_ERROR_OTHER;\n \t\t\tgoto out;\n@@ -1671,7 +1671,7 @@ static int do_fetch(struct transport *transport,\n \n \tif (atomic_fetch) {\n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\tretcode = -1;\n \t\t\tgoto cleanup;\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex ab5b20e39c5038cdf6e6f05f4d66278a9c1ac156..9d2c07f68dafb6fce87cb59abc1cbf27db9aae09 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -1849,7 +1849,7 @@ static void execute_commands_non_atomic(struct command *commands,\n \t\t\tcontinue;\n \n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\trp_error(\"%s\", err.buf);\n \t\t\tstrbuf_reset(&err);\n@@ -1878,7 +1878,7 @@ static void execute_commands_atomic(struct command *commands,\n \tconst char *reported_error = \"atomic push failure\";\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction) {\n \t\trp_error(\"%s\", err.buf);\n \t\tstrbuf_reset(&err);\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex a44f4e7ea9ff55dbd1f102aca5e5f5a046391328..a4eaadff91f1be107a8e0e25a701d2006ff8cac2 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref.buf, repl, &prev,\n \t\t\t\t   NULL, NULL, 0, NULL, &err) ||\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 93d10d59157d2ee1b41f90640bd162917f1eb162..3fa0ab111344dda477763a74d26076b6fb71a5ab 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -681,7 +681,7 @@ int cmd_tag(int argc,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref.buf, &object, &prev,\n \t\t\t\t   NULL, NULL,\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 8a98615dc8613a1be3b17c6d688ab9c0208ed003..670e7812d60ea88a20cd1c5cd113643095cb3be1 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -612,7 +612,7 @@ static void update_refs_stdin(void)\n \tint i, j;\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction)\n \t\tdie(\"%s\", err.buf);\n \n@@ -680,7 +680,7 @@ static void update_refs_stdin(void)\n \t\t\t */\n \t\t\tstate = cmd->state;\n \t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t\t  0, &err);\n \t\t\tif (!transaction)\n \t\t\t\tdie(\"%s\", err.buf);\n \ndiff --git a/refs.c b/refs.c\nindex 5f729ed4124f7fe8fa9df7fd1f1ce951abefc585..9effeb01eb45728514eab0ca92404ea8cf2158d9 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -918,7 +918,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, old_oid,\n \t\t\t\t   NULL, flags, msg, &err) ||\n@@ -1116,6 +1116,7 @@ int read_ref_at(struct ref_store *refs, const char *refname,\n }\n \n struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n+\t\t\t\t\t\t    unsigned int flags,\n \t\t\t\t\t\t    struct strbuf *err)\n {\n \tstruct ref_transaction *tr;\n@@ -1123,6 +1124,7 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n \n \tCALLOC_ARRAY(tr, 1);\n \ttr->ref_store = refs;\n+\ttr->flags = flags;\n \treturn tr;\n }\n \n@@ -1309,7 +1311,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret = 0;\n \n-\tt = ref_store_transaction_begin(refs, &err);\n+\tt = ref_store_transaction_begin(refs, 0, &err);\n \tif (!t ||\n \t    ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,\n \t\t\t\t   flags, msg, &err) ||\n@@ -2120,7 +2122,7 @@ int refs_update_symref(struct ref_store *refs, const char *ref,\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret = 0;\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref, NULL, NULL,\n \t\t\t\t   target, NULL, REF_NO_DEREF,\n@@ -2527,7 +2529,7 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \t * individual updates can't fail, so we can pack all of the\n \t * updates into a single transaction.\n \t */\n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction) {\n \t\tret = error(\"%s\", err.buf);\n \t\tgoto out;\n@@ -2833,7 +2835,7 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\ttransaction = ref_store_transaction_begin(new_refs, errbuf);\n+\ttransaction = ref_store_transaction_begin(new_refs, 0, errbuf);\n \tif (!transaction)\n \t\tgoto done;\n \ndiff --git a/refs.h b/refs.h\nindex 108dfc93b3428db491916ad8a55daea649d83ffd..9821f3e80d900b31c3dede489c2f415d968233d7 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -234,7 +234,7 @@ char *repo_default_branch_name(struct repository *r, int quiet);\n  *         struct strbuf err = STRBUF_INIT;\n  *         int ret = 0;\n  *\n- *         transaction = ref_store_transaction_begin(refs, &err);\n+ *         transaction = ref_store_transaction_begin(refs, 0, &err);\n  *         if (!transaction ||\n  *             ref_transaction_update(...) ||\n  *             ref_transaction_create(...) ||\n@@ -584,6 +584,7 @@ enum action_on_err {\n  * be freed by calling ref_transaction_free().\n  */\n struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n+\t\t\t\t\t\t    unsigned int flags,\n \t\t\t\t\t\t    struct strbuf *err);\n \n /*\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 0824c0b8a946909791da36ddb4171db4ad98913b..df61057c9f24972b72644407cc95057891338d96 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1252,7 +1252,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)\n \tif (check_refname_format(r->name, 0))\n \t\treturn;\n \n-\ttransaction = ref_store_transaction_begin(&refs->base, &err);\n+\ttransaction = ref_store_transaction_begin(&refs->base, 0, &err);\n \tif (!transaction)\n \t\tgoto cleanup;\n \tref_transaction_add_update(\n@@ -1396,7 +1396,8 @@ static int files_pack_refs(struct ref_store *ref_store,\n \tif (!should_pack_refs(refs, opts))\n \t\treturn 0;\n \n-\ttransaction = ref_store_transaction_begin(refs->packed_ref_store, &err);\n+\ttransaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction)\n \t\treturn -1;\n \n@@ -2867,7 +2868,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t */\n \t\t\tif (!packed_transaction) {\n \t\t\t\tpacked_transaction = ref_store_transaction_begin(\n-\t\t\t\t\t\trefs->packed_ref_store, err);\n+\t\t\t\t\t\trefs->packed_ref_store,\n+\t\t\t\t\t\ttransaction->flags, err);\n \t\t\t\tif (!packed_transaction) {\n \t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\t\tgoto cleanup;\n@@ -3174,7 +3176,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \t\t\t\t &affected_refnames))\n \t\tBUG(\"initial ref transaction called with existing refs\");\n \n-\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);\n+\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t\t transaction->flags, err);\n \tif (!packed_transaction) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 2313c830d8facaa17b0b4b073df0de958023062a..dbc6360c5a1d410c192e7eee1bffb1d423e1f9ee 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -193,6 +193,7 @@ struct ref_transaction {\n \tsize_t nr;\n \tenum ref_transaction_state state;\n \tvoid *backend_data;\n+\tunsigned int flags;\n };\n \n /*\ndiff --git a/sequencer.c b/sequencer.c\nindex 353d804999b88d5fd5dcf1254d80781f20e62f2e..287f4e5e8766327da6f31e87ce0c20f63b302b77 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -662,7 +662,7 @@ static int fast_forward_to(struct repository *r,\n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\",\n \t\t\t\t   to, unborn && !is_rebase_i(opts) ?\n@@ -1297,7 +1297,7 @@ int update_head_with_reflog(const struct commit *old_head,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  err);\n+\t\t\t\t\t\t  0, err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\", new_head,\n \t\t\t\t   old_head ? &old_head->object.oid : null_oid(),\n@@ -3890,7 +3890,7 @@ static int do_label(struct repository *r, const char *name, int len)\n \tstrbuf_addf(&ref_name, \"refs/rewritten/%.*s\", len, name);\n \tstrbuf_addf(&msg, \"rebase (label) '%.*s'\", len, name);\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction) {\n \t\terror(\"%s\", err.buf);\n \t\tret = -1;\ndiff --git a/walker.c b/walker.c\nindex 5ea7e5b392b2bd49f249a9acc8d7ce8779357e1b..7cc9dbea46d64d6bd3336025d640f284a6202157 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -290,7 +290,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \n \tif (write_ref) {\n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\terror(\"%s\", err.buf);\n \t\t\tgoto done;\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506843","messageId":"20241108-pks-refs-optimize-migrations-v1-2-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 02/10] refs/files: move logic to commit initial transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:42Z","receivedAt":"2024-11-08T09:35:06Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Move the logic to commit initial transactions such that we can start to\ncall it in `files_transaction_finish()` in a subsequent commit without\nrequiring a separate function declaration.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs/files-backend.c | 202 +++++++++++++++++++++++++--------------------------\n 1 file changed, 101 insertions(+), 101 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex df61057c9f24972b72644407cc95057891338d96..f37c805a34167b3749fbe724788180975abdae90 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2975,6 +2975,107 @@ static int parse_and_write_reflog(struct files_ref_store *refs,\n \treturn 0;\n }\n \n+static int ref_present(const char *refname, const char *referent UNUSED,\n+\t\t       const struct object_id *oid UNUSED,\n+\t\t       int flags UNUSED,\n+\t\t       void *cb_data)\n+{\n+\tstruct string_list *affected_refnames = cb_data;\n+\n+\treturn string_list_has_string(affected_refnames, refname);\n+}\n+\n+static int files_initial_transaction_commit(struct ref_store *ref_store,\n+\t\t\t\t\t    struct ref_transaction *transaction,\n+\t\t\t\t\t    struct strbuf *err)\n+{\n+\tstruct files_ref_store *refs =\n+\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t       \"initial_ref_transaction_commit\");\n+\tsize_t i;\n+\tint ret = 0;\n+\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n+\tstruct ref_transaction *packed_transaction = NULL;\n+\n+\tassert(err);\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tBUG(\"commit called for transaction that is not open\");\n+\n+\t/* Fail if a refname appears more than once in the transaction: */\n+\tfor (i = 0; i < transaction->nr; i++)\n+\t\tstring_list_append(&affected_refnames,\n+\t\t\t\t   transaction->updates[i]->refname);\n+\tstring_list_sort(&affected_refnames);\n+\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\t/*\n+\t * It's really undefined to call this function in an active\n+\t * repository or when there are existing references: we are\n+\t * only locking and changing packed-refs, so (1) any\n+\t * simultaneous processes might try to change a reference at\n+\t * the same time we do, and (2) any existing loose versions of\n+\t * the references that we are setting would have precedence\n+\t * over our values. But some remote helpers create the remote\n+\t * \"HEAD\" and \"master\" branches before calling this function,\n+\t * so here we really only check that none of the references\n+\t * that we are creating already exists.\n+\t */\n+\tif (refs_for_each_rawref(&refs->base, ref_present,\n+\t\t\t\t &affected_refnames))\n+\t\tBUG(\"initial ref transaction called with existing refs\");\n+\n+\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t\t transaction->flags, err);\n+\tif (!packed_transaction) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif ((update->flags & REF_HAVE_OLD) &&\n+\t\t    !is_null_oid(&update->old_oid))\n+\t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n+\t\tif (refs_verify_refname_available(&refs->base, update->refname,\n+\t\t\t\t\t\t  &affected_refnames, NULL,\n+\t\t\t\t\t\t  err)) {\n+\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\tgoto cleanup;\n+\t\t}\n+\n+\t\t/*\n+\t\t * Add a reference creation for this reference to the\n+\t\t * packed-refs transaction:\n+\t\t */\n+\t\tref_transaction_add_update(packed_transaction, update->refname,\n+\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t   &update->new_oid, &update->old_oid,\n+\t\t\t\t\t   NULL, NULL, NULL);\n+\t}\n+\n+\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t}\n+\n+\tpacked_refs_unlock(refs->packed_ref_store);\n+cleanup:\n+\tif (packed_transaction)\n+\t\tref_transaction_free(packed_transaction);\n+\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\tstring_list_clear(&affected_refnames, 0);\n+\treturn ret;\n+}\n+\n static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t    struct strbuf *err)\n@@ -3123,107 +3224,6 @@ static int files_transaction_abort(struct ref_store *ref_store,\n \treturn 0;\n }\n \n-static int ref_present(const char *refname, const char *referent UNUSED,\n-\t\t       const struct object_id *oid UNUSED,\n-\t\t       int flags UNUSED,\n-\t\t       void *cb_data)\n-{\n-\tstruct string_list *affected_refnames = cb_data;\n-\n-\treturn string_list_has_string(affected_refnames, refname);\n-}\n-\n-static int files_initial_transaction_commit(struct ref_store *ref_store,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n-\t\t\t       \"initial_ref_transaction_commit\");\n-\tsize_t i;\n-\tint ret = 0;\n-\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n-\tstruct ref_transaction *packed_transaction = NULL;\n-\n-\tassert(err);\n-\n-\tif (transaction->state != REF_TRANSACTION_OPEN)\n-\t\tBUG(\"commit called for transaction that is not open\");\n-\n-\t/* Fail if a refname appears more than once in the transaction: */\n-\tfor (i = 0; i < transaction->nr; i++)\n-\t\tstring_list_append(&affected_refnames,\n-\t\t\t\t   transaction->updates[i]->refname);\n-\tstring_list_sort(&affected_refnames);\n-\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\t/*\n-\t * It's really undefined to call this function in an active\n-\t * repository or when there are existing references: we are\n-\t * only locking and changing packed-refs, so (1) any\n-\t * simultaneous processes might try to change a reference at\n-\t * the same time we do, and (2) any existing loose versions of\n-\t * the references that we are setting would have precedence\n-\t * over our values. But some remote helpers create the remote\n-\t * \"HEAD\" and \"master\" branches before calling this function,\n-\t * so here we really only check that none of the references\n-\t * that we are creating already exists.\n-\t */\n-\tif (refs_for_each_rawref(&refs->base, ref_present,\n-\t\t\t\t &affected_refnames))\n-\t\tBUG(\"initial ref transaction called with existing refs\");\n-\n-\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n-\t\t\t\t\t\t\t transaction->flags, err);\n-\tif (!packed_transaction) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\tfor (i = 0; i < transaction->nr; i++) {\n-\t\tstruct ref_update *update = transaction->updates[i];\n-\n-\t\tif ((update->flags & REF_HAVE_OLD) &&\n-\t\t    !is_null_oid(&update->old_oid))\n-\t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n-\t\tif (refs_verify_refname_available(&refs->base, update->refname,\n-\t\t\t\t\t\t  &affected_refnames, NULL,\n-\t\t\t\t\t\t  err)) {\n-\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\tgoto cleanup;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Add a reference creation for this reference to the\n-\t\t * packed-refs transaction:\n-\t\t */\n-\t\tref_transaction_add_update(packed_transaction, update->refname,\n-\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n-\t\t\t\t\t   &update->new_oid, &update->old_oid,\n-\t\t\t\t\t   NULL, NULL, NULL);\n-\t}\n-\n-\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t}\n-\n-\tpacked_refs_unlock(refs->packed_ref_store);\n-cleanup:\n-\tif (packed_transaction)\n-\t\tref_transaction_free(packed_transaction);\n-\ttransaction->state = REF_TRANSACTION_CLOSED;\n-\tstring_list_clear(&affected_refnames, 0);\n-\treturn ret;\n-}\n-\n struct expire_reflog_cb {\n \treflog_expiry_should_prune_fn *should_prune_fn;\n \tvoid *policy_cb;\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506844","messageId":"20241108-pks-refs-optimize-migrations-v1-3-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 03/10] refs: introduce \"initial\" transaction flag","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:43Z","receivedAt":"2024-11-08T09:35:06Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"There are two different ways to commit a transaction:\n\n  - `ref_transaction_commit()` can be used to commit a regular\n    transaction and is what almost every caller wants.\n\n  - `initial_ref_transaction_commit()` can be used when it is known that\n    the ref store that the transaction is committed for is empty and\n    when there are no concurrent processes. This is used when cloning a\n    new repository.\n\nImplementing this via two separate functions has a couple of downsides.\nFirst, every reference backend needs to implement a separate callback\neven in the case where they don't special-case the initial transaction.\nSecond, backends are basically forced to reimplement the whole logic for\nhow to commit the transaction like the \"files\" backend does, even though\nbackends may wish to only tweak certain behaviour of a \"normal\" commit.\nThird, it is awkward that callers must never prepare the transaction as\nthis is somewhat different than how a transaction typically works.\n\nRefactor the code such that we instead mark initial transactions via a\nseparate flag when starting the transaction. This addresses all of the\nmentioned painpoints, where the most important part is that it will\nallow backends to have way more leeway in how exactly they want to\nhandle the initial transaction.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/clone.c         |  4 ++--\n refs.c                  | 10 +---------\n refs.h                  | 37 ++++++++++++++++++-------------------\n refs/debug.c            | 13 -------------\n refs/files-backend.c    | 16 ++++++++--------\n refs/packed-backend.c   |  8 --------\n refs/refs-internal.h    |  1 -\n refs/reftable-backend.c |  8 --------\n 8 files changed, 29 insertions(+), 68 deletions(-)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex d963cc6eb5181e1af5bb29c07c3ee2fa24ad04ca..8427ccec17e77b23ee1a7f526b80a52d79a71873 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -574,7 +574,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \tstruct strbuf err = STRBUF_INIT;\n \n \tt = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t0, &err);\n+\t\t\t\t\tREF_TRANSACTION_FLAG_INITIAL, &err);\n \tif (!t)\n \t\tdie(\"%s\", err.buf);\n \n@@ -586,7 +586,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \t\t\tdie(\"%s\", err.buf);\n \t}\n \n-\tif (initial_ref_transaction_commit(t, &err))\n+\tif (ref_transaction_commit(t, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tstrbuf_release(&err);\ndiff --git a/refs.c b/refs.c\nindex 9effeb01eb45728514eab0ca92404ea8cf2158d9..8b9dfc6173fd144fe9a172cb81cf33057ae31368 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2315,7 +2315,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \n \tret = refs->be->transaction_finish(refs, transaction, err);\n-\tif (!ret)\n+\tif (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n \t\trun_transaction_hook(transaction, \"committed\");\n \treturn ret;\n }\n@@ -2486,14 +2486,6 @@ int refs_reflog_expire(struct ref_store *refs,\n \t\t\t\t       cleanup_fn, policy_cb_data);\n }\n \n-int initial_ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err)\n-{\n-\tstruct ref_store *refs = transaction->ref_store;\n-\n-\treturn refs->be->initial_transaction_commit(refs, transaction, err);\n-}\n-\n void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,\n \t\t\t\t\t    ref_transaction_for_each_queued_update_fn cb,\n \t\t\t\t\t    void *cb_data)\ndiff --git a/refs.h b/refs.h\nindex 9821f3e80d900b31c3dede489c2f415d968233d7..024a370554e013d66febee635e4c0415ba061fe6 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -214,11 +214,9 @@ char *repo_default_branch_name(struct repository *r, int quiet);\n  *\n  *   Or\n  *\n- *   - Call `initial_ref_transaction_commit()` if the ref database is\n- *     known to be empty and have no other writers (e.g. during\n- *     clone). This is likely to be much faster than\n- *     `ref_transaction_commit()`. `ref_transaction_prepare()` should\n- *     *not* be called before `initial_ref_transaction_commit()`.\n+ *   - Call `ref_transaction_begin()` with REF_TRANSACTION_FLAG_INITIAL if the\n+ *     ref database is known to be empty and have no other writers (e.g. during\n+ *     clone). This is likely to be much faster than without the flag.\n  *\n  * - Then finally, call `ref_transaction_free()` to free the\n  *   `ref_transaction` data structure.\n@@ -579,6 +577,21 @@ enum action_on_err {\n \tUPDATE_REFS_QUIET_ON_ERR\n };\n \n+enum ref_transaction_flag {\n+\t/*\n+\t * The ref transaction is part of the initial creation of the ref store\n+\t * and can thus assume that the ref store is completely empty. This\n+\t * allows the backend to perform the transaction more efficiently by\n+\t * skipping certain checks.\n+\t *\n+\t * It is a bug to set this flag when there might be other processes\n+\t * accessing the repository or if there are existing references that\n+\t * might conflict with the ones being created. All old_oid values must\n+\t * either be absent or null_oid.\n+\t */\n+\tREF_TRANSACTION_FLAG_INITIAL = (1 << 0),\n+};\n+\n /*\n  * Begin a reference transaction.  The reference transaction must\n  * be freed by calling ref_transaction_free().\n@@ -798,20 +811,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n int ref_transaction_abort(struct ref_transaction *transaction,\n \t\t\t  struct strbuf *err);\n \n-/*\n- * Like ref_transaction_commit(), but optimized for creating\n- * references when originally initializing a repository (e.g., by \"git\n- * clone\"). It writes the new references directly to packed-refs\n- * without locking the individual references.\n- *\n- * It is a bug to call this function when there might be other\n- * processes accessing the repository or if there are existing\n- * references that might conflict with the ones being created. All\n- * old_oid values must either be absent or null_oid.\n- */\n-int initial_ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err);\n-\n /*\n  * Execute the given callback function for each of the reference updates which\n  * have been queued in the given transaction. `old_oid` and `new_oid` may be\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 45e2e784a0f8c49f492eaa9d371aa44f8c7916c3..cbac62c8a4f924580e80f106f87639daf77cef5c 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -118,18 +118,6 @@ static int debug_transaction_abort(struct ref_store *refs,\n \treturn res;\n }\n \n-static int debug_initial_transaction_commit(struct ref_store *refs,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)refs;\n-\tint res;\n-\ttransaction->ref_store = drefs->refs;\n-\tres = drefs->refs->be->initial_transaction_commit(drefs->refs,\n-\t\t\t\t\t\t\t  transaction, err);\n-\treturn res;\n-}\n-\n static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)\n {\n \tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n@@ -443,7 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.transaction_prepare = debug_transaction_prepare,\n \t.transaction_finish = debug_transaction_finish,\n \t.transaction_abort = debug_transaction_abort,\n-\t.initial_transaction_commit = debug_initial_transaction_commit,\n \n \t.pack_refs = debug_pack_refs,\n \t.rename_ref = debug_rename_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f37c805a34167b3749fbe724788180975abdae90..3ed18475a72aa4798d15b2912c37b4caabd47aac 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2781,6 +2781,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n \tassert(err);\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n+\t\tgoto cleanup;\n \tif (!transaction->nr)\n \t\tgoto cleanup;\n \n@@ -2985,13 +2987,10 @@ static int ref_present(const char *refname, const char *referent UNUSED,\n \treturn string_list_has_string(affected_refnames, refname);\n }\n \n-static int files_initial_transaction_commit(struct ref_store *ref_store,\n+static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t\t    struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n-\t\t\t       \"initial_ref_transaction_commit\");\n \tsize_t i;\n \tint ret = 0;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n@@ -2999,8 +2998,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \n \tassert(err);\n \n-\tif (transaction->state != REF_TRANSACTION_OPEN)\n-\t\tBUG(\"commit called for transaction that is not open\");\n+\tif (transaction->state != REF_TRANSACTION_PREPARED)\n+\t\tBUG(\"commit called for transaction that is not prepared\");\n \n \t/* Fail if a refname appears more than once in the transaction: */\n \tfor (i = 0; i < transaction->nr; i++)\n@@ -3063,7 +3062,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \t\tgoto cleanup;\n \t}\n \n-\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n+\tif (ref_transaction_commit(packed_transaction, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t}\n \n@@ -3091,6 +3090,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \tassert(err);\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n+\t\treturn files_transaction_finish_initial(refs, transaction, err);\n \tif (!transaction->nr) {\n \t\ttransaction->state = REF_TRANSACTION_CLOSED;\n \t\treturn 0;\n@@ -3617,7 +3618,6 @@ struct ref_storage_be refs_be_files = {\n \t.transaction_prepare = files_transaction_prepare,\n \t.transaction_finish = files_transaction_finish,\n \t.transaction_abort = files_transaction_abort,\n-\t.initial_transaction_commit = files_initial_transaction_commit,\n \n \t.pack_refs = files_pack_refs,\n \t.rename_ref = files_rename_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex 07c57fd541a5039d5fcb93d9bf78e1916f67b219..794471de60c78494f1f2b0e9de013422e3e7625d 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -1730,13 +1730,6 @@ static int packed_transaction_finish(struct ref_store *ref_store,\n \treturn ret;\n }\n \n-static int packed_initial_transaction_commit(struct ref_store *ref_store UNUSED,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\treturn ref_transaction_commit(transaction, err);\n-}\n-\n static int packed_pack_refs(struct ref_store *ref_store UNUSED,\n \t\t\t    struct pack_refs_opts *pack_opts UNUSED)\n {\n@@ -1769,7 +1762,6 @@ struct ref_storage_be refs_be_packed = {\n \t.transaction_prepare = packed_transaction_prepare,\n \t.transaction_finish = packed_transaction_finish,\n \t.transaction_abort = packed_transaction_abort,\n-\t.initial_transaction_commit = packed_initial_transaction_commit,\n \n \t.pack_refs = packed_pack_refs,\n \t.rename_ref = NULL,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex dbc6360c5a1d410c192e7eee1bffb1d423e1f9ee..33335d54c9162755c70174093017439c0018f36d 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -666,7 +666,6 @@ struct ref_storage_be {\n \tref_transaction_prepare_fn *transaction_prepare;\n \tref_transaction_finish_fn *transaction_finish;\n \tref_transaction_abort_fn *transaction_abort;\n-\tref_transaction_commit_fn *initial_transaction_commit;\n \n \tpack_refs_fn *pack_refs;\n \trename_ref_fn *rename_ref;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 38eb14d591ec0c7c221b6b0f7483e547748e7f1c..8e914afc817f198bed3199630b430e179cabc740 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1490,13 +1490,6 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,\n \treturn ret;\n }\n \n-static int reftable_be_initial_transaction_commit(struct ref_store *ref_store UNUSED,\n-\t\t\t\t\t\t  struct ref_transaction *transaction,\n-\t\t\t\t\t\t  struct strbuf *err)\n-{\n-\treturn ref_transaction_commit(transaction, err);\n-}\n-\n static int reftable_be_pack_refs(struct ref_store *ref_store,\n \t\t\t\t struct pack_refs_opts *opts)\n {\n@@ -2490,7 +2483,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.transaction_prepare = reftable_be_transaction_prepare,\n \t.transaction_finish = reftable_be_transaction_finish,\n \t.transaction_abort = reftable_be_transaction_abort,\n-\t.initial_transaction_commit = reftable_be_initial_transaction_commit,\n \n \t.pack_refs = reftable_be_pack_refs,\n \t.rename_ref = reftable_be_rename_ref,\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506845","messageId":"20241108-pks-refs-optimize-migrations-v1-5-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 05/10] refs: use \"initial\" transaction semantics to migrate refs","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:45Z","receivedAt":"2024-11-08T09:35:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Until now, we couldn't use \"initial\" transaction semantics to migrate\nrefs because the \"files\" backend only supported writing regular refs via\nthe initial transaction because it simply mapped the transaction to a\n\"packed-refs\" transaction. But with the preceding commit, the \"files\"\nbackend has learned to also write symbolic and root refs in the initial\ntransaction by creating a second transaction for all refs that need to\nbe written as loose refs.\n\nAdapt the code to migrate refs to commit the transaction as an initial\ntransaction. This results in a signiticant speedup when migrating many\nrefs:\n\n    Benchmark 1: migrate reftable:files (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):      3.247 s ±  0.034 s    [User: 0.485 s, System: 2.722 s]\n      Range (min … max):    3.216 s …  3.309 s    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):     453.6 ms ±   1.9 ms    [User: 214.6 ms, System: 230.5 ms]\n      Range (min … max):   451.5 ms … 456.4 ms    10 runs\n\n    Summary\n      migrate reftable:files (refcount = 100000, revision = HEAD) ran\n        7.16 ± 0.08 times faster than migrate reftable:files (refcount = 100000, revision = HEAD~)\n\nAs the reftable backend doesn't (yet) special-case initial transactions\nthere is no comparable speedup for that backend.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c                  | 10 ++--------\n t/t1460-refs-migrate.sh |  2 +-\n 2 files changed, 3 insertions(+), 9 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 8b9dfc6173fd144fe9a172cb81cf33057ae31368..0f10c565bbb4e0d91210c52a3221a224e4264d28 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2827,7 +2827,8 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\ttransaction = ref_store_transaction_begin(new_refs, 0, errbuf);\n+\ttransaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL,\n+\t\t\t\t\t\t  errbuf);\n \tif (!transaction)\n \t\tgoto done;\n \n@@ -2852,13 +2853,6 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\t/*\n-\t * TODO: we might want to migrate to `initial_ref_transaction_commit()`\n-\t * here, which is more efficient for the files backend because it would\n-\t * write new refs into the packed-refs file directly. At this point,\n-\t * the files backend doesn't handle pseudo-refs and symrefs correctly\n-\t * though, so this requires some more work.\n-\t */\n \tret = ref_transaction_commit(transaction, errbuf);\n \tif (ret < 0)\n \t\tgoto done;\ndiff --git a/t/t1460-refs-migrate.sh b/t/t1460-refs-migrate.sh\nindex f7c0783d30ccd61b0fee67c115193b42bb0e2c77..b90b38a87f7bb905afeeceb4f9a3bfc8b772e16a 100755\n--- a/t/t1460-refs-migrate.sh\n+++ b/t/t1460-refs-migrate.sh\n@@ -237,7 +237,7 @@ test_expect_success 'migrating from reftable format deletes backend files' '\n \ttest_path_is_missing repo/.git/reftable &&\n \techo \"ref: refs/heads/main\" >expect &&\n \ttest_cmp expect repo/.git/HEAD &&\n-\ttest_path_is_file repo/.git/refs/heads/main\n+\ttest_path_is_file repo/.git/packed-refs\n '\n \n test_done\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506846","messageId":"20241108-pks-refs-optimize-migrations-v1-6-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 06/10] refs: skip collision checks in initial transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:46Z","receivedAt":"2024-11-08T09:35:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Reference transactions use `refs_verify_refname_available()` to check\nfor colliding references. This check consists of two parts:\n\n  - Checks for whether multiple ref updates in the same transaction\n    conflict with each other.\n\n  - Checks for whether existing refs conflict with any refs part of the\n    transaction.\n\nWhile we generally cannot avoid the first check, the second check is\nsuperfluous in cases where the transaction is an initial one in an\notherwise empty ref store. The check results in multiple ref reads as\nwell as the creation of a ref iterator for every ref we're checking,\nwhich adds up quite fast when performing the check for many refs.\n\nIntroduce a new flag that allows us to skip this check and wire it up in\nsuch that the backends pass it when running an initial transaction. This\nleads to significant speedups when migrating ref storage backends. From\n\"files\" to \"reftable\":\n\n    Benchmark 1: migrate files:reftable (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):     472.4 ms ±   6.7 ms    [User: 175.9 ms, System: 285.2 ms]\n      Range (min … max):   463.5 ms … 483.2 ms    10 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):      86.1 ms ±   1.9 ms    [User: 67.9 ms, System: 16.0 ms]\n      Range (min … max):    82.9 ms …  90.9 ms    29 runs\n\n    Summary\n      migrate files:reftable (refcount = 100000, revision = HEAD) ran\n        5.48 ± 0.15 times faster than migrate files:reftable (refcount = 100000, revision = HEAD~)\n\nAnd from \"reftable\" to \"files\":\n\n    Benchmark 1: migrate reftable:files (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):     452.7 ms ±   3.4 ms    [User: 209.9 ms, System: 235.4 ms]\n      Range (min … max):   445.9 ms … 457.5 ms    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):      95.2 ms ±   2.2 ms    [User: 73.6 ms, System: 20.6 ms]\n      Range (min … max):    91.7 ms … 100.8 ms    28 runs\n\n    Summary\n      migrate reftable:files (refcount = 100000, revision = HEAD) ran\n        4.76 ± 0.11 times faster than migrate reftable:files (refcount = 100000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c                    | 37 +++++++++++++++++++++----------------\n refs.h                    |  5 ++++-\n refs/files-backend.c      | 13 ++++++-------\n refs/reftable-backend.c   |  6 ++++--\n t/helper/test-ref-store.c |  2 +-\n 5 files changed, 36 insertions(+), 27 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 0f10c565bbb4e0d91210c52a3221a224e4264d28..d690eb19b3fd7083a5309deb98738547e4f48040 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2324,6 +2324,7 @@ int refs_verify_refname_available(struct ref_store *refs,\n \t\t\t\t  const char *refname,\n \t\t\t\t  const struct string_list *extras,\n \t\t\t\t  const struct string_list *skip,\n+\t\t\t\t  int initial_transaction,\n \t\t\t\t  struct strbuf *err)\n {\n \tconst char *slash;\n@@ -2332,8 +2333,6 @@ int refs_verify_refname_available(struct ref_store *refs,\n \tstruct strbuf referent = STRBUF_INIT;\n \tstruct object_id oid;\n \tunsigned int type;\n-\tstruct ref_iterator *iter;\n-\tint ok;\n \tint ret = -1;\n \n \t/*\n@@ -2363,7 +2362,8 @@ int refs_verify_refname_available(struct ref_store *refs,\n \t\tif (skip && string_list_has_string(skip, dirname.buf))\n \t\t\tcontinue;\n \n-\t\tif (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n+\t\tif (!initial_transaction &&\n+\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n \t\t\t\t       &type, &ignore_errno)) {\n \t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n \t\t\t\t    dirname.buf, refname);\n@@ -2388,21 +2388,26 @@ int refs_verify_refname_available(struct ref_store *refs,\n \tstrbuf_addstr(&dirname, refname + dirname.len);\n \tstrbuf_addch(&dirname, '/');\n \n-\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n-\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n-\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n-\t\tif (skip &&\n-\t\t    string_list_has_string(skip, iter->refname))\n-\t\t\tcontinue;\n+\tif (!initial_transaction) {\n+\t\tstruct ref_iterator *iter;\n+\t\tint ok;\n \n-\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n-\t\t\t    iter->refname, refname);\n-\t\tref_iterator_abort(iter);\n-\t\tgoto cleanup;\n-\t}\n+\t\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n+\t\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n+\t\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n+\t\t\tif (skip &&\n+\t\t\t    string_list_has_string(skip, iter->refname))\n+\t\t\t\tcontinue;\n \n-\tif (ok != ITER_DONE)\n-\t\tBUG(\"error while iterating over references\");\n+\t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n+\t\t\t\t    iter->refname, refname);\n+\t\t\tref_iterator_abort(iter);\n+\t\t\tgoto cleanup;\n+\t\t}\n+\n+\t\tif (ok != ITER_DONE)\n+\t\t\tBUG(\"error while iterating over references\");\n+\t}\n \n \textra_refname = find_descendant_ref(dirname.buf, extras, skip);\n \tif (extra_refname)\ndiff --git a/refs.h b/refs.h\nindex 024a370554e013d66febee635e4c0415ba061fe6..980bd20cf24e15144aeff991eeba8b27a936d386 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -101,13 +101,16 @@ int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,\n  * both \"foo\" and with \"foo/bar/baz\" but not with \"foo/bar\" or\n  * \"foo/barbados\".\n  *\n+ * If `initial_transaction` is truish, then all collision checks with\n+ * preexisting refs are skipped.\n+ *\n  * extras and skip must be sorted.\n  */\n-\n int refs_verify_refname_available(struct ref_store *refs,\n \t\t\t\t  const char *refname,\n \t\t\t\t  const struct string_list *extras,\n \t\t\t\t  const struct string_list *skip,\n+\t\t\t\t  int initial_transaction,\n \t\t\t\t  struct strbuf *err);\n \n int refs_ref_exists(struct ref_store *refs, const char *refname);\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 116d4259697b20583cb2db34ed47025e8781cd42..d27806c02c272f8bddc789b509e3c3c7af4f75aa 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -706,7 +706,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t * reason to expect this error to be transitory.\n \t\t */\n \t\tif (refs_verify_refname_available(&refs->base, refname,\n-\t\t\t\t\t\t  extras, NULL, err)) {\n+\t\t\t\t\t\t  extras, NULL, 0, err)) {\n \t\t\tif (mustexist) {\n \t\t\t\t/*\n \t\t\t\t * To the user the relevant error is\n@@ -813,7 +813,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n \t\t\t\tif (refs_verify_refname_available(\n \t\t\t\t\t\t    &refs->base, refname,\n-\t\t\t\t\t\t    extras, NULL, err)) {\n+\t\t\t\t\t\t    extras, NULL, 0, err)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * The error message set by\n \t\t\t\t\t * verify_refname_available() is OK.\n@@ -850,7 +850,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t */\n \t\tif (refs_verify_refname_available(\n \t\t\t\t    refs->packed_ref_store, refname,\n-\t\t\t\t    extras, NULL, err)) {\n+\t\t\t\t    extras, NULL, 0, err)) {\n \t\t\tret = TRANSACTION_NAME_CONFLICT;\n \t\t\tgoto error_return;\n \t\t}\n@@ -1159,7 +1159,7 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,\n \t */\n \tif (is_null_oid(&lock->old_oid) &&\n \t    refs_verify_refname_available(refs->packed_ref_store, refname,\n-\t\t\t\t\t  NULL, NULL, err))\n+\t\t\t\t\t  NULL, NULL, 0, err))\n \t\tgoto error_return;\n \n \tlock->ref_name = xstrdup(refname);\n@@ -1538,7 +1538,7 @@ static int refs_rename_ref_available(struct ref_store *refs,\n \n \tstring_list_insert(&skip, old_refname);\n \tok = !refs_verify_refname_available(refs, new_refname,\n-\t\t\t\t\t    NULL, &skip, &err);\n+\t\t\t\t\t    NULL, &skip, 0, &err);\n \tif (!ok)\n \t\terror(\"%s\", err.buf);\n \n@@ -3043,8 +3043,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n \n \t\tif (refs_verify_refname_available(&refs->base, update->refname,\n-\t\t\t\t\t\t  &affected_refnames, NULL,\n-\t\t\t\t\t\t  err)) {\n+\t\t\t\t\t\t  &affected_refnames, NULL, 1, err)) {\n \t\t\tret = TRANSACTION_NAME_CONFLICT;\n \t\t\tgoto cleanup;\n \t\t}\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 8e914afc817f198bed3199630b430e179cabc740..bbc779ab410b41f00759a3a41a76dd708f115c90 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1097,7 +1097,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t * at a later point.\n \t\t\t */\n \t\t\tret = refs_verify_refname_available(ref_store, u->refname,\n-\t\t\t\t\t\t\t    &affected_refnames, NULL, err);\n+\t\t\t\t\t\t\t    &affected_refnames, NULL,\n+\t\t\t\t\t\t\t    transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n+\t\t\t\t\t\t\t    err);\n \t\t\tif (ret < 0)\n \t\t\t\tgoto done;\n \n@@ -1584,7 +1586,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tif (arg->delete_old)\n \t\tstring_list_insert(&skip, arg->oldname);\n \tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n-\t\t\t\t\t    NULL, &skip, &errbuf);\n+\t\t\t\t\t    NULL, &skip, 0, &errbuf);\n \tif (ret < 0) {\n \t\terror(\"%s\", errbuf.buf);\n \t\tgoto done;\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex 65346dee551ccd781a88786f0c8465f60b286cde..240f6fc29d7f1bb20658deee467bcb46ac3407b2 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -199,7 +199,7 @@ static int cmd_verify_ref(struct ref_store *refs, const char **argv)\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret;\n \n-\tret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);\n+\tret = refs_verify_refname_available(refs, refname, NULL, NULL, 0, &err);\n \tif (err.len)\n \t\tputs(err.buf);\n \treturn ret;\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506847","messageId":"20241108-pks-refs-optimize-migrations-v1-4-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 04/10] refs/files: support symbolic and root refs in initial transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:44Z","receivedAt":"2024-11-08T09:35:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The \"files\" backend has implemented special logic when committing\nthe first transactions in an otherwise empty ref store: instead of\nwriting all refs as separate loose files, it instead knows to write them\nall into a \"packed-refs\" file directly. This is significantly more\nefficient than having to write each of the refs as separate \"loose\" ref.\n\nThe only user of this optimization is git-clone(1), which only uses this\nmechanism to write regular refs. Consequently, the implementation does\nnot know how to handle both symbolic and root refs. While fine in the\ncontext of git-clone(1), this keeps us from using the mechanism in more\ncases.\n\nAdapt the logic to also support symbolic and root refs by using a second\ntransaction that we use for all of the refs that need to be written as\nloose refs.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs/files-backend.c | 44 ++++++++++++++++++++++++++++++++++----------\n 1 file changed, 34 insertions(+), 10 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 3ed18475a72aa4798d15b2912c37b4caabd47aac..116d4259697b20583cb2db34ed47025e8781cd42 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2995,6 +2995,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tint ret = 0;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n \tstruct ref_transaction *packed_transaction = NULL;\n+\tstruct ref_transaction *loose_transaction = NULL;\n \n \tassert(err);\n \n@@ -3040,6 +3041,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\tif ((update->flags & REF_HAVE_OLD) &&\n \t\t    !is_null_oid(&update->old_oid))\n \t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n+\n \t\tif (refs_verify_refname_available(&refs->base, update->refname,\n \t\t\t\t\t\t  &affected_refnames, NULL,\n \t\t\t\t\t\t  err)) {\n@@ -3048,26 +3050,48 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t}\n \n \t\t/*\n-\t\t * Add a reference creation for this reference to the\n-\t\t * packed-refs transaction:\n+\t\t * packed-refs don't support symbolic refs and root refs, so we\n+\t\t * have to queue these references via the loose transaction.\n \t\t */\n-\t\tref_transaction_add_update(packed_transaction, update->refname,\n-\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n-\t\t\t\t\t   &update->new_oid, &update->old_oid,\n-\t\t\t\t\t   NULL, NULL, NULL);\n+\t\tif (update->new_target || is_root_ref(update->refname)) {\n+\t\t\tif (!loose_transaction) {\n+\t\t\t\tloose_transaction = ref_store_transaction_begin(&refs->base, 0, err);\n+\t\t\t\tif (!loose_transaction) {\n+\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tgoto cleanup;\n+\t\t\t\t}\n+\t\t\t}\n+\n+\t\t\tref_transaction_add_update(loose_transaction, update->refname,\n+\t\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t\t   update->new_target ? NULL : &update->new_oid, NULL,\n+\t\t\t\t\t\t   update->new_target, NULL, NULL);\n+\t\t} else {\n+\t\t\tref_transaction_add_update(packed_transaction, update->refname,\n+\t\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t\t   &update->new_oid, &update->old_oid,\n+\t\t\t\t\t\t   NULL, NULL, NULL);\n+\t\t}\n \t}\n \n-\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n+\tif (packed_refs_lock(refs->packed_ref_store, 0, err) ||\n+\t    ref_transaction_commit(packed_transaction, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\n \t}\n+\tpacked_refs_unlock(refs->packed_ref_store);\n \n-\tif (ref_transaction_commit(packed_transaction, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n+\tif (loose_transaction) {\n+\t\tif (ref_transaction_prepare(loose_transaction, err) ||\n+\t\t    ref_transaction_commit(loose_transaction, err)) {\n+\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tgoto cleanup;\n+\t\t}\n \t}\n \n-\tpacked_refs_unlock(refs->packed_ref_store);\n cleanup:\n+\tif (loose_transaction)\n+\t\tref_transaction_free(loose_transaction);\n \tif (packed_transaction)\n \t\tref_transaction_free(packed_transaction);\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506848","messageId":"20241108-pks-refs-optimize-migrations-v1-9-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 09/10] reftable/block: rename `block_writer::buf` variable","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:49Z","receivedAt":"2024-11-08T09:35:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Adapt the name of the `block_writer::buf` variable to instead be called\n`block`. This aligns it with the existing `block_len` variable, which\ntracks the length of this buffer, and is generally a bit more tied to\nthe actual context where this variable gets used.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/block.c | 20 ++++++++++----------\n reftable/block.h |  8 ++++----\n 2 files changed, 14 insertions(+), 14 deletions(-)\n\ndiff --git a/reftable/block.c b/reftable/block.c\nindex f5b432566a6b9f171a1f1374b6c892ab0696d744..3fa36c002a0c1852790780e74a6e055161f857d9 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -70,14 +70,14 @@ static int block_writer_register_restart(struct block_writer *w, int n,\n \treturn 0;\n }\n \n-int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n+int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,\n \t\t      uint32_t block_size, uint32_t header_off, int hash_size)\n {\n-\tbw->buf = buf;\n+\tbw->block = block;\n \tbw->hash_size = hash_size;\n \tbw->block_size = block_size;\n \tbw->header_off = header_off;\n-\tbw->buf[header_off] = typ;\n+\tbw->block[header_off] = typ;\n \tbw->next = header_off + 4;\n \tbw->restart_interval = 16;\n \tbw->entries = 0;\n@@ -95,7 +95,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n \n uint8_t block_writer_type(struct block_writer *bw)\n {\n-\treturn bw->buf[bw->header_off];\n+\treturn bw->block[bw->header_off];\n }\n \n /* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on\n@@ -107,7 +107,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \tstruct reftable_buf last =\n \t\tw->entries % w->restart_interval == 0 ? empty : w->last_key;\n \tstruct string_view out = {\n-\t\t.buf = w->buf + w->next,\n+\t\t.buf = w->block + w->next,\n \t\t.len = w->block_size - w->next,\n \t};\n \n@@ -153,13 +153,13 @@ int block_writer_finish(struct block_writer *w)\n {\n \tint i;\n \tfor (i = 0; i < w->restart_len; i++) {\n-\t\tput_be24(w->buf + w->next, w->restarts[i]);\n+\t\tput_be24(w->block + w->next, w->restarts[i]);\n \t\tw->next += 3;\n \t}\n \n-\tput_be16(w->buf + w->next, w->restart_len);\n+\tput_be16(w->block + w->next, w->restart_len);\n \tw->next += 2;\n-\tput_be24(w->buf + 1 + w->header_off, w->next);\n+\tput_be24(w->block + 1 + w->header_off, w->next);\n \n \t/*\n \t * Log records are stored zlib-compressed. Note that the compression\n@@ -188,7 +188,7 @@ int block_writer_finish(struct block_writer *w)\n \n \t\tw->zstream->next_out = w->compressed;\n \t\tw->zstream->avail_out = compressed_len;\n-\t\tw->zstream->next_in = w->buf + block_header_skip;\n+\t\tw->zstream->next_in = w->block + block_header_skip;\n \t\tw->zstream->avail_in = src_len;\n \n \t\t/*\n@@ -206,7 +206,7 @@ int block_writer_finish(struct block_writer *w)\n \t\t * adjust the `next` pointer to point right after the\n \t\t * compressed data.\n \t\t */\n-\t\tmemcpy(w->buf + block_header_skip, w->compressed,\n+\t\tmemcpy(w->block + block_header_skip, w->compressed,\n \t\t       w->zstream->total_out);\n \t\tw->next = w->zstream->total_out + block_header_skip;\n \t}\ndiff --git a/reftable/block.h b/reftable/block.h\nindex 9a3effa513420039ee3f2834339c5082f64339d0..b3f837d612a8f0fbe98430b04e2dddaa975a15ab 100644\n--- a/reftable/block.h\n+++ b/reftable/block.h\n@@ -22,7 +22,7 @@ struct block_writer {\n \tunsigned char *compressed;\n \tsize_t compressed_cap;\n \n-\tuint8_t *buf;\n+\tuint8_t *block;\n \tuint32_t block_size;\n \n \t/* Offset of the global header. Nonzero in the first block only. */\n@@ -43,9 +43,9 @@ struct block_writer {\n };\n \n /*\n- * initializes the blockwriter to write `typ` entries, using `buf` as temporary\n- * storage. `buf` is not owned by the block_writer. */\n-int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n+ * initializes the blockwriter to write `typ` entries, using `block` as temporary\n+ * storage. `block` is not owned by the block_writer. */\n+int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,\n \t\t      uint32_t block_size, uint32_t header_off, int hash_size);\n \n /* returns the block type (eg. 'r' for ref records. */\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506849","messageId":"20241108-pks-refs-optimize-migrations-v1-8-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 08/10] reftable/writer: optimize allocations by using a scratch buffer","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:48Z","receivedAt":"2024-11-08T09:35:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Both `writer_add_record()` and `reftable_writer_add_ref()` get executed\nfor every single ref record we're adding to the reftable writer. And as\nboth functions use a local buffer to write data, the allocations we have\nto do here add up during larger transactions.\n\nRefactor the code to use a scratch buffer part of the `reftable_writer`\nitself such that we can reuse it. This signifcantly reduces the number\nof allocations during large transactions, e.g. when migrating refs from\nthe \"files\" backend to the \"reftable\" backend. Before this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 5,032,171 allocs, 5,032,122 frees, 418,792,092 bytes allocated\n\nAfter this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated\n\nThis also translate into a small speedup:\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     827.2 ms ±  16.5 ms    [User: 689.4 ms, System: 124.9 ms]\n      Range (min … max):   809.0 ms … 924.7 ms    50 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     813.6 ms ±  11.6 ms    [User: 679.0 ms, System: 123.4 ms]\n      Range (min … max):   786.7 ms … 833.5 ms    50 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = HEAD) ran\n        1.02 ± 0.02 times faster than migrate files:reftable (refcount = 1000000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/writer.c | 23 +++++++++++------------\n reftable/writer.h |  1 +\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex fd136794d5a27b33b5017f36fbd6b095ab8dac5b..6501376ce826469556a7dfa3c39258847300ae66 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -148,6 +148,7 @@ int reftable_writer_new(struct reftable_writer **out,\n \n \treftable_buf_init(&wp->block_writer_data.last_key);\n \treftable_buf_init(&wp->last_key);\n+\treftable_buf_init(&wp->buf);\n \tREFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);\n \tif (!wp->block) {\n \t\treftable_free(wp);\n@@ -180,6 +181,7 @@ static void writer_release(struct reftable_writer *w)\n \t\tw->block_writer = NULL;\n \t\twriter_clear_index(w);\n \t\treftable_buf_release(&w->last_key);\n+\t\treftable_buf_release(&w->buf);\n \t}\n }\n \n@@ -249,20 +251,19 @@ static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *has\n static int writer_add_record(struct reftable_writer *w,\n \t\t\t     struct reftable_record *rec)\n {\n-\tstruct reftable_buf key = REFTABLE_BUF_INIT;\n \tint err;\n \n-\terr = reftable_record_key(rec, &key);\n+\terr = reftable_record_key(rec, &w->buf);\n \tif (err < 0)\n \t\tgoto done;\n \n-\tif (reftable_buf_cmp(&w->last_key, &key) >= 0) {\n+\tif (reftable_buf_cmp(&w->last_key, &w->buf) >= 0) {\n \t\terr = REFTABLE_API_ERROR;\n \t\tgoto done;\n \t}\n \n \treftable_buf_reset(&w->last_key);\n-\terr = reftable_buf_add(&w->last_key, key.buf, key.len);\n+\terr = reftable_buf_add(&w->last_key, w->buf.buf, w->buf.len);\n \tif (err < 0)\n \t\tgoto done;\n \n@@ -312,7 +313,6 @@ static int writer_add_record(struct reftable_writer *w,\n \t}\n \n done:\n-\treftable_buf_release(&key);\n \treturn err;\n }\n \n@@ -325,7 +325,6 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \t\t\t.ref = *ref\n \t\t},\n \t};\n-\tstruct reftable_buf buf = REFTABLE_BUF_INIT;\n \tint err;\n \n \tif (!ref->refname ||\n@@ -340,24 +339,25 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \t\tgoto out;\n \n \tif (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) {\n-\t\terr = reftable_buf_add(&buf, (char *)reftable_ref_record_val1(ref),\n+\t\treftable_buf_reset(&w->buf);\n+\t\terr = reftable_buf_add(&w->buf, (char *)reftable_ref_record_val1(ref),\n \t\t\t\t       hash_size(w->opts.hash_id));\n \t\tif (err < 0)\n \t\t\tgoto out;\n \n-\t\terr = writer_index_hash(w, &buf);\n+\t\terr = writer_index_hash(w, &w->buf);\n \t\tif (err < 0)\n \t\t\tgoto out;\n \t}\n \n \tif (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) {\n-\t\treftable_buf_reset(&buf);\n-\t\terr = reftable_buf_add(&buf, reftable_ref_record_val2(ref),\n+\t\treftable_buf_reset(&w->buf);\n+\t\terr = reftable_buf_add(&w->buf, reftable_ref_record_val2(ref),\n \t\t\t\t       hash_size(w->opts.hash_id));\n \t\tif (err < 0)\n \t\t\tgoto out;\n \n-\t\terr = writer_index_hash(w, &buf);\n+\t\terr = writer_index_hash(w, &w->buf);\n \t\tif (err < 0)\n \t\t\tgoto out;\n \t}\n@@ -365,7 +365,6 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \terr = 0;\n \n out:\n-\treftable_buf_release(&buf);\n \treturn err;\n }\n \ndiff --git a/reftable/writer.h b/reftable/writer.h\nindex e8a6fbb78543e6e56920a2999601db0db9fe4d97..421a897dccd85ad0532860ff1b4f38b2813d438d 100644\n--- a/reftable/writer.h\n+++ b/reftable/writer.h\n@@ -20,6 +20,7 @@ struct reftable_writer {\n \tvoid *write_arg;\n \tint pending_padding;\n \tstruct reftable_buf last_key;\n+\tstruct reftable_buf buf;\n \n \t/* offset of next block to write. */\n \tuint64_t next;\n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506850","messageId":"20241108-pks-refs-optimize-migrations-v1-7-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 07/10] refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:47Z","receivedAt":"2024-11-08T09:35:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"When the `REF_SKIP_CREATE_REFLOG` flag is set we skip the creation of\nthe reflog entry, but we still normalize the reflog message when we\nqueue the update. This is a waste of resources as the normalized message\nwill never get used in the first place.\n\nFix this issue by skipping the normalization in case the flag is set.\nThis leads to a surprisingly large speedup when migrating from the\n\"files\" to the \"reftable\" backend:\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     878.5 ms ±  14.9 ms    [User: 726.5 ms, System: 139.2 ms]\n      Range (min … max):   858.4 ms … 941.3 ms    50 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     831.1 ms ±  10.5 ms    [User: 694.1 ms, System: 126.3 ms]\n      Range (min … max):   812.4 ms … 851.4 ms    50 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = HEAD) ran\n        1.06 ± 0.02 times faster than migrate files:reftable (refcount = 1000000, revision = HEAD~)\n\nAnd an ever larger speedup when migrating the other way round:\n\n    Benchmark 1: migrate reftable:files (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     923.6 ms ±  11.6 ms    [User: 705.5 ms, System: 208.1 ms]\n      Range (min … max):   905.3 ms … 946.5 ms    50 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     818.5 ms ±   9.0 ms    [User: 627.6 ms, System: 180.6 ms]\n      Range (min … max):   802.2 ms … 842.9 ms    50 runs\n\n    Summary\n      migrate reftable:files (refcount = 1000000, revision = HEAD) ran\n        1.13 ± 0.02 times faster than migrate reftable:files (refcount = 1000000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex d690eb19b3fd7083a5309deb98738547e4f48040..65eea3eb7734d03f09a22e8edfe5074d532398ff 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1188,8 +1188,9 @@ struct ref_update *ref_transaction_add_update(\n \t\toidcpy(&update->new_oid, new_oid);\n \tif ((flags & REF_HAVE_OLD) && old_oid)\n \t\toidcpy(&update->old_oid, old_oid);\n+\tif (!(flags & REF_SKIP_CREATE_REFLOG))\n+\t\tupdate->msg = normalize_reflog_message(msg);\n \n-\tupdate->msg = normalize_reflog_message(msg);\n \treturn update;\n }\n \n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506851","messageId":"20241108-pks-refs-optimize-migrations-v1-10-7fd37fa80e35@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH 10/10] reftable/block: optimize allocations by using scratch buffer","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-08T09:34:50Z","receivedAt":"2024-11-08T09:35:09Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The block writer needs to compute the key for every record that one adds\nto the writer. The buffer for this key is stored on the stack and thus\nreallocated on every call to `block_writer_add()`, which is inefficient.\n\nRefactor the code so that we store the buffer in the `block_writer`\nstruct itself so that we can reuse it. This reduces the number of\nallocations when writing many refs, e.g. when migrating one million refs\nfrom the \"files\" backend to the \"reftable backend. Before this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated\n\nAfter this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 2,013,250 allocs, 2,013,201 frees, 347,543,583 bytes allocated\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/block.c | 13 +++++--------\n reftable/block.h |  1 +\n 2 files changed, 6 insertions(+), 8 deletions(-)\n\ndiff --git a/reftable/block.c b/reftable/block.c\nindex 3fa36c002a0c1852790780e74a6e055161f857d9..1aa7e8cd3cbf0980f6bc20262be89e755d0a4b4b 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -110,24 +110,21 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \t\t.buf = w->block + w->next,\n \t\t.len = w->block_size - w->next,\n \t};\n-\n \tstruct string_view start = out;\n-\n \tint is_restart = 0;\n-\tstruct reftable_buf key = REFTABLE_BUF_INIT;\n \tint n = 0;\n \tint err;\n \n-\terr = reftable_record_key(rec, &key);\n+\terr = reftable_record_key(rec, &w->buf);\n \tif (err < 0)\n \t\tgoto done;\n \n-\tif (!key.len) {\n+\tif (!w->buf.len) {\n \t\terr = REFTABLE_API_ERROR;\n \t\tgoto done;\n \t}\n \n-\tn = reftable_encode_key(&is_restart, out, last, key,\n+\tn = reftable_encode_key(&is_restart, out, last, w->buf,\n \t\t\t\treftable_record_val_type(rec));\n \tif (n < 0) {\n \t\terr = -1;\n@@ -143,9 +140,8 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \tstring_view_consume(&out, n);\n \n \terr = block_writer_register_restart(w, start.len - out.len, is_restart,\n-\t\t\t\t\t    &key);\n+\t\t\t\t\t    &w->buf);\n done:\n-\treftable_buf_release(&key);\n \treturn err;\n }\n \n@@ -569,6 +565,7 @@ void block_writer_release(struct block_writer *bw)\n \tREFTABLE_FREE_AND_NULL(bw->zstream);\n \tREFTABLE_FREE_AND_NULL(bw->restarts);\n \tREFTABLE_FREE_AND_NULL(bw->compressed);\n+\treftable_buf_release(&bw->buf);\n \treftable_buf_release(&bw->last_key);\n \t/* the block is not owned. */\n }\ndiff --git a/reftable/block.h b/reftable/block.h\nindex b3f837d612a8f0fbe98430b04e2dddaa975a15ab..d76f00553073c10185e716e71e2f415ce5dcf7e2 100644\n--- a/reftable/block.h\n+++ b/reftable/block.h\n@@ -39,6 +39,7 @@ struct block_writer {\n \tuint32_t restart_cap;\n \n \tstruct reftable_buf last_key;\n+\tstruct reftable_buf buf;\n \tint entries;\n };\n \n\n-- \n2.47.0.229.g8f8d6eee53.dirty\n\n"},{"id":"506981","messageId":"CAOLa=ZQKJ3Twdze-08t3kh=kiuy6h9GfNSO25PkinjTwF5oG+w@mail.gmail.com","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-1-7fd37fa80e35@pks.im","subject":"Re: [PATCH 01/10] refs: allow passing flags when setting up a transaction","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2024-11-11T10:30:18Z","receivedAt":"2024-11-11T10:30:20Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> Allow passing flags when setting up a transaction such that the\n> behaviour of the transaction itself can be altered. Adapt callers\n> accordingly.\n>\n\nMaybe it is self-explanatory with the upcoming patches, but it'd be nice\nto know _why_ this change is being made.\n\nThe patch itself looks good.\n\n[snip]\n"},{"id":"507014","messageId":"CAOLa=ZSKH_OY5XG+toFUT2fD+6c7fshNZDfcOOSydxH23D91Zg@mail.gmail.com","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-4-7fd37fa80e35@pks.im","subject":"Re: [PATCH 04/10] refs/files: support symbolic and root refs in initial transaction","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2024-11-11T10:42:11Z","receivedAt":"2024-11-11T10:42:13Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> The \"files\" backend has implemented special logic when committing\n> the first transactions in an otherwise empty ref store: instead of\n> writing all refs as separate loose files, it instead knows to write them\n> all into a \"packed-refs\" file directly. This is significantly more\n> efficient than having to write each of the refs as separate \"loose\" ref.\n>\n> The only user of this optimization is git-clone(1), which only uses this\n> mechanism to write regular refs. Consequently, the implementation does\n> not know how to handle both symbolic and root refs. While fine in the\n> context of git-clone(1), this keeps us from using the mechanism in more\n> cases.\n>\n> Adapt the logic to also support symbolic and root refs by using a second\n> transaction that we use for all of the refs that need to be written as\n> loose refs.\n>\n\nThe patch looks good. I was wondering if another way would be to just\nadd symref and root ref support to packed-refs. But that might be a\nbigger undertaking than what we're doing here.\n\nBut thinking about it, seems like we can do that in a backwards\ncompatible way too.\n\nThe changes in this patch look good.\n\n[snip]\n"},{"id":"507015","messageId":"CAOLa=ZSOVYKgwkm-4w6cr_SSpGTJ2QMC-5pa3xm3mR5sBWmYMQ@mail.gmail.com","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-5-7fd37fa80e35@pks.im","subject":"Re: [PATCH 05/10] refs: use \"initial\" transaction semantics to migrate refs","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2024-11-11T10:43:39Z","receivedAt":"2024-11-11T10:43:41Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> Until now, we couldn't use \"initial\" transaction semantics to migrate\n> refs because the \"files\" backend only supported writing regular refs via\n> the initial transaction because it simply mapped the transaction to a\n> \"packed-refs\" transaction. But with the preceding commit, the \"files\"\n> backend has learned to also write symbolic and root refs in the initial\n> transaction by creating a second transaction for all refs that need to\n> be written as loose refs.\n>\n> Adapt the code to migrate refs to commit the transaction as an initial\n> transaction. This results in a signiticant speedup when migrating many\n> refs:\n>\n>     Benchmark 1: migrate reftable:files (refcount = 100000, revision = HEAD~)\n>       Time (mean ± σ):      3.247 s ±  0.034 s    [User: 0.485 s, System: 2.722 s]\n>       Range (min … max):    3.216 s …  3.309 s    10 runs\n>\n>     Benchmark 2: migrate reftable:files (refcount = 100000, revision = HEAD)\n>       Time (mean ± σ):     453.6 ms ±   1.9 ms    [User: 214.6 ms, System: 230.5 ms]\n>       Range (min … max):   451.5 ms … 456.4 ms    10 runs\n>\n>     Summary\n>       migrate reftable:files (refcount = 100000, revision = HEAD) ran\n>         7.16 ± 0.08 times faster than migrate reftable:files (refcount = 100000, revision = HEAD~)\n>\n> As the reftable backend doesn't (yet) special-case initial transactions\n> there is no comparable speedup for that backend.\n>\n\nNice. Really like how the actual perf changes were built up to a single\nline change.\n\n[snip]\n"},{"id":"507016","messageId":"CAOLa=ZRrPx2BMe+b3iOQMK9U+GiY6UVYpRJyqxrUamAG2b2EYg@mail.gmail.com","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-6-7fd37fa80e35@pks.im","subject":"Re: [PATCH 06/10] refs: skip collision checks in initial transactions","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2024-11-11T10:53:08Z","receivedAt":"2024-11-11T10:53:10Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n[snip]\n\n> diff --git a/refs.c b/refs.c\n> index 0f10c565bbb4e0d91210c52a3221a224e4264d28..d690eb19b3fd7083a5309deb98738547e4f48040 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -2324,6 +2324,7 @@ int refs_verify_refname_available(struct ref_store *refs,\n>  \t\t\t\t  const char *refname,\n>  \t\t\t\t  const struct string_list *extras,\n>  \t\t\t\t  const struct string_list *skip,\n> +\t\t\t\t  int initial_transaction,\n>  \t\t\t\t  struct strbuf *err)\n>  {\n>  \tconst char *slash;\n> @@ -2332,8 +2333,6 @@ int refs_verify_refname_available(struct ref_store *refs,\n>  \tstruct strbuf referent = STRBUF_INIT;\n>  \tstruct object_id oid;\n>  \tunsigned int type;\n> -\tstruct ref_iterator *iter;\n> -\tint ok;\n>  \tint ret = -1;\n>\n>  \t/*\n> @@ -2363,7 +2362,8 @@ int refs_verify_refname_available(struct ref_store *refs,\n>  \t\tif (skip && string_list_has_string(skip, dirname.buf))\n>  \t\t\tcontinue;\n>\n> -\t\tif (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n> +\t\tif (!initial_transaction &&\n> +\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n\nNit: It would be nice to have a comment here, perhaps something like:\n     /* in initial transaction there are no refs in the ref store */\n\n>  \t\t\t\t       &type, &ignore_errno)) {\n>  \t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n>  \t\t\t\t    dirname.buf, refname);\n> @@ -2388,21 +2388,26 @@ int refs_verify_refname_available(struct ref_store *refs,\n>  \tstrbuf_addstr(&dirname, refname + dirname.len);\n>  \tstrbuf_addch(&dirname, '/');\n>\n> -\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n> -\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n> -\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n> -\t\tif (skip &&\n> -\t\t    string_list_has_string(skip, iter->refname))\n> -\t\t\tcontinue;\n> +\tif (!initial_transaction) {\n> +\t\tstruct ref_iterator *iter;\n> +\t\tint ok;\n>\n> -\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n> -\t\t\t    iter->refname, refname);\n> -\t\tref_iterator_abort(iter);\n> -\t\tgoto cleanup;\n> -\t}\n> +\t\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n> +\t\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n> +\t\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n> +\t\t\tif (skip &&\n> +\t\t\t    string_list_has_string(skip, iter->refname))\n> +\t\t\t\tcontinue;\n>\n> -\tif (ok != ITER_DONE)\n> -\t\tBUG(\"error while iterating over references\");\n> +\t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n> +\t\t\t\t    iter->refname, refname);\n> +\t\t\tref_iterator_abort(iter);\n> +\t\t\tgoto cleanup;\n> +\t\t}\n> +\n> +\t\tif (ok != ITER_DONE)\n> +\t\t\tBUG(\"error while iterating over references\");\n> +\t}\n>\n>  \textra_refname = find_descendant_ref(dirname.buf, extras, skip);\n>  \tif (extra_refname)\n> diff --git a/refs.h b/refs.h\n> index 024a370554e013d66febee635e4c0415ba061fe6..980bd20cf24e15144aeff991eeba8b27a936d386 100644\n> --- a/refs.h\n> +++ b/refs.h\n> @@ -101,13 +101,16 @@ int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,\n>   * both \"foo\" and with \"foo/bar/baz\" but not with \"foo/bar\" or\n>   * \"foo/barbados\".\n>   *\n> + * If `initial_transaction` is truish, then all collision checks with\n> + * preexisting refs are skipped.\n> + *\n\nOkay we have it here, so this should do.\n\n[snip]\n"},{"id":"507018","messageId":"CAOLa=ZTGtGJDnMmuv++FS9Rv4KiRQewOepo_qOY=6h1xtNmNZA@mail.gmail.com","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2024-11-11T10:57:43Z","receivedAt":"2024-11-11T10:57:45Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> Hi,\n>\n> I have recently learned that ref format migrations can take a\n> significant amount of time in the order of minutes when migrating\n> millions of refs. This is probably not entirely surprising: the initial\n> focus for the logic to migrate ref backends was mostly focussed on\n> getting the basic feature working, and I didn't yet invest any time into\n> optimizing the code path at all. But I was still mildly surprised that\n> the migration of a couple million refs was taking minutes to finish.\n>\n> This patch series thus optimizes how we migrate ref formats. This is\n> mostly done by expanding upon the \"initial transaction\" semantics that\n> we already use for git-clone(1). These semantics allow us to assume that\n> the ref backend is completely empty and that there are no concurrent\n> writers, and thus we are free to perform certain optimizations that\n> wouldn't have otherwise been possible. On the one hand this allows us to\n> drop needless collision checks. On the other hand, it also allows us to\n> write regular refs directly into the \"packed-refs\" file when migrating\n> from the \"reftable\" backend to the \"files\" backend.\n>\n> This leads to some significant speedups. Migrating 1 million refs from\n> \"files\" to \"reftable\":\n>\n>     Benchmark 1: migrate files:reftable (refcount = 1000000, revision = origin/master)\n>       Time (mean ± σ):      4.580 s ±  0.062 s    [User: 1.818 s, System: 2.746 s]\n>       Range (min … max):    4.534 s …  4.743 s    10 runs\n>\n>     Benchmark 2: migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations)\n>       Time (mean ± σ):     767.7 ms ±   9.5 ms    [User: 629.2 ms, System: 126.1 ms]\n>       Range (min … max):   755.8 ms … 786.9 ms    10 runs\n>\n>     Summary\n>       migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n>         5.97 ± 0.11 times faster than migrate files:reftable (refcount = 1000000, revision = origin/master)\n>\n> And migrating from \"reftable\" to \"files:\n>\n>     Benchmark 1: migrate reftable:files (refcount = 1000000, revision = origin/master)\n>       Time (mean ± σ):     35.409 s ±  0.302 s    [User: 5.061 s, System: 29.244 s]\n>       Range (min … max):   35.055 s … 35.898 s    10 runs\n>\n>     Benchmark 2: migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations)\n>       Time (mean ± σ):     855.9 ms ±  61.5 ms    [User: 646.7 ms, System: 187.1 ms]\n>       Range (min … max):   830.0 ms … 1030.3 ms    10 runs\n>\n>     Summary\n>       migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n>        41.37 ± 2.99 times faster than migrate reftable:files (refcount = 1000000, revision = origin/master)\n>\n> Thanks!\n>\n> Patrick\n>\n\nI read through the series, apart from a few small nits, the patches\nlook good and straightforward.\n\nThanks\n"},{"id":"507029","messageId":"ZzH-MZ29Nu9yUu_Y@pks.im","threadId":"62467","inReplyTo":"CAOLa=ZQKJ3Twdze-08t3kh=kiuy6h9GfNSO25PkinjTwF5oG+w@mail.gmail.com","subject":"Re: [PATCH 01/10] refs: allow passing flags when setting up a transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-11T12:53:05Z","receivedAt":"2024-11-11T12:53:17Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 11, 2024 at 05:30:18AM -0500, karthik nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > Allow passing flags when setting up a transaction such that the\n> > behaviour of the transaction itself can be altered. Adapt callers\n> > accordingly.\n> >\n> \n> Maybe it is self-explanatory with the upcoming patches, but it'd be nice\n> to know _why_ this change is being made.\n> \n> The patch itself looks good.\n\nExplaining the exact use case here wouldn't make sense, as I'd basically\nhave to repeat the explanation given in the subsequent patch that wires\nthings up. But I can mention that this will be used in a later patch.\n\nPatrick\n"},{"id":"507030","messageId":"ZzH-NTDJlUH48vRM@pks.im","threadId":"62467","inReplyTo":"CAOLa=ZSKH_OY5XG+toFUT2fD+6c7fshNZDfcOOSydxH23D91Zg@mail.gmail.com","subject":"Re: [PATCH 04/10] refs/files: support symbolic and root refs in initial transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-11T12:53:09Z","receivedAt":"2024-11-11T12:53:19Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 11, 2024 at 02:42:11AM -0800, karthik nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > The \"files\" backend has implemented special logic when committing\n> > the first transactions in an otherwise empty ref store: instead of\n> > writing all refs as separate loose files, it instead knows to write them\n> > all into a \"packed-refs\" file directly. This is significantly more\n> > efficient than having to write each of the refs as separate \"loose\" ref.\n> >\n> > The only user of this optimization is git-clone(1), which only uses this\n> > mechanism to write regular refs. Consequently, the implementation does\n> > not know how to handle both symbolic and root refs. While fine in the\n> > context of git-clone(1), this keeps us from using the mechanism in more\n> > cases.\n> >\n> > Adapt the logic to also support symbolic and root refs by using a second\n> > transaction that we use for all of the refs that need to be written as\n> > loose refs.\n> >\n> \n> The patch looks good. I was wondering if another way would be to just\n> add symref and root ref support to packed-refs. But that might be a\n> bigger undertaking than what we're doing here.\n> \n> But thinking about it, seems like we can do that in a backwards\n> compatible way too.\n\nI don't think you can without introducing a new version of the format,\nmostly because you also have to think about clients that don't support\nthe new format. So all of this would be a big undertaking, and given\nthat we have the reftable backend nowadays I don't think it'd be all\nthat valuable.\n\nPatrick\n"},{"id":"507031","messageId":"ZzH-OaXmFcZkGE43@pks.im","threadId":"62467","inReplyTo":"CAOLa=ZTGtGJDnMmuv++FS9Rv4KiRQewOepo_qOY=6h1xtNmNZA@mail.gmail.com","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-11T12:53:13Z","receivedAt":"2024-11-11T12:53:22Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 11, 2024 at 05:57:43AM -0500, karthik nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> I read through the series, apart from a few small nits, the patches\n> look good and straightforward.\n\nI've queued the single change to the first commit message locally, but\ndon't think that this is sufficient reason yet to reroll the patch\nseries, so I'll wait for additional feedback.\n\nThanks for your review!\n\nPatrick\n"},{"id":"507684","messageId":"xmqqzfluidwo.fsf@gitster.g","threadId":"62467","inReplyTo":"ZzH-OaXmFcZkGE43@pks.im","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-11-20T07:04:55Z","receivedAt":"2024-11-20T07:04:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Mon, Nov 11, 2024 at 05:57:43AM -0500, karthik nayak wrote:\n>> Patrick Steinhardt <ps@pks.im> writes:\n>> I read through the series, apart from a few small nits, the patches\n>> look good and straightforward.\n>\n> I've queued the single change to the first commit message locally, but\n> don't think that this is sufficient reason yet to reroll the patch\n> series, so I'll wait for additional feedback.\n\nIt's been a week and a half, and since Karthik's reviews nothing has\nhappened.  So, let's start looking at merging it with that minimum\nupdate, perhaps?\n\nThanks.\n"},{"id":"507685","messageId":"Zz2UtKNnREAJgtaI@pks.im","threadId":"62467","inReplyTo":"xmqqzfluidwo.fsf@gitster.g","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:50:12Z","receivedAt":"2024-11-20T07:50:26Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Nov 20, 2024 at 04:04:55PM +0900, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > On Mon, Nov 11, 2024 at 05:57:43AM -0500, karthik nayak wrote:\n> >> Patrick Steinhardt <ps@pks.im> writes:\n> >> I read through the series, apart from a few small nits, the patches\n> >> look good and straightforward.\n> >\n> > I've queued the single change to the first commit message locally, but\n> > don't think that this is sufficient reason yet to reroll the patch\n> > series, so I'll wait for additional feedback.\n> \n> It's been a week and a half, and since Karthik's reviews nothing has\n> happened.  So, let's start looking at merging it with that minimum\n> update, perhaps?\n\nSure, let's do it that way. Will send the updated patch series in a\nsecond, thanks!\n\nPatrick\n"},{"id":"507686","messageId":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im","subject":"[PATCH v2 00/10] refs: optimize ref format migrations","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:29Z","receivedAt":"2024-11-20T07:51:44Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nI have recently learned that ref format migrations can take a\nsignificant amount of time in the order of minutes when migrating\nmillions of refs. This is probably not entirely surprising: the initial\nfocus for the logic to migrate ref backends was mostly focussed on\ngetting the basic feature working, and I didn't yet invest any time into\noptimizing the code path at all. But I was still mildly surprised that\nthe migration of a couple million refs was taking minutes to finish.\n\nThis patch series thus optimizes how we migrate ref formats. This is\nmostly done by expanding upon the \"initial transaction\" semantics that\nwe already use for git-clone(1). These semantics allow us to assume that\nthe ref backend is completely empty and that there are no concurrent\nwriters, and thus we are free to perform certain optimizations that\nwouldn't have otherwise been possible. On the one hand this allows us to\ndrop needless collision checks. On the other hand, it also allows us to\nwrite regular refs directly into the \"packed-refs\" file when migrating\nfrom the \"reftable\" backend to the \"files\" backend.\n\nThis leads to some significant speedups. Migrating 1 million refs from\n\"files\" to \"reftable\":\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = origin/master)\n      Time (mean ± σ):      4.580 s ±  0.062 s    [User: 1.818 s, System: 2.746 s]\n      Range (min … max):    4.534 s …  4.743 s    10 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations)\n      Time (mean ± σ):     767.7 ms ±   9.5 ms    [User: 629.2 ms, System: 126.1 ms]\n      Range (min … max):   755.8 ms … 786.9 ms    10 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n        5.97 ± 0.11 times faster than migrate files:reftable (refcount = 1000000, revision = origin/master)\n\nAnd migrating from \"reftable\" to \"files:\n\n    Benchmark 1: migrate reftable:files (refcount = 1000000, revision = origin/master)\n      Time (mean ± σ):     35.409 s ±  0.302 s    [User: 5.061 s, System: 29.244 s]\n      Range (min … max):   35.055 s … 35.898 s    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations)\n      Time (mean ± σ):     855.9 ms ±  61.5 ms    [User: 646.7 ms, System: 187.1 ms]\n      Range (min … max):   830.0 ms … 1030.3 ms    10 runs\n\n    Summary\n      migrate reftable:files (refcount = 1000000, revision = pks-refs-optimize-migrations) ran\n       41.37 ± 2.99 times faster than migrate reftable:files (refcount = 1000000, revision = origin/master)\n\nChanges in v2:\n\n  - Mention in the first commit message that the introduced flag will be\n    used in a subsequent patch.\n  - Link to v1: https://lore.kernel.org/r/20241108-pks-refs-optimize-migrations-v1-0-7fd37fa80e35@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (10):\n      refs: allow passing flags when setting up a transaction\n      refs/files: move logic to commit initial transaction\n      refs: introduce \"initial\" transaction flag\n      refs/files: support symbolic and root refs in initial transaction\n      refs: use \"initial\" transaction semantics to migrate refs\n      refs: skip collision checks in initial transactions\n      refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG`\n      reftable/writer: optimize allocations by using a scratch buffer\n      reftable/block: rename `block_writer::buf` variable\n      reftable/block: optimize allocations by using scratch buffer\n\n branch.c                  |   2 +-\n builtin/clone.c           |   4 +-\n builtin/fast-import.c     |   4 +-\n builtin/fetch.c           |   4 +-\n builtin/receive-pack.c    |   4 +-\n builtin/replace.c         |   2 +-\n builtin/tag.c             |   2 +-\n builtin/update-ref.c      |   4 +-\n refs.c                    |  70 ++++++-------\n refs.h                    |  45 +++++----\n refs/debug.c              |  13 ---\n refs/files-backend.c      | 244 +++++++++++++++++++++++++---------------------\n refs/packed-backend.c     |   8 --\n refs/refs-internal.h      |   2 +-\n refs/reftable-backend.c   |  14 +--\n reftable/block.c          |  33 +++----\n reftable/block.h          |   9 +-\n reftable/writer.c         |  23 +++--\n reftable/writer.h         |   1 +\n sequencer.c               |   6 +-\n t/helper/test-ref-store.c |   2 +-\n t/t1460-refs-migrate.sh   |   2 +-\n walker.c                  |   2 +-\n 23 files changed, 247 insertions(+), 253 deletions(-)\n\nRange-diff versus v1:\n\n 1:  059a17163a !  1:  167a262a0c refs: allow passing flags when setting up a transaction\n    @@ Commit message\n         refs: allow passing flags when setting up a transaction\n     \n         Allow passing flags when setting up a transaction such that the\n    -    behaviour of the transaction itself can be altered. Adapt callers\n    -    accordingly.\n    +    behaviour of the transaction itself can be altered. This functionality\n    +    will be used in a subsequent patch.\n    +\n    +    Adapt callers accordingly.\n     \n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n 2:  2acf64db6b =  2:  39255cdeff refs/files: move logic to commit initial transaction\n 3:  bac7ca1d80 =  3:  061950cf39 refs: introduce \"initial\" transaction flag\n 4:  1101c9da75 =  4:  a559563c57 refs/files: support symbolic and root refs in initial transaction\n 5:  75e769ec3e =  5:  8605fb50bc refs: use \"initial\" transaction semantics to migrate refs\n 6:  a91fe4e2d5 =  6:  eb9cacdaa6 refs: skip collision checks in initial transactions\n 7:  55901bd45f =  7:  6ac43c46b7 refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG`\n 8:  d15006b445 =  8:  3b192c1e02 reftable/writer: optimize allocations by using a scratch buffer\n 9:  4b5603743f =  9:  96cd8b8287 reftable/block: rename `block_writer::buf` variable\n10:  4dd22bdbef = 10:  a36318ece6 reftable/block: optimize allocations by using scratch buffer\n\n---\nbase-commit: facbe4f633e4ad31e641f64617bc88074c659959\nchange-id: 20241108-pks-refs-optimize-migrations-6d0ceee4abb7\n\n"},{"id":"507687","messageId":"20241120-pks-refs-optimize-migrations-v2-1-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 01/10] refs: allow passing flags when setting up a transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:30Z","receivedAt":"2024-11-20T07:51:45Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Allow passing flags when setting up a transaction such that the\nbehaviour of the transaction itself can be altered. This functionality\nwill be used in a subsequent patch.\n\nAdapt callers accordingly.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n branch.c               |  2 +-\n builtin/clone.c        |  2 +-\n builtin/fast-import.c  |  4 ++--\n builtin/fetch.c        |  4 ++--\n builtin/receive-pack.c |  4 ++--\n builtin/replace.c      |  2 +-\n builtin/tag.c          |  2 +-\n builtin/update-ref.c   |  4 ++--\n refs.c                 | 12 +++++++-----\n refs.h                 |  3 ++-\n refs/files-backend.c   | 11 +++++++----\n refs/refs-internal.h   |  1 +\n sequencer.c            |  6 +++---\n walker.c               |  2 +-\n 14 files changed, 33 insertions(+), 26 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 44977ad0aadbd40c878a0475ef2df2a20798936b..ebaa870c018747358255d3f150d136f0df447d5d 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -627,7 +627,7 @@ void create_branch(struct repository *r,\n \telse\n \t\tmsg = xstrfmt(\"branch: Created from %s\", start_name);\n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t\tref_transaction_update(transaction, ref.buf,\n \t\t\t\t\t&oid, forcing ? NULL : null_oid(),\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 59fcb317a68a77eee3ca96a60720c556e044c369..d963cc6eb5181e1af5bb29c07c3ee2fa24ad04ca 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -574,7 +574,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \tstruct strbuf err = STRBUF_INIT;\n \n \tt = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t&err);\n+\t\t\t\t\t0, &err);\n \tif (!t)\n \t\tdie(\"%s\", err.buf);\n \ndiff --git a/builtin/fast-import.c b/builtin/fast-import.c\nindex 76d5c20f141f42da988dddae0e549144d1379031..db82c37a06f05d25e0a8c34cbec055e6f9717191 100644\n--- a/builtin/fast-import.c\n+++ b/builtin/fast-import.c\n@@ -1634,7 +1634,7 @@ static int update_branch(struct branch *b)\n \t\t}\n \t}\n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, b->name, &b->oid, &old_oid,\n \t\t\t\t   NULL, NULL, 0, msg, &err) ||\n@@ -1669,7 +1669,7 @@ static void dump_tags(void)\n \tstruct ref_transaction *transaction;\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction) {\n \t\tfailure |= error(\"%s\", err.buf);\n \t\tgoto cleanup;\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex d9027e4dc9245a32a87c47d89f9a29fcfd42534c..68d291c7dd0a4fe805e3b006cc68b185481fba6b 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -669,7 +669,7 @@ static int s_update_ref(const char *action,\n \t */\n \tif (!transaction) {\n \t\ttransaction = our_transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t\t\t    &err);\n+\t\t\t\t\t\t\t\t\t    0, &err);\n \t\tif (!transaction) {\n \t\t\tret = STORE_REF_ERROR_OTHER;\n \t\t\tgoto out;\n@@ -1671,7 +1671,7 @@ static int do_fetch(struct transport *transport,\n \n \tif (atomic_fetch) {\n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\tretcode = -1;\n \t\t\tgoto cleanup;\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex ab5b20e39c5038cdf6e6f05f4d66278a9c1ac156..9d2c07f68dafb6fce87cb59abc1cbf27db9aae09 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -1849,7 +1849,7 @@ static void execute_commands_non_atomic(struct command *commands,\n \t\t\tcontinue;\n \n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\trp_error(\"%s\", err.buf);\n \t\t\tstrbuf_reset(&err);\n@@ -1878,7 +1878,7 @@ static void execute_commands_atomic(struct command *commands,\n \tconst char *reported_error = \"atomic push failure\";\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction) {\n \t\trp_error(\"%s\", err.buf);\n \t\tstrbuf_reset(&err);\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex a44f4e7ea9ff55dbd1f102aca5e5f5a046391328..a4eaadff91f1be107a8e0e25a701d2006ff8cac2 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref.buf, repl, &prev,\n \t\t\t\t   NULL, NULL, 0, NULL, &err) ||\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 93d10d59157d2ee1b41f90640bd162917f1eb162..3fa0ab111344dda477763a74d26076b6fb71a5ab 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -681,7 +681,7 @@ int cmd_tag(int argc,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref.buf, &object, &prev,\n \t\t\t\t   NULL, NULL,\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 8a98615dc8613a1be3b17c6d688ab9c0208ed003..670e7812d60ea88a20cd1c5cd113643095cb3be1 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -612,7 +612,7 @@ static void update_refs_stdin(void)\n \tint i, j;\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction)\n \t\tdie(\"%s\", err.buf);\n \n@@ -680,7 +680,7 @@ static void update_refs_stdin(void)\n \t\t\t */\n \t\t\tstate = cmd->state;\n \t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t\t  0, &err);\n \t\t\tif (!transaction)\n \t\t\t\tdie(\"%s\", err.buf);\n \ndiff --git a/refs.c b/refs.c\nindex 5f729ed4124f7fe8fa9df7fd1f1ce951abefc585..9effeb01eb45728514eab0ca92404ea8cf2158d9 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -918,7 +918,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, old_oid,\n \t\t\t\t   NULL, flags, msg, &err) ||\n@@ -1116,6 +1116,7 @@ int read_ref_at(struct ref_store *refs, const char *refname,\n }\n \n struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n+\t\t\t\t\t\t    unsigned int flags,\n \t\t\t\t\t\t    struct strbuf *err)\n {\n \tstruct ref_transaction *tr;\n@@ -1123,6 +1124,7 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n \n \tCALLOC_ARRAY(tr, 1);\n \ttr->ref_store = refs;\n+\ttr->flags = flags;\n \treturn tr;\n }\n \n@@ -1309,7 +1311,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret = 0;\n \n-\tt = ref_store_transaction_begin(refs, &err);\n+\tt = ref_store_transaction_begin(refs, 0, &err);\n \tif (!t ||\n \t    ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,\n \t\t\t\t   flags, msg, &err) ||\n@@ -2120,7 +2122,7 @@ int refs_update_symref(struct ref_store *refs, const char *ref,\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret = 0;\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref, NULL, NULL,\n \t\t\t\t   target, NULL, REF_NO_DEREF,\n@@ -2527,7 +2529,7 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \t * individual updates can't fail, so we can pack all of the\n \t * updates into a single transaction.\n \t */\n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction) {\n \t\tret = error(\"%s\", err.buf);\n \t\tgoto out;\n@@ -2833,7 +2835,7 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\ttransaction = ref_store_transaction_begin(new_refs, errbuf);\n+\ttransaction = ref_store_transaction_begin(new_refs, 0, errbuf);\n \tif (!transaction)\n \t\tgoto done;\n \ndiff --git a/refs.h b/refs.h\nindex 108dfc93b3428db491916ad8a55daea649d83ffd..9821f3e80d900b31c3dede489c2f415d968233d7 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -234,7 +234,7 @@ char *repo_default_branch_name(struct repository *r, int quiet);\n  *         struct strbuf err = STRBUF_INIT;\n  *         int ret = 0;\n  *\n- *         transaction = ref_store_transaction_begin(refs, &err);\n+ *         transaction = ref_store_transaction_begin(refs, 0, &err);\n  *         if (!transaction ||\n  *             ref_transaction_update(...) ||\n  *             ref_transaction_create(...) ||\n@@ -584,6 +584,7 @@ enum action_on_err {\n  * be freed by calling ref_transaction_free().\n  */\n struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n+\t\t\t\t\t\t    unsigned int flags,\n \t\t\t\t\t\t    struct strbuf *err);\n \n /*\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 0824c0b8a946909791da36ddb4171db4ad98913b..df61057c9f24972b72644407cc95057891338d96 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1252,7 +1252,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)\n \tif (check_refname_format(r->name, 0))\n \t\treturn;\n \n-\ttransaction = ref_store_transaction_begin(&refs->base, &err);\n+\ttransaction = ref_store_transaction_begin(&refs->base, 0, &err);\n \tif (!transaction)\n \t\tgoto cleanup;\n \tref_transaction_add_update(\n@@ -1396,7 +1396,8 @@ static int files_pack_refs(struct ref_store *ref_store,\n \tif (!should_pack_refs(refs, opts))\n \t\treturn 0;\n \n-\ttransaction = ref_store_transaction_begin(refs->packed_ref_store, &err);\n+\ttransaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction)\n \t\treturn -1;\n \n@@ -2867,7 +2868,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t */\n \t\t\tif (!packed_transaction) {\n \t\t\t\tpacked_transaction = ref_store_transaction_begin(\n-\t\t\t\t\t\trefs->packed_ref_store, err);\n+\t\t\t\t\t\trefs->packed_ref_store,\n+\t\t\t\t\t\ttransaction->flags, err);\n \t\t\t\tif (!packed_transaction) {\n \t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\t\tgoto cleanup;\n@@ -3174,7 +3176,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \t\t\t\t &affected_refnames))\n \t\tBUG(\"initial ref transaction called with existing refs\");\n \n-\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);\n+\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t\t transaction->flags, err);\n \tif (!packed_transaction) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 2313c830d8facaa17b0b4b073df0de958023062a..dbc6360c5a1d410c192e7eee1bffb1d423e1f9ee 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -193,6 +193,7 @@ struct ref_transaction {\n \tsize_t nr;\n \tenum ref_transaction_state state;\n \tvoid *backend_data;\n+\tunsigned int flags;\n };\n \n /*\ndiff --git a/sequencer.c b/sequencer.c\nindex 353d804999b88d5fd5dcf1254d80781f20e62f2e..287f4e5e8766327da6f31e87ce0c20f63b302b77 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -662,7 +662,7 @@ static int fast_forward_to(struct repository *r,\n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t  0, &err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\",\n \t\t\t\t   to, unborn && !is_rebase_i(opts) ?\n@@ -1297,7 +1297,7 @@ int update_head_with_reflog(const struct commit *old_head,\n \t}\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  err);\n+\t\t\t\t\t\t  0, err);\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\", new_head,\n \t\t\t\t   old_head ? &old_head->object.oid : null_oid(),\n@@ -3890,7 +3890,7 @@ static int do_label(struct repository *r, const char *name, int len)\n \tstrbuf_addf(&ref_name, \"refs/rewritten/%.*s\", len, name);\n \tstrbuf_addf(&msg, \"rebase (label) '%.*s'\", len, name);\n \n-\ttransaction = ref_store_transaction_begin(refs, &err);\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n \tif (!transaction) {\n \t\terror(\"%s\", err.buf);\n \t\tret = -1;\ndiff --git a/walker.c b/walker.c\nindex 5ea7e5b392b2bd49f249a9acc8d7ce8779357e1b..7cc9dbea46d64d6bd3336025d640f284a6202157 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -290,7 +290,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \n \tif (write_ref) {\n \t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t  &err);\n+\t\t\t\t\t\t\t  0, &err);\n \t\tif (!transaction) {\n \t\t\terror(\"%s\", err.buf);\n \t\t\tgoto done;\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507688","messageId":"20241120-pks-refs-optimize-migrations-v2-2-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 02/10] refs/files: move logic to commit initial transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:31Z","receivedAt":"2024-11-20T07:51:46Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Move the logic to commit initial transactions such that we can start to\ncall it in `files_transaction_finish()` in a subsequent commit without\nrequiring a separate function declaration.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs/files-backend.c | 202 +++++++++++++++++++++++++--------------------------\n 1 file changed, 101 insertions(+), 101 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex df61057c9f24972b72644407cc95057891338d96..f37c805a34167b3749fbe724788180975abdae90 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2975,6 +2975,107 @@ static int parse_and_write_reflog(struct files_ref_store *refs,\n \treturn 0;\n }\n \n+static int ref_present(const char *refname, const char *referent UNUSED,\n+\t\t       const struct object_id *oid UNUSED,\n+\t\t       int flags UNUSED,\n+\t\t       void *cb_data)\n+{\n+\tstruct string_list *affected_refnames = cb_data;\n+\n+\treturn string_list_has_string(affected_refnames, refname);\n+}\n+\n+static int files_initial_transaction_commit(struct ref_store *ref_store,\n+\t\t\t\t\t    struct ref_transaction *transaction,\n+\t\t\t\t\t    struct strbuf *err)\n+{\n+\tstruct files_ref_store *refs =\n+\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t       \"initial_ref_transaction_commit\");\n+\tsize_t i;\n+\tint ret = 0;\n+\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n+\tstruct ref_transaction *packed_transaction = NULL;\n+\n+\tassert(err);\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tBUG(\"commit called for transaction that is not open\");\n+\n+\t/* Fail if a refname appears more than once in the transaction: */\n+\tfor (i = 0; i < transaction->nr; i++)\n+\t\tstring_list_append(&affected_refnames,\n+\t\t\t\t   transaction->updates[i]->refname);\n+\tstring_list_sort(&affected_refnames);\n+\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\t/*\n+\t * It's really undefined to call this function in an active\n+\t * repository or when there are existing references: we are\n+\t * only locking and changing packed-refs, so (1) any\n+\t * simultaneous processes might try to change a reference at\n+\t * the same time we do, and (2) any existing loose versions of\n+\t * the references that we are setting would have precedence\n+\t * over our values. But some remote helpers create the remote\n+\t * \"HEAD\" and \"master\" branches before calling this function,\n+\t * so here we really only check that none of the references\n+\t * that we are creating already exists.\n+\t */\n+\tif (refs_for_each_rawref(&refs->base, ref_present,\n+\t\t\t\t &affected_refnames))\n+\t\tBUG(\"initial ref transaction called with existing refs\");\n+\n+\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n+\t\t\t\t\t\t\t transaction->flags, err);\n+\tif (!packed_transaction) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif ((update->flags & REF_HAVE_OLD) &&\n+\t\t    !is_null_oid(&update->old_oid))\n+\t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n+\t\tif (refs_verify_refname_available(&refs->base, update->refname,\n+\t\t\t\t\t\t  &affected_refnames, NULL,\n+\t\t\t\t\t\t  err)) {\n+\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\tgoto cleanup;\n+\t\t}\n+\n+\t\t/*\n+\t\t * Add a reference creation for this reference to the\n+\t\t * packed-refs transaction:\n+\t\t */\n+\t\tref_transaction_add_update(packed_transaction, update->refname,\n+\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t   &update->new_oid, &update->old_oid,\n+\t\t\t\t\t   NULL, NULL, NULL);\n+\t}\n+\n+\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t}\n+\n+\tpacked_refs_unlock(refs->packed_ref_store);\n+cleanup:\n+\tif (packed_transaction)\n+\t\tref_transaction_free(packed_transaction);\n+\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\tstring_list_clear(&affected_refnames, 0);\n+\treturn ret;\n+}\n+\n static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t    struct strbuf *err)\n@@ -3123,107 +3224,6 @@ static int files_transaction_abort(struct ref_store *ref_store,\n \treturn 0;\n }\n \n-static int ref_present(const char *refname, const char *referent UNUSED,\n-\t\t       const struct object_id *oid UNUSED,\n-\t\t       int flags UNUSED,\n-\t\t       void *cb_data)\n-{\n-\tstruct string_list *affected_refnames = cb_data;\n-\n-\treturn string_list_has_string(affected_refnames, refname);\n-}\n-\n-static int files_initial_transaction_commit(struct ref_store *ref_store,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n-\t\t\t       \"initial_ref_transaction_commit\");\n-\tsize_t i;\n-\tint ret = 0;\n-\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n-\tstruct ref_transaction *packed_transaction = NULL;\n-\n-\tassert(err);\n-\n-\tif (transaction->state != REF_TRANSACTION_OPEN)\n-\t\tBUG(\"commit called for transaction that is not open\");\n-\n-\t/* Fail if a refname appears more than once in the transaction: */\n-\tfor (i = 0; i < transaction->nr; i++)\n-\t\tstring_list_append(&affected_refnames,\n-\t\t\t\t   transaction->updates[i]->refname);\n-\tstring_list_sort(&affected_refnames);\n-\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\t/*\n-\t * It's really undefined to call this function in an active\n-\t * repository or when there are existing references: we are\n-\t * only locking and changing packed-refs, so (1) any\n-\t * simultaneous processes might try to change a reference at\n-\t * the same time we do, and (2) any existing loose versions of\n-\t * the references that we are setting would have precedence\n-\t * over our values. But some remote helpers create the remote\n-\t * \"HEAD\" and \"master\" branches before calling this function,\n-\t * so here we really only check that none of the references\n-\t * that we are creating already exists.\n-\t */\n-\tif (refs_for_each_rawref(&refs->base, ref_present,\n-\t\t\t\t &affected_refnames))\n-\t\tBUG(\"initial ref transaction called with existing refs\");\n-\n-\tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n-\t\t\t\t\t\t\t transaction->flags, err);\n-\tif (!packed_transaction) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\tfor (i = 0; i < transaction->nr; i++) {\n-\t\tstruct ref_update *update = transaction->updates[i];\n-\n-\t\tif ((update->flags & REF_HAVE_OLD) &&\n-\t\t    !is_null_oid(&update->old_oid))\n-\t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n-\t\tif (refs_verify_refname_available(&refs->base, update->refname,\n-\t\t\t\t\t\t  &affected_refnames, NULL,\n-\t\t\t\t\t\t  err)) {\n-\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\tgoto cleanup;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Add a reference creation for this reference to the\n-\t\t * packed-refs transaction:\n-\t\t */\n-\t\tref_transaction_add_update(packed_transaction, update->refname,\n-\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n-\t\t\t\t\t   &update->new_oid, &update->old_oid,\n-\t\t\t\t\t   NULL, NULL, NULL);\n-\t}\n-\n-\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n-\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t}\n-\n-\tpacked_refs_unlock(refs->packed_ref_store);\n-cleanup:\n-\tif (packed_transaction)\n-\t\tref_transaction_free(packed_transaction);\n-\ttransaction->state = REF_TRANSACTION_CLOSED;\n-\tstring_list_clear(&affected_refnames, 0);\n-\treturn ret;\n-}\n-\n struct expire_reflog_cb {\n \treflog_expiry_should_prune_fn *should_prune_fn;\n \tvoid *policy_cb;\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507690","messageId":"20241120-pks-refs-optimize-migrations-v2-3-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 03/10] refs: introduce \"initial\" transaction flag","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:32Z","receivedAt":"2024-11-20T07:51:47Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"There are two different ways to commit a transaction:\n\n  - `ref_transaction_commit()` can be used to commit a regular\n    transaction and is what almost every caller wants.\n\n  - `initial_ref_transaction_commit()` can be used when it is known that\n    the ref store that the transaction is committed for is empty and\n    when there are no concurrent processes. This is used when cloning a\n    new repository.\n\nImplementing this via two separate functions has a couple of downsides.\nFirst, every reference backend needs to implement a separate callback\neven in the case where they don't special-case the initial transaction.\nSecond, backends are basically forced to reimplement the whole logic for\nhow to commit the transaction like the \"files\" backend does, even though\nbackends may wish to only tweak certain behaviour of a \"normal\" commit.\nThird, it is awkward that callers must never prepare the transaction as\nthis is somewhat different than how a transaction typically works.\n\nRefactor the code such that we instead mark initial transactions via a\nseparate flag when starting the transaction. This addresses all of the\nmentioned painpoints, where the most important part is that it will\nallow backends to have way more leeway in how exactly they want to\nhandle the initial transaction.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/clone.c         |  4 ++--\n refs.c                  | 10 +---------\n refs.h                  | 37 ++++++++++++++++++-------------------\n refs/debug.c            | 13 -------------\n refs/files-backend.c    | 16 ++++++++--------\n refs/packed-backend.c   |  8 --------\n refs/refs-internal.h    |  1 -\n refs/reftable-backend.c |  8 --------\n 8 files changed, 29 insertions(+), 68 deletions(-)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex d963cc6eb5181e1af5bb29c07c3ee2fa24ad04ca..8427ccec17e77b23ee1a7f526b80a52d79a71873 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -574,7 +574,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \tstruct strbuf err = STRBUF_INIT;\n \n \tt = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t0, &err);\n+\t\t\t\t\tREF_TRANSACTION_FLAG_INITIAL, &err);\n \tif (!t)\n \t\tdie(\"%s\", err.buf);\n \n@@ -586,7 +586,7 @@ static void write_remote_refs(const struct ref *local_refs)\n \t\t\tdie(\"%s\", err.buf);\n \t}\n \n-\tif (initial_ref_transaction_commit(t, &err))\n+\tif (ref_transaction_commit(t, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tstrbuf_release(&err);\ndiff --git a/refs.c b/refs.c\nindex 9effeb01eb45728514eab0ca92404ea8cf2158d9..8b9dfc6173fd144fe9a172cb81cf33057ae31368 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2315,7 +2315,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \n \tret = refs->be->transaction_finish(refs, transaction, err);\n-\tif (!ret)\n+\tif (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n \t\trun_transaction_hook(transaction, \"committed\");\n \treturn ret;\n }\n@@ -2486,14 +2486,6 @@ int refs_reflog_expire(struct ref_store *refs,\n \t\t\t\t       cleanup_fn, policy_cb_data);\n }\n \n-int initial_ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err)\n-{\n-\tstruct ref_store *refs = transaction->ref_store;\n-\n-\treturn refs->be->initial_transaction_commit(refs, transaction, err);\n-}\n-\n void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,\n \t\t\t\t\t    ref_transaction_for_each_queued_update_fn cb,\n \t\t\t\t\t    void *cb_data)\ndiff --git a/refs.h b/refs.h\nindex 9821f3e80d900b31c3dede489c2f415d968233d7..024a370554e013d66febee635e4c0415ba061fe6 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -214,11 +214,9 @@ char *repo_default_branch_name(struct repository *r, int quiet);\n  *\n  *   Or\n  *\n- *   - Call `initial_ref_transaction_commit()` if the ref database is\n- *     known to be empty and have no other writers (e.g. during\n- *     clone). This is likely to be much faster than\n- *     `ref_transaction_commit()`. `ref_transaction_prepare()` should\n- *     *not* be called before `initial_ref_transaction_commit()`.\n+ *   - Call `ref_transaction_begin()` with REF_TRANSACTION_FLAG_INITIAL if the\n+ *     ref database is known to be empty and have no other writers (e.g. during\n+ *     clone). This is likely to be much faster than without the flag.\n  *\n  * - Then finally, call `ref_transaction_free()` to free the\n  *   `ref_transaction` data structure.\n@@ -579,6 +577,21 @@ enum action_on_err {\n \tUPDATE_REFS_QUIET_ON_ERR\n };\n \n+enum ref_transaction_flag {\n+\t/*\n+\t * The ref transaction is part of the initial creation of the ref store\n+\t * and can thus assume that the ref store is completely empty. This\n+\t * allows the backend to perform the transaction more efficiently by\n+\t * skipping certain checks.\n+\t *\n+\t * It is a bug to set this flag when there might be other processes\n+\t * accessing the repository or if there are existing references that\n+\t * might conflict with the ones being created. All old_oid values must\n+\t * either be absent or null_oid.\n+\t */\n+\tREF_TRANSACTION_FLAG_INITIAL = (1 << 0),\n+};\n+\n /*\n  * Begin a reference transaction.  The reference transaction must\n  * be freed by calling ref_transaction_free().\n@@ -798,20 +811,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n int ref_transaction_abort(struct ref_transaction *transaction,\n \t\t\t  struct strbuf *err);\n \n-/*\n- * Like ref_transaction_commit(), but optimized for creating\n- * references when originally initializing a repository (e.g., by \"git\n- * clone\"). It writes the new references directly to packed-refs\n- * without locking the individual references.\n- *\n- * It is a bug to call this function when there might be other\n- * processes accessing the repository or if there are existing\n- * references that might conflict with the ones being created. All\n- * old_oid values must either be absent or null_oid.\n- */\n-int initial_ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err);\n-\n /*\n  * Execute the given callback function for each of the reference updates which\n  * have been queued in the given transaction. `old_oid` and `new_oid` may be\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 45e2e784a0f8c49f492eaa9d371aa44f8c7916c3..cbac62c8a4f924580e80f106f87639daf77cef5c 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -118,18 +118,6 @@ static int debug_transaction_abort(struct ref_store *refs,\n \treturn res;\n }\n \n-static int debug_initial_transaction_commit(struct ref_store *refs,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)refs;\n-\tint res;\n-\ttransaction->ref_store = drefs->refs;\n-\tres = drefs->refs->be->initial_transaction_commit(drefs->refs,\n-\t\t\t\t\t\t\t  transaction, err);\n-\treturn res;\n-}\n-\n static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)\n {\n \tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n@@ -443,7 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.transaction_prepare = debug_transaction_prepare,\n \t.transaction_finish = debug_transaction_finish,\n \t.transaction_abort = debug_transaction_abort,\n-\t.initial_transaction_commit = debug_initial_transaction_commit,\n \n \t.pack_refs = debug_pack_refs,\n \t.rename_ref = debug_rename_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f37c805a34167b3749fbe724788180975abdae90..3ed18475a72aa4798d15b2912c37b4caabd47aac 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2781,6 +2781,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n \tassert(err);\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n+\t\tgoto cleanup;\n \tif (!transaction->nr)\n \t\tgoto cleanup;\n \n@@ -2985,13 +2987,10 @@ static int ref_present(const char *refname, const char *referent UNUSED,\n \treturn string_list_has_string(affected_refnames, refname);\n }\n \n-static int files_initial_transaction_commit(struct ref_store *ref_store,\n+static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t\t    struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n-\t\t\t       \"initial_ref_transaction_commit\");\n \tsize_t i;\n \tint ret = 0;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n@@ -2999,8 +2998,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \n \tassert(err);\n \n-\tif (transaction->state != REF_TRANSACTION_OPEN)\n-\t\tBUG(\"commit called for transaction that is not open\");\n+\tif (transaction->state != REF_TRANSACTION_PREPARED)\n+\t\tBUG(\"commit called for transaction that is not prepared\");\n \n \t/* Fail if a refname appears more than once in the transaction: */\n \tfor (i = 0; i < transaction->nr; i++)\n@@ -3063,7 +3062,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,\n \t\tgoto cleanup;\n \t}\n \n-\tif (initial_ref_transaction_commit(packed_transaction, err)) {\n+\tif (ref_transaction_commit(packed_transaction, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t}\n \n@@ -3091,6 +3090,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \tassert(err);\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n+\t\treturn files_transaction_finish_initial(refs, transaction, err);\n \tif (!transaction->nr) {\n \t\ttransaction->state = REF_TRANSACTION_CLOSED;\n \t\treturn 0;\n@@ -3617,7 +3618,6 @@ struct ref_storage_be refs_be_files = {\n \t.transaction_prepare = files_transaction_prepare,\n \t.transaction_finish = files_transaction_finish,\n \t.transaction_abort = files_transaction_abort,\n-\t.initial_transaction_commit = files_initial_transaction_commit,\n \n \t.pack_refs = files_pack_refs,\n \t.rename_ref = files_rename_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex 07c57fd541a5039d5fcb93d9bf78e1916f67b219..794471de60c78494f1f2b0e9de013422e3e7625d 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -1730,13 +1730,6 @@ static int packed_transaction_finish(struct ref_store *ref_store,\n \treturn ret;\n }\n \n-static int packed_initial_transaction_commit(struct ref_store *ref_store UNUSED,\n-\t\t\t\t\t    struct ref_transaction *transaction,\n-\t\t\t\t\t    struct strbuf *err)\n-{\n-\treturn ref_transaction_commit(transaction, err);\n-}\n-\n static int packed_pack_refs(struct ref_store *ref_store UNUSED,\n \t\t\t    struct pack_refs_opts *pack_opts UNUSED)\n {\n@@ -1769,7 +1762,6 @@ struct ref_storage_be refs_be_packed = {\n \t.transaction_prepare = packed_transaction_prepare,\n \t.transaction_finish = packed_transaction_finish,\n \t.transaction_abort = packed_transaction_abort,\n-\t.initial_transaction_commit = packed_initial_transaction_commit,\n \n \t.pack_refs = packed_pack_refs,\n \t.rename_ref = NULL,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex dbc6360c5a1d410c192e7eee1bffb1d423e1f9ee..33335d54c9162755c70174093017439c0018f36d 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -666,7 +666,6 @@ struct ref_storage_be {\n \tref_transaction_prepare_fn *transaction_prepare;\n \tref_transaction_finish_fn *transaction_finish;\n \tref_transaction_abort_fn *transaction_abort;\n-\tref_transaction_commit_fn *initial_transaction_commit;\n \n \tpack_refs_fn *pack_refs;\n \trename_ref_fn *rename_ref;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 38eb14d591ec0c7c221b6b0f7483e547748e7f1c..8e914afc817f198bed3199630b430e179cabc740 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1490,13 +1490,6 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,\n \treturn ret;\n }\n \n-static int reftable_be_initial_transaction_commit(struct ref_store *ref_store UNUSED,\n-\t\t\t\t\t\t  struct ref_transaction *transaction,\n-\t\t\t\t\t\t  struct strbuf *err)\n-{\n-\treturn ref_transaction_commit(transaction, err);\n-}\n-\n static int reftable_be_pack_refs(struct ref_store *ref_store,\n \t\t\t\t struct pack_refs_opts *opts)\n {\n@@ -2490,7 +2483,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.transaction_prepare = reftable_be_transaction_prepare,\n \t.transaction_finish = reftable_be_transaction_finish,\n \t.transaction_abort = reftable_be_transaction_abort,\n-\t.initial_transaction_commit = reftable_be_initial_transaction_commit,\n \n \t.pack_refs = reftable_be_pack_refs,\n \t.rename_ref = reftable_be_rename_ref,\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507689","messageId":"20241120-pks-refs-optimize-migrations-v2-4-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 04/10] refs/files: support symbolic and root refs in initial transaction","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:33Z","receivedAt":"2024-11-20T07:51:48Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The \"files\" backend has implemented special logic when committing\nthe first transactions in an otherwise empty ref store: instead of\nwriting all refs as separate loose files, it instead knows to write them\nall into a \"packed-refs\" file directly. This is significantly more\nefficient than having to write each of the refs as separate \"loose\" ref.\n\nThe only user of this optimization is git-clone(1), which only uses this\nmechanism to write regular refs. Consequently, the implementation does\nnot know how to handle both symbolic and root refs. While fine in the\ncontext of git-clone(1), this keeps us from using the mechanism in more\ncases.\n\nAdapt the logic to also support symbolic and root refs by using a second\ntransaction that we use for all of the refs that need to be written as\nloose refs.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs/files-backend.c | 44 ++++++++++++++++++++++++++++++++++----------\n 1 file changed, 34 insertions(+), 10 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 3ed18475a72aa4798d15b2912c37b4caabd47aac..116d4259697b20583cb2db34ed47025e8781cd42 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2995,6 +2995,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tint ret = 0;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n \tstruct ref_transaction *packed_transaction = NULL;\n+\tstruct ref_transaction *loose_transaction = NULL;\n \n \tassert(err);\n \n@@ -3040,6 +3041,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\tif ((update->flags & REF_HAVE_OLD) &&\n \t\t    !is_null_oid(&update->old_oid))\n \t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n+\n \t\tif (refs_verify_refname_available(&refs->base, update->refname,\n \t\t\t\t\t\t  &affected_refnames, NULL,\n \t\t\t\t\t\t  err)) {\n@@ -3048,26 +3050,48 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t}\n \n \t\t/*\n-\t\t * Add a reference creation for this reference to the\n-\t\t * packed-refs transaction:\n+\t\t * packed-refs don't support symbolic refs and root refs, so we\n+\t\t * have to queue these references via the loose transaction.\n \t\t */\n-\t\tref_transaction_add_update(packed_transaction, update->refname,\n-\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n-\t\t\t\t\t   &update->new_oid, &update->old_oid,\n-\t\t\t\t\t   NULL, NULL, NULL);\n+\t\tif (update->new_target || is_root_ref(update->refname)) {\n+\t\t\tif (!loose_transaction) {\n+\t\t\t\tloose_transaction = ref_store_transaction_begin(&refs->base, 0, err);\n+\t\t\t\tif (!loose_transaction) {\n+\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tgoto cleanup;\n+\t\t\t\t}\n+\t\t\t}\n+\n+\t\t\tref_transaction_add_update(loose_transaction, update->refname,\n+\t\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t\t   update->new_target ? NULL : &update->new_oid, NULL,\n+\t\t\t\t\t\t   update->new_target, NULL, NULL);\n+\t\t} else {\n+\t\t\tref_transaction_add_update(packed_transaction, update->refname,\n+\t\t\t\t\t\t   update->flags & ~REF_HAVE_OLD,\n+\t\t\t\t\t\t   &update->new_oid, &update->old_oid,\n+\t\t\t\t\t\t   NULL, NULL, NULL);\n+\t\t}\n \t}\n \n-\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n+\tif (packed_refs_lock(refs->packed_ref_store, 0, err) ||\n+\t    ref_transaction_commit(packed_transaction, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\n \t}\n+\tpacked_refs_unlock(refs->packed_ref_store);\n \n-\tif (ref_transaction_commit(packed_transaction, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n+\tif (loose_transaction) {\n+\t\tif (ref_transaction_prepare(loose_transaction, err) ||\n+\t\t    ref_transaction_commit(loose_transaction, err)) {\n+\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tgoto cleanup;\n+\t\t}\n \t}\n \n-\tpacked_refs_unlock(refs->packed_ref_store);\n cleanup:\n+\tif (loose_transaction)\n+\t\tref_transaction_free(loose_transaction);\n \tif (packed_transaction)\n \t\tref_transaction_free(packed_transaction);\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507691","messageId":"20241120-pks-refs-optimize-migrations-v2-5-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 05/10] refs: use \"initial\" transaction semantics to migrate refs","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:34Z","receivedAt":"2024-11-20T07:51:50Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Until now, we couldn't use \"initial\" transaction semantics to migrate\nrefs because the \"files\" backend only supported writing regular refs via\nthe initial transaction because it simply mapped the transaction to a\n\"packed-refs\" transaction. But with the preceding commit, the \"files\"\nbackend has learned to also write symbolic and root refs in the initial\ntransaction by creating a second transaction for all refs that need to\nbe written as loose refs.\n\nAdapt the code to migrate refs to commit the transaction as an initial\ntransaction. This results in a signiticant speedup when migrating many\nrefs:\n\n    Benchmark 1: migrate reftable:files (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):      3.247 s ±  0.034 s    [User: 0.485 s, System: 2.722 s]\n      Range (min … max):    3.216 s …  3.309 s    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):     453.6 ms ±   1.9 ms    [User: 214.6 ms, System: 230.5 ms]\n      Range (min … max):   451.5 ms … 456.4 ms    10 runs\n\n    Summary\n      migrate reftable:files (refcount = 100000, revision = HEAD) ran\n        7.16 ± 0.08 times faster than migrate reftable:files (refcount = 100000, revision = HEAD~)\n\nAs the reftable backend doesn't (yet) special-case initial transactions\nthere is no comparable speedup for that backend.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c                  | 10 ++--------\n t/t1460-refs-migrate.sh |  2 +-\n 2 files changed, 3 insertions(+), 9 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 8b9dfc6173fd144fe9a172cb81cf33057ae31368..0f10c565bbb4e0d91210c52a3221a224e4264d28 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2827,7 +2827,8 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\ttransaction = ref_store_transaction_begin(new_refs, 0, errbuf);\n+\ttransaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL,\n+\t\t\t\t\t\t  errbuf);\n \tif (!transaction)\n \t\tgoto done;\n \n@@ -2852,13 +2853,6 @@ int repo_migrate_ref_storage_format(struct repository *repo,\n \tif (ret < 0)\n \t\tgoto done;\n \n-\t/*\n-\t * TODO: we might want to migrate to `initial_ref_transaction_commit()`\n-\t * here, which is more efficient for the files backend because it would\n-\t * write new refs into the packed-refs file directly. At this point,\n-\t * the files backend doesn't handle pseudo-refs and symrefs correctly\n-\t * though, so this requires some more work.\n-\t */\n \tret = ref_transaction_commit(transaction, errbuf);\n \tif (ret < 0)\n \t\tgoto done;\ndiff --git a/t/t1460-refs-migrate.sh b/t/t1460-refs-migrate.sh\nindex f7c0783d30ccd61b0fee67c115193b42bb0e2c77..b90b38a87f7bb905afeeceb4f9a3bfc8b772e16a 100755\n--- a/t/t1460-refs-migrate.sh\n+++ b/t/t1460-refs-migrate.sh\n@@ -237,7 +237,7 @@ test_expect_success 'migrating from reftable format deletes backend files' '\n \ttest_path_is_missing repo/.git/reftable &&\n \techo \"ref: refs/heads/main\" >expect &&\n \ttest_cmp expect repo/.git/HEAD &&\n-\ttest_path_is_file repo/.git/refs/heads/main\n+\ttest_path_is_file repo/.git/packed-refs\n '\n \n test_done\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507692","messageId":"20241120-pks-refs-optimize-migrations-v2-6-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 06/10] refs: skip collision checks in initial transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:35Z","receivedAt":"2024-11-20T07:51:51Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Reference transactions use `refs_verify_refname_available()` to check\nfor colliding references. This check consists of two parts:\n\n  - Checks for whether multiple ref updates in the same transaction\n    conflict with each other.\n\n  - Checks for whether existing refs conflict with any refs part of the\n    transaction.\n\nWhile we generally cannot avoid the first check, the second check is\nsuperfluous in cases where the transaction is an initial one in an\notherwise empty ref store. The check results in multiple ref reads as\nwell as the creation of a ref iterator for every ref we're checking,\nwhich adds up quite fast when performing the check for many refs.\n\nIntroduce a new flag that allows us to skip this check and wire it up in\nsuch that the backends pass it when running an initial transaction. This\nleads to significant speedups when migrating ref storage backends. From\n\"files\" to \"reftable\":\n\n    Benchmark 1: migrate files:reftable (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):     472.4 ms ±   6.7 ms    [User: 175.9 ms, System: 285.2 ms]\n      Range (min … max):   463.5 ms … 483.2 ms    10 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):      86.1 ms ±   1.9 ms    [User: 67.9 ms, System: 16.0 ms]\n      Range (min … max):    82.9 ms …  90.9 ms    29 runs\n\n    Summary\n      migrate files:reftable (refcount = 100000, revision = HEAD) ran\n        5.48 ± 0.15 times faster than migrate files:reftable (refcount = 100000, revision = HEAD~)\n\nAnd from \"reftable\" to \"files\":\n\n    Benchmark 1: migrate reftable:files (refcount = 100000, revision = HEAD~)\n      Time (mean ± σ):     452.7 ms ±   3.4 ms    [User: 209.9 ms, System: 235.4 ms]\n      Range (min … max):   445.9 ms … 457.5 ms    10 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 100000, revision = HEAD)\n      Time (mean ± σ):      95.2 ms ±   2.2 ms    [User: 73.6 ms, System: 20.6 ms]\n      Range (min … max):    91.7 ms … 100.8 ms    28 runs\n\n    Summary\n      migrate reftable:files (refcount = 100000, revision = HEAD) ran\n        4.76 ± 0.11 times faster than migrate reftable:files (refcount = 100000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c                    | 37 +++++++++++++++++++++----------------\n refs.h                    |  5 ++++-\n refs/files-backend.c      | 13 ++++++-------\n refs/reftable-backend.c   |  6 ++++--\n t/helper/test-ref-store.c |  2 +-\n 5 files changed, 36 insertions(+), 27 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 0f10c565bbb4e0d91210c52a3221a224e4264d28..d690eb19b3fd7083a5309deb98738547e4f48040 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2324,6 +2324,7 @@ int refs_verify_refname_available(struct ref_store *refs,\n \t\t\t\t  const char *refname,\n \t\t\t\t  const struct string_list *extras,\n \t\t\t\t  const struct string_list *skip,\n+\t\t\t\t  int initial_transaction,\n \t\t\t\t  struct strbuf *err)\n {\n \tconst char *slash;\n@@ -2332,8 +2333,6 @@ int refs_verify_refname_available(struct ref_store *refs,\n \tstruct strbuf referent = STRBUF_INIT;\n \tstruct object_id oid;\n \tunsigned int type;\n-\tstruct ref_iterator *iter;\n-\tint ok;\n \tint ret = -1;\n \n \t/*\n@@ -2363,7 +2362,8 @@ int refs_verify_refname_available(struct ref_store *refs,\n \t\tif (skip && string_list_has_string(skip, dirname.buf))\n \t\t\tcontinue;\n \n-\t\tif (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n+\t\tif (!initial_transaction &&\n+\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n \t\t\t\t       &type, &ignore_errno)) {\n \t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n \t\t\t\t    dirname.buf, refname);\n@@ -2388,21 +2388,26 @@ int refs_verify_refname_available(struct ref_store *refs,\n \tstrbuf_addstr(&dirname, refname + dirname.len);\n \tstrbuf_addch(&dirname, '/');\n \n-\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n-\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n-\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n-\t\tif (skip &&\n-\t\t    string_list_has_string(skip, iter->refname))\n-\t\t\tcontinue;\n+\tif (!initial_transaction) {\n+\t\tstruct ref_iterator *iter;\n+\t\tint ok;\n \n-\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n-\t\t\t    iter->refname, refname);\n-\t\tref_iterator_abort(iter);\n-\t\tgoto cleanup;\n-\t}\n+\t\titer = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,\n+\t\t\t\t\t       DO_FOR_EACH_INCLUDE_BROKEN);\n+\t\twhile ((ok = ref_iterator_advance(iter)) == ITER_OK) {\n+\t\t\tif (skip &&\n+\t\t\t    string_list_has_string(skip, iter->refname))\n+\t\t\t\tcontinue;\n \n-\tif (ok != ITER_DONE)\n-\t\tBUG(\"error while iterating over references\");\n+\t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n+\t\t\t\t    iter->refname, refname);\n+\t\t\tref_iterator_abort(iter);\n+\t\t\tgoto cleanup;\n+\t\t}\n+\n+\t\tif (ok != ITER_DONE)\n+\t\t\tBUG(\"error while iterating over references\");\n+\t}\n \n \textra_refname = find_descendant_ref(dirname.buf, extras, skip);\n \tif (extra_refname)\ndiff --git a/refs.h b/refs.h\nindex 024a370554e013d66febee635e4c0415ba061fe6..980bd20cf24e15144aeff991eeba8b27a936d386 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -101,13 +101,16 @@ int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,\n  * both \"foo\" and with \"foo/bar/baz\" but not with \"foo/bar\" or\n  * \"foo/barbados\".\n  *\n+ * If `initial_transaction` is truish, then all collision checks with\n+ * preexisting refs are skipped.\n+ *\n  * extras and skip must be sorted.\n  */\n-\n int refs_verify_refname_available(struct ref_store *refs,\n \t\t\t\t  const char *refname,\n \t\t\t\t  const struct string_list *extras,\n \t\t\t\t  const struct string_list *skip,\n+\t\t\t\t  int initial_transaction,\n \t\t\t\t  struct strbuf *err);\n \n int refs_ref_exists(struct ref_store *refs, const char *refname);\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 116d4259697b20583cb2db34ed47025e8781cd42..d27806c02c272f8bddc789b509e3c3c7af4f75aa 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -706,7 +706,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t * reason to expect this error to be transitory.\n \t\t */\n \t\tif (refs_verify_refname_available(&refs->base, refname,\n-\t\t\t\t\t\t  extras, NULL, err)) {\n+\t\t\t\t\t\t  extras, NULL, 0, err)) {\n \t\t\tif (mustexist) {\n \t\t\t\t/*\n \t\t\t\t * To the user the relevant error is\n@@ -813,7 +813,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n \t\t\t\tif (refs_verify_refname_available(\n \t\t\t\t\t\t    &refs->base, refname,\n-\t\t\t\t\t\t    extras, NULL, err)) {\n+\t\t\t\t\t\t    extras, NULL, 0, err)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * The error message set by\n \t\t\t\t\t * verify_refname_available() is OK.\n@@ -850,7 +850,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t */\n \t\tif (refs_verify_refname_available(\n \t\t\t\t    refs->packed_ref_store, refname,\n-\t\t\t\t    extras, NULL, err)) {\n+\t\t\t\t    extras, NULL, 0, err)) {\n \t\t\tret = TRANSACTION_NAME_CONFLICT;\n \t\t\tgoto error_return;\n \t\t}\n@@ -1159,7 +1159,7 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,\n \t */\n \tif (is_null_oid(&lock->old_oid) &&\n \t    refs_verify_refname_available(refs->packed_ref_store, refname,\n-\t\t\t\t\t  NULL, NULL, err))\n+\t\t\t\t\t  NULL, NULL, 0, err))\n \t\tgoto error_return;\n \n \tlock->ref_name = xstrdup(refname);\n@@ -1538,7 +1538,7 @@ static int refs_rename_ref_available(struct ref_store *refs,\n \n \tstring_list_insert(&skip, old_refname);\n \tok = !refs_verify_refname_available(refs, new_refname,\n-\t\t\t\t\t    NULL, &skip, &err);\n+\t\t\t\t\t    NULL, &skip, 0, &err);\n \tif (!ok)\n \t\terror(\"%s\", err.buf);\n \n@@ -3043,8 +3043,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t\tBUG(\"initial ref transaction with old_sha1 set\");\n \n \t\tif (refs_verify_refname_available(&refs->base, update->refname,\n-\t\t\t\t\t\t  &affected_refnames, NULL,\n-\t\t\t\t\t\t  err)) {\n+\t\t\t\t\t\t  &affected_refnames, NULL, 1, err)) {\n \t\t\tret = TRANSACTION_NAME_CONFLICT;\n \t\t\tgoto cleanup;\n \t\t}\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 8e914afc817f198bed3199630b430e179cabc740..bbc779ab410b41f00759a3a41a76dd708f115c90 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1097,7 +1097,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t * at a later point.\n \t\t\t */\n \t\t\tret = refs_verify_refname_available(ref_store, u->refname,\n-\t\t\t\t\t\t\t    &affected_refnames, NULL, err);\n+\t\t\t\t\t\t\t    &affected_refnames, NULL,\n+\t\t\t\t\t\t\t    transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n+\t\t\t\t\t\t\t    err);\n \t\t\tif (ret < 0)\n \t\t\t\tgoto done;\n \n@@ -1584,7 +1586,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tif (arg->delete_old)\n \t\tstring_list_insert(&skip, arg->oldname);\n \tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n-\t\t\t\t\t    NULL, &skip, &errbuf);\n+\t\t\t\t\t    NULL, &skip, 0, &errbuf);\n \tif (ret < 0) {\n \t\terror(\"%s\", errbuf.buf);\n \t\tgoto done;\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex 65346dee551ccd781a88786f0c8465f60b286cde..240f6fc29d7f1bb20658deee467bcb46ac3407b2 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -199,7 +199,7 @@ static int cmd_verify_ref(struct ref_store *refs, const char **argv)\n \tstruct strbuf err = STRBUF_INIT;\n \tint ret;\n \n-\tret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);\n+\tret = refs_verify_refname_available(refs, refname, NULL, NULL, 0, &err);\n \tif (err.len)\n \t\tputs(err.buf);\n \treturn ret;\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507693","messageId":"20241120-pks-refs-optimize-migrations-v2-7-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 07/10] refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:36Z","receivedAt":"2024-11-20T07:51:52Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"When the `REF_SKIP_CREATE_REFLOG` flag is set we skip the creation of\nthe reflog entry, but we still normalize the reflog message when we\nqueue the update. This is a waste of resources as the normalized message\nwill never get used in the first place.\n\nFix this issue by skipping the normalization in case the flag is set.\nThis leads to a surprisingly large speedup when migrating from the\n\"files\" to the \"reftable\" backend:\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     878.5 ms ±  14.9 ms    [User: 726.5 ms, System: 139.2 ms]\n      Range (min … max):   858.4 ms … 941.3 ms    50 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     831.1 ms ±  10.5 ms    [User: 694.1 ms, System: 126.3 ms]\n      Range (min … max):   812.4 ms … 851.4 ms    50 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = HEAD) ran\n        1.06 ± 0.02 times faster than migrate files:reftable (refcount = 1000000, revision = HEAD~)\n\nAnd an ever larger speedup when migrating the other way round:\n\n    Benchmark 1: migrate reftable:files (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     923.6 ms ±  11.6 ms    [User: 705.5 ms, System: 208.1 ms]\n      Range (min … max):   905.3 ms … 946.5 ms    50 runs\n\n    Benchmark 2: migrate reftable:files (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     818.5 ms ±   9.0 ms    [User: 627.6 ms, System: 180.6 ms]\n      Range (min … max):   802.2 ms … 842.9 ms    50 runs\n\n    Summary\n      migrate reftable:files (refcount = 1000000, revision = HEAD) ran\n        1.13 ± 0.02 times faster than migrate reftable:files (refcount = 1000000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n refs.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex d690eb19b3fd7083a5309deb98738547e4f48040..65eea3eb7734d03f09a22e8edfe5074d532398ff 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1188,8 +1188,9 @@ struct ref_update *ref_transaction_add_update(\n \t\toidcpy(&update->new_oid, new_oid);\n \tif ((flags & REF_HAVE_OLD) && old_oid)\n \t\toidcpy(&update->old_oid, old_oid);\n+\tif (!(flags & REF_SKIP_CREATE_REFLOG))\n+\t\tupdate->msg = normalize_reflog_message(msg);\n \n-\tupdate->msg = normalize_reflog_message(msg);\n \treturn update;\n }\n \n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507694","messageId":"20241120-pks-refs-optimize-migrations-v2-8-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 08/10] reftable/writer: optimize allocations by using a scratch buffer","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:37Z","receivedAt":"2024-11-20T07:51:53Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Both `writer_add_record()` and `reftable_writer_add_ref()` get executed\nfor every single ref record we're adding to the reftable writer. And as\nboth functions use a local buffer to write data, the allocations we have\nto do here add up during larger transactions.\n\nRefactor the code to use a scratch buffer part of the `reftable_writer`\nitself such that we can reuse it. This signifcantly reduces the number\nof allocations during large transactions, e.g. when migrating refs from\nthe \"files\" backend to the \"reftable\" backend. Before this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 5,032,171 allocs, 5,032,122 frees, 418,792,092 bytes allocated\n\nAfter this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated\n\nThis also translate into a small speedup:\n\n    Benchmark 1: migrate files:reftable (refcount = 1000000, revision = HEAD~)\n      Time (mean ± σ):     827.2 ms ±  16.5 ms    [User: 689.4 ms, System: 124.9 ms]\n      Range (min … max):   809.0 ms … 924.7 ms    50 runs\n\n    Benchmark 2: migrate files:reftable (refcount = 1000000, revision = HEAD)\n      Time (mean ± σ):     813.6 ms ±  11.6 ms    [User: 679.0 ms, System: 123.4 ms]\n      Range (min … max):   786.7 ms … 833.5 ms    50 runs\n\n    Summary\n      migrate files:reftable (refcount = 1000000, revision = HEAD) ran\n        1.02 ± 0.02 times faster than migrate files:reftable (refcount = 1000000, revision = HEAD~)\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/writer.c | 23 +++++++++++------------\n reftable/writer.h |  1 +\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex fd136794d5a27b33b5017f36fbd6b095ab8dac5b..6501376ce826469556a7dfa3c39258847300ae66 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -148,6 +148,7 @@ int reftable_writer_new(struct reftable_writer **out,\n \n \treftable_buf_init(&wp->block_writer_data.last_key);\n \treftable_buf_init(&wp->last_key);\n+\treftable_buf_init(&wp->buf);\n \tREFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);\n \tif (!wp->block) {\n \t\treftable_free(wp);\n@@ -180,6 +181,7 @@ static void writer_release(struct reftable_writer *w)\n \t\tw->block_writer = NULL;\n \t\twriter_clear_index(w);\n \t\treftable_buf_release(&w->last_key);\n+\t\treftable_buf_release(&w->buf);\n \t}\n }\n \n@@ -249,20 +251,19 @@ static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *has\n static int writer_add_record(struct reftable_writer *w,\n \t\t\t     struct reftable_record *rec)\n {\n-\tstruct reftable_buf key = REFTABLE_BUF_INIT;\n \tint err;\n \n-\terr = reftable_record_key(rec, &key);\n+\terr = reftable_record_key(rec, &w->buf);\n \tif (err < 0)\n \t\tgoto done;\n \n-\tif (reftable_buf_cmp(&w->last_key, &key) >= 0) {\n+\tif (reftable_buf_cmp(&w->last_key, &w->buf) >= 0) {\n \t\terr = REFTABLE_API_ERROR;\n \t\tgoto done;\n \t}\n \n \treftable_buf_reset(&w->last_key);\n-\terr = reftable_buf_add(&w->last_key, key.buf, key.len);\n+\terr = reftable_buf_add(&w->last_key, w->buf.buf, w->buf.len);\n \tif (err < 0)\n \t\tgoto done;\n \n@@ -312,7 +313,6 @@ static int writer_add_record(struct reftable_writer *w,\n \t}\n \n done:\n-\treftable_buf_release(&key);\n \treturn err;\n }\n \n@@ -325,7 +325,6 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \t\t\t.ref = *ref\n \t\t},\n \t};\n-\tstruct reftable_buf buf = REFTABLE_BUF_INIT;\n \tint err;\n \n \tif (!ref->refname ||\n@@ -340,24 +339,25 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \t\tgoto out;\n \n \tif (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) {\n-\t\terr = reftable_buf_add(&buf, (char *)reftable_ref_record_val1(ref),\n+\t\treftable_buf_reset(&w->buf);\n+\t\terr = reftable_buf_add(&w->buf, (char *)reftable_ref_record_val1(ref),\n \t\t\t\t       hash_size(w->opts.hash_id));\n \t\tif (err < 0)\n \t\t\tgoto out;\n \n-\t\terr = writer_index_hash(w, &buf);\n+\t\terr = writer_index_hash(w, &w->buf);\n \t\tif (err < 0)\n \t\t\tgoto out;\n \t}\n \n \tif (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) {\n-\t\treftable_buf_reset(&buf);\n-\t\terr = reftable_buf_add(&buf, reftable_ref_record_val2(ref),\n+\t\treftable_buf_reset(&w->buf);\n+\t\terr = reftable_buf_add(&w->buf, reftable_ref_record_val2(ref),\n \t\t\t\t       hash_size(w->opts.hash_id));\n \t\tif (err < 0)\n \t\t\tgoto out;\n \n-\t\terr = writer_index_hash(w, &buf);\n+\t\terr = writer_index_hash(w, &w->buf);\n \t\tif (err < 0)\n \t\t\tgoto out;\n \t}\n@@ -365,7 +365,6 @@ int reftable_writer_add_ref(struct reftable_writer *w,\n \terr = 0;\n \n out:\n-\treftable_buf_release(&buf);\n \treturn err;\n }\n \ndiff --git a/reftable/writer.h b/reftable/writer.h\nindex e8a6fbb78543e6e56920a2999601db0db9fe4d97..421a897dccd85ad0532860ff1b4f38b2813d438d 100644\n--- a/reftable/writer.h\n+++ b/reftable/writer.h\n@@ -20,6 +20,7 @@ struct reftable_writer {\n \tvoid *write_arg;\n \tint pending_padding;\n \tstruct reftable_buf last_key;\n+\tstruct reftable_buf buf;\n \n \t/* offset of next block to write. */\n \tuint64_t next;\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507695","messageId":"20241120-pks-refs-optimize-migrations-v2-9-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 09/10] reftable/block: rename `block_writer::buf` variable","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:38Z","receivedAt":"2024-11-20T07:51:54Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Adapt the name of the `block_writer::buf` variable to instead be called\n`block`. This aligns it with the existing `block_len` variable, which\ntracks the length of this buffer, and is generally a bit more tied to\nthe actual context where this variable gets used.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/block.c | 20 ++++++++++----------\n reftable/block.h |  8 ++++----\n 2 files changed, 14 insertions(+), 14 deletions(-)\n\ndiff --git a/reftable/block.c b/reftable/block.c\nindex f5b432566a6b9f171a1f1374b6c892ab0696d744..3fa36c002a0c1852790780e74a6e055161f857d9 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -70,14 +70,14 @@ static int block_writer_register_restart(struct block_writer *w, int n,\n \treturn 0;\n }\n \n-int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n+int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,\n \t\t      uint32_t block_size, uint32_t header_off, int hash_size)\n {\n-\tbw->buf = buf;\n+\tbw->block = block;\n \tbw->hash_size = hash_size;\n \tbw->block_size = block_size;\n \tbw->header_off = header_off;\n-\tbw->buf[header_off] = typ;\n+\tbw->block[header_off] = typ;\n \tbw->next = header_off + 4;\n \tbw->restart_interval = 16;\n \tbw->entries = 0;\n@@ -95,7 +95,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n \n uint8_t block_writer_type(struct block_writer *bw)\n {\n-\treturn bw->buf[bw->header_off];\n+\treturn bw->block[bw->header_off];\n }\n \n /* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on\n@@ -107,7 +107,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \tstruct reftable_buf last =\n \t\tw->entries % w->restart_interval == 0 ? empty : w->last_key;\n \tstruct string_view out = {\n-\t\t.buf = w->buf + w->next,\n+\t\t.buf = w->block + w->next,\n \t\t.len = w->block_size - w->next,\n \t};\n \n@@ -153,13 +153,13 @@ int block_writer_finish(struct block_writer *w)\n {\n \tint i;\n \tfor (i = 0; i < w->restart_len; i++) {\n-\t\tput_be24(w->buf + w->next, w->restarts[i]);\n+\t\tput_be24(w->block + w->next, w->restarts[i]);\n \t\tw->next += 3;\n \t}\n \n-\tput_be16(w->buf + w->next, w->restart_len);\n+\tput_be16(w->block + w->next, w->restart_len);\n \tw->next += 2;\n-\tput_be24(w->buf + 1 + w->header_off, w->next);\n+\tput_be24(w->block + 1 + w->header_off, w->next);\n \n \t/*\n \t * Log records are stored zlib-compressed. Note that the compression\n@@ -188,7 +188,7 @@ int block_writer_finish(struct block_writer *w)\n \n \t\tw->zstream->next_out = w->compressed;\n \t\tw->zstream->avail_out = compressed_len;\n-\t\tw->zstream->next_in = w->buf + block_header_skip;\n+\t\tw->zstream->next_in = w->block + block_header_skip;\n \t\tw->zstream->avail_in = src_len;\n \n \t\t/*\n@@ -206,7 +206,7 @@ int block_writer_finish(struct block_writer *w)\n \t\t * adjust the `next` pointer to point right after the\n \t\t * compressed data.\n \t\t */\n-\t\tmemcpy(w->buf + block_header_skip, w->compressed,\n+\t\tmemcpy(w->block + block_header_skip, w->compressed,\n \t\t       w->zstream->total_out);\n \t\tw->next = w->zstream->total_out + block_header_skip;\n \t}\ndiff --git a/reftable/block.h b/reftable/block.h\nindex 9a3effa513420039ee3f2834339c5082f64339d0..b3f837d612a8f0fbe98430b04e2dddaa975a15ab 100644\n--- a/reftable/block.h\n+++ b/reftable/block.h\n@@ -22,7 +22,7 @@ struct block_writer {\n \tunsigned char *compressed;\n \tsize_t compressed_cap;\n \n-\tuint8_t *buf;\n+\tuint8_t *block;\n \tuint32_t block_size;\n \n \t/* Offset of the global header. Nonzero in the first block only. */\n@@ -43,9 +43,9 @@ struct block_writer {\n };\n \n /*\n- * initializes the blockwriter to write `typ` entries, using `buf` as temporary\n- * storage. `buf` is not owned by the block_writer. */\n-int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,\n+ * initializes the blockwriter to write `typ` entries, using `block` as temporary\n+ * storage. `block` is not owned by the block_writer. */\n+int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,\n \t\t      uint32_t block_size, uint32_t header_off, int hash_size);\n \n /* returns the block type (eg. 'r' for ref records. */\n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507696","messageId":"20241120-pks-refs-optimize-migrations-v2-10-a233374b7452@pks.im","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-0-a233374b7452@pks.im","subject":"[PATCH v2 10/10] reftable/block: optimize allocations by using scratch buffer","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-11-20T07:51:39Z","receivedAt":"2024-11-20T07:51:55Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The block writer needs to compute the key for every record that one adds\nto the writer. The buffer for this key is stored on the stack and thus\nreallocated on every call to `block_writer_add()`, which is inefficient.\n\nRefactor the code so that we store the buffer in the `block_writer`\nstruct itself so that we can reuse it. This reduces the number of\nallocations when writing many refs, e.g. when migrating one million refs\nfrom the \"files\" backend to the \"reftable backend. Before this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated\n\nAfter this change:\n\n    HEAP SUMMARY:\n        in use at exit: 80,048 bytes in 49 blocks\n      total heap usage: 2,013,250 allocs, 2,013,201 frees, 347,543,583 bytes allocated\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/block.c | 13 +++++--------\n reftable/block.h |  1 +\n 2 files changed, 6 insertions(+), 8 deletions(-)\n\ndiff --git a/reftable/block.c b/reftable/block.c\nindex 3fa36c002a0c1852790780e74a6e055161f857d9..1aa7e8cd3cbf0980f6bc20262be89e755d0a4b4b 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -110,24 +110,21 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \t\t.buf = w->block + w->next,\n \t\t.len = w->block_size - w->next,\n \t};\n-\n \tstruct string_view start = out;\n-\n \tint is_restart = 0;\n-\tstruct reftable_buf key = REFTABLE_BUF_INIT;\n \tint n = 0;\n \tint err;\n \n-\terr = reftable_record_key(rec, &key);\n+\terr = reftable_record_key(rec, &w->buf);\n \tif (err < 0)\n \t\tgoto done;\n \n-\tif (!key.len) {\n+\tif (!w->buf.len) {\n \t\terr = REFTABLE_API_ERROR;\n \t\tgoto done;\n \t}\n \n-\tn = reftable_encode_key(&is_restart, out, last, key,\n+\tn = reftable_encode_key(&is_restart, out, last, w->buf,\n \t\t\t\treftable_record_val_type(rec));\n \tif (n < 0) {\n \t\terr = -1;\n@@ -143,9 +140,8 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n \tstring_view_consume(&out, n);\n \n \terr = block_writer_register_restart(w, start.len - out.len, is_restart,\n-\t\t\t\t\t    &key);\n+\t\t\t\t\t    &w->buf);\n done:\n-\treftable_buf_release(&key);\n \treturn err;\n }\n \n@@ -569,6 +565,7 @@ void block_writer_release(struct block_writer *bw)\n \tREFTABLE_FREE_AND_NULL(bw->zstream);\n \tREFTABLE_FREE_AND_NULL(bw->restarts);\n \tREFTABLE_FREE_AND_NULL(bw->compressed);\n+\treftable_buf_release(&bw->buf);\n \treftable_buf_release(&bw->last_key);\n \t/* the block is not owned. */\n }\ndiff --git a/reftable/block.h b/reftable/block.h\nindex b3f837d612a8f0fbe98430b04e2dddaa975a15ab..d76f00553073c10185e716e71e2f415ce5dcf7e2 100644\n--- a/reftable/block.h\n+++ b/reftable/block.h\n@@ -39,6 +39,7 @@ struct block_writer {\n \tuint32_t restart_cap;\n \n \tstruct reftable_buf last_key;\n+\tstruct reftable_buf buf;\n \tint entries;\n };\n \n\n-- \n2.47.0.274.g962d0b743d.dirty\n\n"},{"id":"507705","messageId":"CAP8UFD0xMcqfJT+JnqqFSSiN0QOzsJ7pvQmZ_SXtxz3f5iJVBA@mail.gmail.com","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-1-a233374b7452@pks.im","subject":"Re: [PATCH v2 01/10] refs: allow passing flags when setting up a transaction","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2024-11-20T10:19:14Z","receivedAt":"2024-11-20T10:19:27Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Nov 20, 2024 at 8:54 AM Patrick Steinhardt <ps@pks.im> wrote:\n>\n> Allow passing flags when setting up a transaction such that the\n> behaviour of the transaction itself can be altered. This functionality\n> will be used in a subsequent patch.\n>\n> Adapt callers accordingly.\n\nFrom reading the above, it might seem that the change is only about\nadding a new 'flags' argument to the ref_store_transaction_begin()\nfunction that sets up a transaction. When looking at the patch though,\nit appears that it's also about adding a 'flags' field to 'struct\nref_transaction'. It might be nice to mention that in the commit\nmessage too.\n\n\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index 2313c830d8facaa17b0b4b073df0de958023062a..dbc6360c5a1d410c192e7eee1bffb1d423e1f9ee 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -193,6 +193,7 @@ struct ref_transaction {\n>         size_t nr;\n>         enum ref_transaction_state state;\n>         void *backend_data;\n> +       unsigned int flags;\n>  };\n"},{"id":"507706","messageId":"CAP8UFD0i+ayKYwEitf7DYA-Hxo16d89LtHv2i89rHh9cPWR4LA@mail.gmail.com","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-6-a233374b7452@pks.im","subject":"Re: [PATCH v2 06/10] refs: skip collision checks in initial transactions","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2024-11-20T10:21:44Z","receivedAt":"2024-11-20T10:21:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Nov 20, 2024 at 8:54 AM Patrick Steinhardt <ps@pks.im> wrote:\n\n> diff --git a/refs.c b/refs.c\n> index 0f10c565bbb4e0d91210c52a3221a224e4264d28..d690eb19b3fd7083a5309deb98738547e4f48040 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -2324,6 +2324,7 @@ int refs_verify_refname_available(struct ref_store *refs,\n>                                   const char *refname,\n>                                   const struct string_list *extras,\n>                                   const struct string_list *skip,\n> +                                 int initial_transaction,\n\nNit: Using 'unsigned int' instead of 'int' might be slightly better as\nthe type would be the same as \"transaction->flags &\nREF_TRANSACTION_FLAG_INITIAL\" we pass as an argument below. It might\nalso make it slightly simpler to convert \"int initial_transaction\" to\n\"int flags\" if we add more flags and need to do that in the future.\n(Other functions add an 'int initial_transaction' argument which could\nbe an 'unsigned int' instead.)\n\n>                                   struct strbuf *err)\n"},{"id":"507707","messageId":"CAP8UFD35YvWgQUcXT4z6k1foH4wN31KX1Xg_09GR16POs51NGg@mail.gmail.com","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-8-a233374b7452@pks.im","subject":"Re: [PATCH v2 08/10] reftable/writer: optimize allocations by using a scratch buffer","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2024-11-20T10:21:54Z","receivedAt":"2024-11-20T10:22:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Nov 20, 2024 at 9:07 AM Patrick Steinhardt <ps@pks.im> wrote:\n\n> diff --git a/reftable/writer.h b/reftable/writer.h\n> index e8a6fbb78543e6e56920a2999601db0db9fe4d97..421a897dccd85ad0532860ff1b4f38b2813d438d 100644\n> --- a/reftable/writer.h\n> +++ b/reftable/writer.h\n> @@ -20,6 +20,7 @@ struct reftable_writer {\n>         void *write_arg;\n>         int pending_padding;\n>         struct reftable_buf last_key;\n> +       struct reftable_buf buf;\n\nNit: It would be nice to add a comment, so that readers don't have to\nlook at .c files, or the commit message, to find what this field is\nused for.\n\n\n>         /* offset of next block to write. */\n>         uint64_t next;\n"},{"id":"507708","messageId":"CAP8UFD2hjbmqkKcx+8=6uMmaXOZJbL4kJ8X9HtniuRsX=-=rtw@mail.gmail.com","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-10-a233374b7452@pks.im","subject":"Re: [PATCH v2 10/10] reftable/block: optimize allocations by using scratch buffer","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2024-11-20T10:22:00Z","receivedAt":"2024-11-20T10:22:14Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Nov 20, 2024 at 8:56 AM Patrick Steinhardt <ps@pks.im> wrote:\n\n> diff --git a/reftable/block.h b/reftable/block.h\n> index b3f837d612a8f0fbe98430b04e2dddaa975a15ab..d76f00553073c10185e716e71e2f415ce5dcf7e2 100644\n> --- a/reftable/block.h\n> +++ b/reftable/block.h\n> @@ -39,6 +39,7 @@ struct block_writer {\n>         uint32_t restart_cap;\n>\n>         struct reftable_buf last_key;\n> +       struct reftable_buf buf;\n\nNit (similar as for patch 8/10): It would be nice to add a comment, so\nthat readers don't have to look at .c files, or the commit message, to\nfind what this field is used for.\n\n>         int entries;\n>  };\n"},{"id":"507709","messageId":"CAP8UFD1vLm5G=+K=b17_JGCvkm01BmaLE7EcdWhGO6bPWyJ9hA@mail.gmail.com","threadId":"62467","inReplyTo":"Zz2UtKNnREAJgtaI@pks.im","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2024-11-20T10:25:26Z","receivedAt":"2024-11-20T10:25:40Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Nov 20, 2024 at 9:00 AM Patrick Steinhardt <ps@pks.im> wrote:\n\n> Sure, let's do it that way. Will send the updated patch series in a\n> second, thanks!\n\nI took a look at the v2 and found a number of small nits. Not sure\nit's worth a reroll though, as otherwise I found it great!\n\nThanks.\n"},{"id":"507715","messageId":"afed1ddc-90fd-446a-9da0-b8e8f3fbb903@app.fastmail.com","threadId":"62467","inReplyTo":"20241120-pks-refs-optimize-migrations-v2-6-a233374b7452@pks.im","subject":"Re: [PATCH v2 06/10] refs: skip collision checks in initial transactions","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2024-11-20T10:42:29Z","receivedAt":"2024-11-20T10:42:50Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"Hi\n\nOn Wed, Nov 20, 2024, at 08:51, Patrick Steinhardt wrote:\n> While we generally cannot avoid the first check, the second check is\n> superfluous in cases where the transaction is an initial one in an\n> otherwise empty ref store. The check results in multiple ref reads as\n> well as the creation of a ref iterator for every ref we're checking,\n> which adds up quite fast when performing the check for many refs.\n>\n> Introduce a new flag that allows us to skip this check and wire it up in\n> such that the backends pass it when running an initial transaction. This\n\nMissing word? “wire it up in such that”.\n\nMaybe: “wire it up so that”.\n\n> leads to significant speedups when migrating ref storage backends. From\n> \"files\" to \"reftable\":\n\n"},{"id":"508020","messageId":"Z0QQlPXc0gcNohhp@pks.im","threadId":"62467","inReplyTo":"CAP8UFD35YvWgQUcXT4z6k1foH4wN31KX1Xg_09GR16POs51NGg@mail.gmail.com","subject":"Re: [PATCH v2 08/10] reftable/writer: optimize allocations by using a scratch buffer","fromName":"Patrick Steinhardt","fromEmail":"pks@pks.im","sentAt":"2024-11-25T05:52:20Z","receivedAt":"2024-11-25T05:52:36Z","isPatch":true,"sender":{"key":"pks@pks.im","avatar":null},"body":"On Wed, Nov 20, 2024 at 11:21:54AM +0100, Christian Couder wrote:\n> On Wed, Nov 20, 2024 at 9:07 AM Patrick Steinhardt <ps@pks.im> wrote:\n> \n> > diff --git a/reftable/writer.h b/reftable/writer.h\n> > index e8a6fbb78543e6e56920a2999601db0db9fe4d97..421a897dccd85ad0532860ff1b4f38b2813d438d 100644\n> > --- a/reftable/writer.h\n> > +++ b/reftable/writer.h\n> > @@ -20,6 +20,7 @@ struct reftable_writer {\n> >         void *write_arg;\n> >         int pending_padding;\n> >         struct reftable_buf last_key;\n> > +       struct reftable_buf buf;\n> \n> Nit: It would be nice to add a comment, so that readers don't have to\n> look at .c files, or the commit message, to find what this field is\n> used for.\n\nFair enough. I'll also rename it to `scratch` while at it to clarify its\nintent a bit. Other parts of the reftable library already use that name.\n\nPatrick\n"},{"id":"508021","messageId":"Z0QQmmGt27U3Nsfw@pks.im","threadId":"62467","inReplyTo":"CAP8UFD1vLm5G=+K=b17_JGCvkm01BmaLE7EcdWhGO6bPWyJ9hA@mail.gmail.com","subject":"Re: [PATCH 00/10] refs: optimize ref format migrations","fromName":"Patrick Steinhardt","fromEmail":"pks@pks.im","sentAt":"2024-11-25T05:52:26Z","receivedAt":"2024-11-25T05:52:40Z","isPatch":true,"sender":{"key":"pks@pks.im","avatar":null},"body":"On Wed, Nov 20, 2024 at 11:25:26AM +0100, Christian Couder wrote:\n> On Wed, Nov 20, 2024 at 9:00 AM Patrick Steinhardt <ps@pks.im> wrote:\n> \n> > Sure, let's do it that way. Will send the updated patch series in a\n> > second, thanks!\n> \n> I took a look at the v2 and found a number of small nits. Not sure\n> it's worth a reroll though, as otherwise I found it great!\n\nThanks for your review, I'll send a v3 soonish!\n\nPatrick\n"},{"id":"508022","messageId":"Z0QQnqZ5x9Y-Js8V@pks.im","threadId":"62467","inReplyTo":"afed1ddc-90fd-446a-9da0-b8e8f3fbb903@app.fastmail.com","subject":"Re: [PATCH v2 06/10] refs: skip collision checks in initial transactions","fromName":"Patrick Steinhardt","fromEmail":"pks@pks.im","sentAt":"2024-11-25T05:52:30Z","receivedAt":"2024-11-25T05:52:44Z","isPatch":true,"sender":{"key":"pks@pks.im","avatar":null},"body":"On Wed, Nov 20, 2024 at 11:42:29AM +0100, Kristoffer Haugsbakk wrote:\n> Hi\n> \n> On Wed, Nov 20, 2024, at 08:51, Patrick Steinhardt wrote:\n> > While we generally cannot avoid the first check, the second check is\n> > superfluous in cases where the transaction is an initial one in an\n> > otherwise empty ref store. The check results in multiple ref reads as\n> > well as the creation of a ref iterator for every ref we're checking,\n> > which adds up quite fast when performing the check for many refs.\n> >\n> > Introduce a new flag that allows us to skip this check and wire it up in\n> > such that the backends pass it when running an initial transaction. This\n> \n> Missing word? “wire it up in such that”.\n> \n> Maybe: “wire it up so that”.\n\nAh, indeed. Will fix. Thanks!\n\nPatrick\n"},{"id":"508023","messageId":"Z0QQoTIS4ytoGsUM@pks.im","threadId":"62467","inReplyTo":"CAP8UFD0i+ayKYwEitf7DYA-Hxo16d89LtHv2i89rHh9cPWR4LA@mail.gmail.com","subject":"Re: [PATCH v2 06/10] refs: skip collision checks in initial transactions","fromName":"Patrick Steinhardt","fromEmail":"pks@pks.im","sentAt":"2024-11-25T05:52:33Z","receivedAt":"2024-11-25T05:52:47Z","isPatch":true,"sender":{"key":"pks@pks.im","avatar":null},"body":"On Wed, Nov 20, 2024 at 11:21:44AM +0100, Christian Couder wrote:\n> On Wed, Nov 20, 2024 at 8:54 AM Patrick Steinhardt <ps@pks.im> wrote:\n> \n> > diff --git a/refs.c b/refs.c\n> > index 0f10c565bbb4e0d91210c52a3221a224e4264d28..d690eb19b3fd7083a5309deb98738547e4f48040 100644\n> > --- a/refs.c\n> > +++ b/refs.c\n> > @@ -2324,6 +2324,7 @@ int refs_verify_refname_available(struct ref_store *refs,\n> >                                   const char *refname,\n> >                                   const struct string_list *extras,\n> >                                   const struct string_list *skip,\n> > +                                 int initial_transaction,\n> \n> Nit: Using 'unsigned int' instead of 'int' might be slightly better as\n> the type would be the same as \"transaction->flags &\n> REF_TRANSACTION_FLAG_INITIAL\" we pass as an argument below. It might\n> also make it slightly simpler to convert \"int initial_transaction\" to\n> \"int flags\" if we add more flags and need to do that in the future.\n> (Other functions add an 'int initial_transaction' argument which could\n> be an 'unsigned int' instead.)\n\nMakes sense, will adapt.\n\nPatrick\n"}]}