git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: karthik nayak <karthik.188@gmail.com>,
	 Junio C Hamano <gitster@pobox.com>,
	 Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>,
	 Christian Couder <chriscool@tuxfamily.org>
Subject: [PATCH v3 10/10] reftable/block: optimize allocations by using scratch buffer
Date: Mon, 25 Nov 2024 07:27:15 +0100	[thread overview]
Message-ID: <20241125-pks-refs-optimize-migrations-v3-10-17bc85e33ad7@pks.im> (raw)
In-Reply-To: <20241125-pks-refs-optimize-migrations-v3-0-17bc85e33ad7@pks.im>

The block writer needs to compute the key for every record that one adds
to the writer. The buffer for this key is stored on the stack and thus
reallocated on every call to `block_writer_add()`, which is inefficient.

Refactor the code so that we store the buffer in the `block_writer`
struct itself so that we can reuse it. This reduces the number of
allocations when writing many refs, e.g. when migrating one million refs
from the "files" backend to the "reftable backend. Before this change:

    HEAP SUMMARY:
        in use at exit: 80,048 bytes in 49 blocks
      total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated

After this change:

    HEAP SUMMARY:
        in use at exit: 80,048 bytes in 49 blocks
      total heap usage: 2,013,250 allocs, 2,013,201 frees, 347,543,583 bytes allocated

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/block.c | 13 +++++--------
 reftable/block.h |  2 ++
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/reftable/block.c b/reftable/block.c
index 3fa36c002a0c1852790780e74a6e055161f857d9..01980784854cc454938bd2278b94047ff62c20d4 100644
--- a/reftable/block.c
+++ b/reftable/block.c
@@ -110,24 +110,21 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
 		.buf = w->block + w->next,
 		.len = w->block_size - w->next,
 	};
-
 	struct string_view start = out;
-
 	int is_restart = 0;
-	struct reftable_buf key = REFTABLE_BUF_INIT;
 	int n = 0;
 	int err;
 
-	err = reftable_record_key(rec, &key);
+	err = reftable_record_key(rec, &w->scratch);
 	if (err < 0)
 		goto done;
 
-	if (!key.len) {
+	if (!w->scratch.len) {
 		err = REFTABLE_API_ERROR;
 		goto done;
 	}
 
-	n = reftable_encode_key(&is_restart, out, last, key,
+	n = reftable_encode_key(&is_restart, out, last, w->scratch,
 				reftable_record_val_type(rec));
 	if (n < 0) {
 		err = -1;
@@ -143,9 +140,8 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
 	string_view_consume(&out, n);
 
 	err = block_writer_register_restart(w, start.len - out.len, is_restart,
-					    &key);
+					    &w->scratch);
 done:
-	reftable_buf_release(&key);
 	return err;
 }
 
@@ -569,6 +565,7 @@ void block_writer_release(struct block_writer *bw)
 	REFTABLE_FREE_AND_NULL(bw->zstream);
 	REFTABLE_FREE_AND_NULL(bw->restarts);
 	REFTABLE_FREE_AND_NULL(bw->compressed);
+	reftable_buf_release(&bw->scratch);
 	reftable_buf_release(&bw->last_key);
 	/* the block is not owned. */
 }
diff --git a/reftable/block.h b/reftable/block.h
index b3f837d612a8f0fbe98430b04e2dddaa975a15ab..0431e8591f41dedfb96eef304ea63ef2e9e5f5dd 100644
--- a/reftable/block.h
+++ b/reftable/block.h
@@ -39,6 +39,8 @@ struct block_writer {
 	uint32_t restart_cap;
 
 	struct reftable_buf last_key;
+	/* Scratch buffer used to avoid allocations. */
+	struct reftable_buf scratch;
 	int entries;
 };
 

-- 
2.47.0.274.g962d0b743d.dirty


  parent reply	other threads:[~2024-11-25  6:27 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-08  9:34 [PATCH 00/10] refs: optimize ref format migrations Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 01/10] refs: allow passing flags when setting up a transaction Patrick Steinhardt
