From patchwork Tue Feb 6 06:35:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13546713 Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) (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 CEBA6127B73 for ; Tue, 6 Feb 2024 06:35:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.20 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201356; cv=none; b=Zo8cEJc+avU3z2WoFej4M0iH3vfEH4Oh/bQe1PCIS6e/GMfwycg9/FwF31gU+5y9h0MR4aZQpp5IuHHq1QttupKC8DPZSDGAJ9fliKL/44hmMfMOn/Kmq1lsb9AdSkmW+lobspf2kYKtn7av02AvP8kOYu92qACnnkWgPgUsgMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201356; c=relaxed/simple; bh=tGjYmNrNoUe2tYCGSGhJ0erhiFTJnxpRcIele1GpA7Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WJ3Pa9vItuCn80fQqP5GMtc8oXpHNYyDn0D594M9Qoot8nssNBYNf9uG0EvdcR7mICRdG/EmXgKxWa33q76OwrcUBmbOQHGiai9/Cbo0D623XLRdfMMCFJoVgyB11lk3imBp6rmlHPQhbFdyqo9qEZLbpYQ0ZGHAugm0qpzvRiA= 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=ncxia9PG; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=muGGXmu8; arc=none smtp.client-ip=64.147.123.20 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="ncxia9PG"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="muGGXmu8" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id B03CD3200ABE; Tue, 6 Feb 2024 01:35:53 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 06 Feb 2024 01:35:54 -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=1707201352; x=1707287752; bh=Gr6RobcUCE QqjFTVBtMmWdvZpJqbHPgQ4qR9RrTAPAc=; b=ncxia9PGvymYVAXGincHPlegGb rhS5KVliMNPGm2Uqan0vEKnt2bZRakM5EgZF3nOAzxtZW3o5PFuz7jGFZ2G9RGJU qxZkJbg3X9xsGCcsrG3zXGYFEZI3qkz6UDOPpQNhjuIyKS0JOlfhonblvXZzBVqU 7OdS5jKeqnqxtmSh1qPoyzgDtAl4PemmZ932fS02On+OwGNJSXU2racEq5fu1nUm lxVLLmLFVJeOTbQ68YHNe+WY1ujNENLSUigjBVYtHkzSLSMyV0F8oTyzPo4n8R+G 8KDG6HdLhWLYn0JpAemllsoHXkjofUEtBsfNqLtSCOQA0QURA7pt+/Z5YX8A== 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=1707201352; x=1707287752; bh=Gr6RobcUCEQqjFTVBtMmWdvZpJqb HPgQ4qR9RrTAPAc=; b=muGGXmu8bFMO5+kIg6pc8grmcHiwqIXVbRUGKpdID5aD 6TiwAUW9bsig7+NHqDOGWNteKnEJISoNl8GXx+lFfeWewx9bv78eURl4epEmhr2h iVT2efPUpd7E3Fe7veLqyCH7f3of1LVOwXpK+62ApqOOYLvU/JIZNcuGDBYRTfWR qs8BpFdPXL3r6j9glcEI2fzib9rYhcBBvLuh4U5/hDmvYWDw+FmB5AyNFH9Z/8S1 kPFSVV7zb+dpGzEKOv1G1L3duBUlUT+G1d5mFdMvDSP6p9qaHHEQzdjeAQ5GHWrf nnnuCzokmKyOEY/UrVGjXm43eV+MBuyNy3slYnpERw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrfedvvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrght rhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtth gvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleff teenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpehpsh esphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 6 Feb 2024 01:35:51 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 7188ed17 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 6 Feb 2024 06:32:22 +0000 (UTC) Date: Tue, 6 Feb 2024 07:35:50 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Eric Sunshine , Junio C Hamano , Toon Claes , Karthik Nayak Subject: [PATCH v3 7/9] reftable/merged: refactor seeking of records Message-ID: <4605ad724746db8c33710e54369105214481dbfb.1707200355.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,