From patchwork Thu Feb 1 07:33:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13540719 Received: from wfhigh3-smtp.messagingengine.com (wfhigh3-smtp.messagingengine.com [64.147.123.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16AB7158D81 for ; Thu, 1 Feb 2024 07:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706772805; cv=none; b=Gtsmaw3cu01Fz9yrK8eizF729UDPcQ6tncYxxgo7+8HjRhTo+BTQd6rRqF39RrGvoTEbcw2xpek8w+Ct+28Y8a8JLaIIU9i2L6D0u+O3bwy1OPGqTka7k+vtGPOUjLJy+gOFjyyEQCJuSXHLBqoZwZeoYVNdpCzZBa3SRNS7tSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706772805; c=relaxed/simple; bh=d0ivr+yGCD+kEvcVYb7HVRYZlfabGqrez78Gz0ggHMA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L/veC6BJg+hqBOUEU3VrCQQhJ+CGQL/DFzOYyUG2OlphBQoW6YB38+aUGZVdKkZ8cOmg6+qARhh383cFrm8CqFzHJgBTKYOY8no+X5b6qtgdM+59FpiUUxxY7bkWMWj0NmoJhAbEX2fbgjnUKRD6+Arv4iPYSz6Cbh0q17i05SI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=Tc/rdq9a; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=rBnuiPGB; arc=none smtp.client-ip=64.147.123.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="Tc/rdq9a"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="rBnuiPGB" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.west.internal (Postfix) with ESMTP id 12A2F1800085; Thu, 1 Feb 2024 02:33:22 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 01 Feb 2024 02:33:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1706772802; x=1706859202; bh=gau0Ngv/9d rp7CToT9JVGgDIK8w9nuUAmrETk0AwDEc=; b=Tc/rdq9aiFiOOGXcuPiGyH676e bgpZU8IvuDlKQxijjmp9sAhqE4xh2787hzu1eyIADjTsjIM5+ahvc0ICaS6ib3ST F1o+KMYkHcJgbiX56EYLTBhFOBRp8YHtE9Kbi8NP7U4k6yhjUmbX5kzzoI3JwTmL gdTZwbeSVGVJmcmfNBUcxA+uu6RcLSVFrkSaFyNHhCaxr2YNSx7QCC7aF/Kqyms1 QhrSoF5qYV1OIY6sGqT4Hzzs+Fno+HpEJjrJQqvyXRGmvjAz+/vFO/4gRLms8tCK U0iQKV1E167uRTX9RyZLVKsZM0m7S6TGkBUa/GffeTeOrONkdwa7yIpBjNPg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1706772802; x=1706859202; bh=gau0Ngv/9drp7CToT9JVGgDIK8w9 nuUAmrETk0AwDEc=; b=rBnuiPGB0ALAzEe5UvmcL21dqxum2+MFg6hZBt4AXNX1 MN/MS3V0v79H5fIz/vUSrFS07f/lkIiwKNrx2YMar12JH/DbdULGKFjMEG1NQpFE DW3lYmPitAgcdJ2MxMvlE9x+xwFFMD8rKfqETHFczrzHbz5P/4g4vaOrd1wYgbkB F39B28lJE+XPRAAGlT507WOOD7Q2ng/ROJlNaj/8jvwEcnpG0zGuSkGCp0DlI5eD D5Qp7C0euzSe2IzeQoAaGAi+1PkquTbVf1JJlrjSFwCgos4U3kdygx+WJrQFpjlM DCCKSMIe6WyEueUEQXz5l6dlAF/Dw54qTUNhFW+OaA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrfedutddguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrght rhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtth gvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleff teenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpsh esphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 1 Feb 2024 02:33:21 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id e9b1e6f4 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 1 Feb 2024 07:29:58 +0000 (UTC) Date: Thu, 1 Feb 2024 08:33:19 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Eric Sunshine , Junio C Hamano Subject: [PATCH v2 7/9] reftable/merged: refactor seeking of records Message-ID: <80cf2fd272577b13f4fd23f009d22294629f5f3b.1706772591.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The code to seek reftable records in the merged table code is quite hard to read and does not conform to our coding style in multiple ways: - We have multiple exit paths where we release resources even though that is not really necessary. - We use a scoped error variable `e` which is hard to reason about. This variable is not required at all. - We allocate memory in the variable declarations, which is easy to miss. Refactor the function so that it becomes more maintainable in the future. Signed-off-by: Patrick Steinhardt --- reftable/merged.c | 54 ++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/reftable/merged.c b/reftable/merged.c index e2c6253324..0abcda26e8 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -238,50 +238,38 @@ static int merged_table_seek_record(struct reftable_merged_table *mt, struct reftable_iterator *it, struct reftable_record *rec) { - struct reftable_iterator *iters = reftable_calloc( - mt->stack_len, sizeof(*iters)); struct merged_iter merged = { - .stack = iters, .typ = reftable_record_type(rec), .hash_id = mt->hash_id, .suppress_deletions = mt->suppress_deletions, .key = STRBUF_INIT, .entry_key = STRBUF_INIT, }; - int n = 0; - int err = 0; - int i = 0; - for (i = 0; i < mt->stack_len && err == 0; i++) { - int e = reftable_table_seek_record(&mt->stack[i], &iters[n], - rec); - if (e < 0) { - err = e; - } - if (e == 0) { - n++; - } - } - if (err < 0) { - int i = 0; - for (i = 0; i < n; i++) { - reftable_iterator_destroy(&iters[i]); - } - reftable_free(iters); - return err; + struct merged_iter *p; + int err; + + REFTABLE_CALLOC_ARRAY(merged.stack, mt->stack_len); + for (size_t i = 0; i < mt->stack_len; i++) { + err = reftable_table_seek_record(&mt->stack[i], + &merged.stack[merged.stack_len], rec); + if (err < 0) + goto out; + if (!err) + merged.stack_len++; } - merged.stack_len = n; err = merged_iter_init(&merged); - if (err < 0) { + if (err < 0) + goto out; + + p = reftable_malloc(sizeof(struct merged_iter)); + *p = merged; + iterator_from_merged_iter(it, p); + +out: + if (err < 0) merged_iter_close(&merged); - return err; - } else { - struct merged_iter *p = - reftable_malloc(sizeof(struct merged_iter)); - *p = merged; - iterator_from_merged_iter(it, p); - } - return 0; + return err; } int reftable_merged_table_seek_ref(struct reftable_merged_table *mt,