2024-11-11 10:30   ` karthik nayak
2024-11-11 12:53     ` Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 02/10] refs/files: move logic to commit initial transaction Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 03/10] refs: introduce "initial" transaction flag Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 04/10] refs/files: support symbolic and root refs in initial transaction Patrick Steinhardt
2024-11-11 10:42   ` karthik nayak
2024-11-11 12:53     ` Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 05/10] refs: use "initial" transaction semantics to migrate refs Patrick Steinhardt
2024-11-11 10:43   ` karthik nayak
2024-11-08  9:34 ` [PATCH 06/10] refs: skip collision checks in initial transactions Patrick Steinhardt
2024-11-11 10:53   ` karthik nayak
2024-11-08  9:34 ` [PATCH 07/10] refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG` Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 08/10] reftable/writer: optimize allocations by using a scratch buffer Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 09/10] reftable/block: rename `block_writer::buf` variable Patrick Steinhardt
2024-11-08  9:34 ` [PATCH 10/10] reftable/block: optimize allocations by using scratch buffer Patrick Steinhardt
2024-11-11 10:57 ` [PATCH 00/10] refs: optimize ref format migrations karthik nayak
2024-11-11 12:53   ` Patrick Steinhardt
2024-11-20  7:04     ` Junio C Hamano
2024-11-20  7:50       ` Patrick Steinhardt
2024-11-20 10:25         ` Christian Couder
2024-11-25  5:52           ` Patrick Steinhardt
2024-11-20  7:51 ` [PATCH v2 " Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 01/10] refs: allow passing flags when setting up a transaction Patrick Steinhardt
2024-11-20 10:19     ` Christian Couder
2024-11-20  7:51   ` [PATCH v2 02/10] refs/files: move logic to commit initial transaction Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 03/10] refs: introduce "initial" transaction flag Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 04/10] refs/files: support symbolic and root refs in initial transaction Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 05/10] refs: use "initial" transaction semantics to migrate refs Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 06/10] refs: skip collision checks in initial transactions Patrick Steinhardt
2024-11-20 10:21     ` Christian Couder
2024-11-25  5:52       ` Patrick Steinhardt
2024-11-20 10:42     ` Kristoffer Haugsbakk
2024-11-25  5:52       ` Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 07/10] refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG` Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 08/10] reftable/writer: optimize allocations by using a scratch buffer Patrick Steinhardt
2024-11-20 10:21     ` Christian Couder
2024-11-25  5:52       ` Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 09/10] reftable/block: rename `block_writer::buf` variable Patrick Steinhardt
2024-11-20  7:51   ` [PATCH v2 10/10] reftable/block: optimize allocations by using scratch buffer Patrick Steinhardt
2024-11-20 10:22     ` Christian Couder
2024-11-25  6:27 ` [PATCH v3 00/10] refs: optimize ref format migrations Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 01/10] refs: allow passing flags when setting up a transaction Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 02/10] refs/files: move logic to commit initial transaction Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 03/10] refs: introduce "initial" transaction flag Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 04/10] refs/files: support symbolic and root refs in initial transaction Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 05/10] refs: use "initial" transaction semantics to migrate refs Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 06/10] refs: skip collision checks in initial transactions Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 07/10] refs: don't normalize log messages with `REF_SKIP_CREATE_REFLOG` Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 08/10] reftable/writer: optimize allocations by using a scratch buffer Patrick Steinhardt
2024-11-25  6:27   ` [PATCH v3 09/10] reftable/block: rename `block_writer::buf` variable Patrick Steinhardt
2024-11-25  6:27   ` Patrick Steinhardt [this message]
2024-11-25  6:29   ` [PATCH v3 00/10] refs: optimize ref format migrations Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241125-pks-refs-optimize-migrations-v3-10-17bc85e33ad7@pks.im \
    --to=ps@pks.im \
    --cc=chriscool@tuxfamily.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=karthik.188@gmail.com \
    --cc=kristofferhaugsbakk@fastmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).