From patchwork Wed Aug 25 13:49:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 12457569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BA4BC4320A for ; Wed, 25 Aug 2021 13:50:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4FC95610D2 for ; Wed, 25 Aug 2021 13:50:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237039AbhHYNus (ORCPT ); Wed, 25 Aug 2021 09:50:48 -0400 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:47943 "EHLO wout5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233490AbhHYNur (ORCPT ); Wed, 25 Aug 2021 09:50:47 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 884513200A11 for ; Wed, 25 Aug 2021 09:50:01 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 25 Aug 2021 09:50:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:subject:message-id:mime-version:content-type; s=fm1; bh=j30bBLoSbHF9o+qm//Dklb+2jQcFzP+3fYS8JGrpkqE=; b=s1YUx31vdrVb Y3IWPM8Y4khwsqyatCcYyRzMIzjSbjjMIVvVtV05hw4dWAP+f4vJcB5ZsoloqUoA 2De/9+t6/VNTQhFhb7LOGf68mQE927oUQNWSK7C6SbwFfXNj9r1O/R5fwPLR4XO5 eJjhP5m3jRIGW6p+jzorXjz1FzFMxWM3YkQhIlzHtVEFTXL4WbFT0F2TD7VJdQBd MzNkh5eVw0ioGmxgOVm0m3vkam7pflnb5hLDCoiGN7HWOvNFDL6GKC/cPRS67/L/ 6hS6/ihbGjREChrFHB4p8gS3zxqwKO3+uKpC8FW/+D+df7VeJW1BUICDnHfEO8zK 1kx7JQ7WbQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=j30bBLoSbHF9o+qm//Dklb+2jQcFz P+3fYS8JGrpkqE=; b=EjVOLRUenbPSKKGd8wp+9WDSEF3WeaHjjA6wcSxcKMAUT lvvysTorXGG3PzIZkX+X1tFcbMKEtnZMjEePx6Kbq3yYg/L5QzO6uUjKBksH+sTL A3W61CYvFjeZNav0FxJ6u5RC6FOe7rCwR1kWqOXyCmE59c+awFhoC2AkzGCjAkco x5ClhFCm7HUksXXRvd2ecgriMx2benmoVgOFOfa+jYSBNgRPDyfe95lx6Ns+FQvC HHLNPukCXt7FDncgrI7i9oc3zgj8UHSgeWIwRiRkPGa32DKYdJDvfe8ui4PTYinS 1gX9KiTVEMbCcuEczq/qe7G+KZA7wtdE89bw/79FA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddruddtledgjedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkgggtugesghdtreertd dtjeenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhs rdhimheqnecuggftrfgrthhtvghrnhepjedtfefftedugfevvdejtdehjefgtddvvdethe eiteduuedtteeileetkefhfeeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghm pehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 25 Aug 2021 09:49:59 -0400 (EDT) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id b841d9be (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Wed, 25 Aug 2021 13:49:52 +0000 (UTC) Date: Wed, 25 Aug 2021 15:49:51 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH] ls-refs: reuse buffer when sending refs Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In the initial reference advertisement, the Git server will first announce all of its references to the client. The logic is handled in `send_ref()`, which will allocate a new buffer for each refline it is about to send. This is quite wasteful: instead of allocating a new buffer each time, we can just reuse a buffer. Improve this by passing in a buffer via the `ls_refs_data` struct which is then reused on each reference. In a repository with about 2.3M refs, this speeds up local mirror fetches by about 2%: Benchmark #1: HEAD~: git-fetch Time (mean ± σ): 25.415 s ± 0.131 s [User: 22.722 s, System: 4.740 s] Range (min … max): 25.240 s … 25.543 s 5 runs Benchmark #2: HEAD: git-fetch Time (mean ± σ): 24.922 s ± 0.110 s [User: 22.404 s, System: 4.476 s] Range (min … max): 24.825 s … 25.081 s 5 runs Summary 'HEAD: git-fetch' ran 1.02 ± 0.01 times faster than 'HEAD~: git-fetch' Signed-off-by: Patrick Steinhardt --- Note that while this topic applies on top of "master", I've done the benchmark on top of my other optimizations for fetches. It's cheating a bit, but it's easier to see that the optimization does something when the remaining constant part is lower. ls-refs.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ls-refs.c b/ls-refs.c index 88f6c3f60d..84021416ca 100644 --- a/ls-refs.c +++ b/ls-refs.c @@ -65,6 +65,7 @@ struct ls_refs_data { unsigned peel; unsigned symrefs; struct strvec prefixes; + struct strbuf buf; unsigned unborn : 1; }; @@ -73,7 +74,8 @@ static int send_ref(const char *refname, const struct object_id *oid, { struct ls_refs_data *data = cb_data; const char *refname_nons = strip_namespace(refname); - struct strbuf refline = STRBUF_INIT; + + strbuf_reset(&data->buf); if (ref_is_hidden(refname_nons, refname)) return 0; @@ -82,9 +84,9 @@ static int send_ref(const char *refname, const struct object_id *oid, return 0; if (oid) - strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons); + strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons); else - strbuf_addf(&refline, "unborn %s", refname_nons); + strbuf_addf(&data->buf, "unborn %s", refname_nons); if (data->symrefs && flag & REF_ISSYMREF) { struct object_id unused; const char *symref_target = resolve_ref_unsafe(refname, 0, @@ -94,20 +96,19 @@ static int send_ref(const char *refname, const struct object_id *oid, if (!symref_target) die("'%s' is a symref but it is not?", refname); - strbuf_addf(&refline, " symref-target:%s", + strbuf_addf(&data->buf, " symref-target:%s", strip_namespace(symref_target)); } if (data->peel && oid) { struct object_id peeled; if (!peel_iterated_oid(oid, &peeled)) - strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled)); + strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled)); } - strbuf_addch(&refline, '\n'); - packet_write(1, refline.buf, refline.len); + strbuf_addch(&data->buf, '\n'); + packet_write(1, data->buf.buf, data->buf.len); - strbuf_release(&refline); return 0; } @@ -145,6 +146,7 @@ int ls_refs(struct repository *r, struct strvec *keys, memset(&data, 0, sizeof(data)); strvec_init(&data.prefixes); + strbuf_init(&data.buf, 0); ensure_config_read(); git_config(ls_refs_config, NULL); @@ -173,6 +175,7 @@ int ls_refs(struct repository *r, struct strvec *keys, send_ref, &data, 0); packet_flush(1); strvec_clear(&data.prefixes); + strbuf_release(&data.buf); return 0; }