From patchwork Tue May 7 04:53:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13656225 Received: from fhigh1-smtp.messagingengine.com (fhigh1-smtp.messagingengine.com [103.168.172.152]) (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 B2EEB6BB2F for ; Tue, 7 May 2024 04:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715057588; cv=none; b=GphzAxzWBqW8V0WLU4vro0HNjP1GEMGOEvPgwFXLHUahFkTKGpdv2+E8IiWfJUDx9ZZ3j3eqxhSQd3Z+vVMplVxZ28j40564Vc5tS7XM67DWg1/9O/dsgTrl8Ot2wph5EtEEn3DVmW9/snPvIxkR09bI2ZXIS8zKDbnZNYGnWj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715057588; c=relaxed/simple; bh=QIxnVT7TP1Z7LKljBJOvXZM0iuHhAxLnkCjJBdWOThQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QjcjMcGVPDPetME1Q0BSJQ2GdjOs3qa/kc4kG9cLwjBXfl3o8d1OuQ7EOLoNNWr0pjRIscQCGFgmP7M0wKaMfjQ42gjrdI3A2Q080iEtO9JEGinTegwMBZhi7Ijsw3ran7vTb1m0zK8Qg59NtaBL8qMhoJULdBsz2hLkTmG3lJ0= 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=jKPwLF5U; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=GqCiez30; arc=none smtp.client-ip=103.168.172.152 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="jKPwLF5U"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="GqCiez30" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 4F8421140166; Tue, 7 May 2024 00:53:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 07 May 2024 00:53:04 -0400 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=1715057584; x=1715143984; bh=epubhXTdMf A7w1nIBQEwMEwgpug5cUbu5JXxBDQ3XNc=; b=jKPwLF5UitXmoN5qsIhE5/amC5 18UOqYX++/bW9KPGQWLLoBkqRkQCKWASAQ3LbkTXE0iu3aurWlXXa96QkCZshSri yf1ujVuKQdEtG6HtMx985Jq16W53HvAvzL1Kb2pvutjWur9cJHKd5ys+nZOy4wpv fTmYy6MhvyLrFx3RhX9vu4RttV+DSRq/dBgFyxh2Vr9mvd9ssvNZJu91BmBrvYhS k5XEVHzCU+P9NRYHxUq5qtKsTKjy6Ut4paOKVPRxsvmShFnIj4gW5BJ04t1wAN1+ pn6P+9x9wAzX3yAUkZoHuW+LGDBHK3xubXWNFjjtKfpbzQE4WyJ03V8QxfUg== 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=1715057584; x=1715143984; bh=epubhXTdMfA7w1nIBQEwMEwgpug5 cUbu5JXxBDQ3XNc=; b=GqCiez30lOkLQD0e+t/ytkleWRHQ14Z0tZ1K5AV2DTbe gUObDRqBx1x74YEVeGtLg03YxPmDuSinrOydGaUWM1Al87cHbn8dvZRcPo1H6ags UFssuE2oF0mP2xE4IKyEGrX9lAlQ2Df+matwmW33iEzjRsZHeFNKot4J8Abpkfsf mZMgaGoPl6ZgYLeau7LTiFRJeZdiH5+KqlwlOk14uv6NNlRlgU+mTRdh4TX0aNsp 30X9APOSgm51fLkbPOTfrHxinJYWVAHobvmC83r3TY0ksiOCcPVJShzGk2REB5S3 sjh0ovgQm6wizkXNvflj6ek4P3Mh4qg1bsFFdIT1sg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvddvjedgkeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 7 May 2024 00:53:03 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id de05801a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 7 May 2024 04:52:56 +0000 (UTC) Date: Tue, 7 May 2024 06:53:01 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v4 04/13] attr: don't recompute default attribute source Message-ID: <1204a342161235c38273981874add59031cb554e.1715057362.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 `default_attr_source()` function lazily computes the attr source supposedly once, only. This is done via a static variable `attr_source` that contains the resolved object ID of the attr source's tree. If the variable is the null object ID then we try to look up the attr source, otherwise we skip over it. This approach is flawed though: the variable will never be set to anything else but the null object ID in case there is no attr source. Consequently, we re-compute the information on every call. And in the worst case, when we silently ignore bad trees, this will cause us to try and look up the treeish every single time. Improve this by introducing a separate variable `has_attr_source` to track whether we already computed the attr source and, if so, whether we have an attr source or not. This also allows us to convert the `ignore_bad_attr_tree` to not be static anymore as the code will only be executed once anyway. Signed-off-by: Patrick Steinhardt --- attr.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/attr.c b/attr.c index 6af7151088..a5b717e4ce 100644 --- a/attr.c +++ b/attr.c @@ -1205,41 +1205,48 @@ static void collect_some_attrs(struct index_state *istate, fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem); } static const char *default_attr_source_tree_object_name; -static int ignore_bad_attr_tree; void set_git_attr_source(const char *tree_object_name) { default_attr_source_tree_object_name = xstrdup(tree_object_name); } -static void compute_default_attr_source(struct object_id *attr_source) +static int compute_default_attr_source(struct object_id *attr_source) { + int ignore_bad_attr_tree = 0; + if (!default_attr_source_tree_object_name) default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); if (!default_attr_source_tree_object_name && git_attr_tree) { default_attr_source_tree_object_name = git_attr_tree; ignore_bad_attr_tree = 1; } - if (!default_attr_source_tree_object_name || !is_null_oid(attr_source)) - return; + if (!default_attr_source_tree_object_name) + return 0; if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, - attr_source) && !ignore_bad_attr_tree) - die(_("bad --attr-source or GIT_ATTR_SOURCE")); + attr_source)) { + if (!ignore_bad_attr_tree) + die(_("bad --attr-source or GIT_ATTR_SOURCE")); + return 0; + } + + return 1; } static struct object_id *default_attr_source(void) { static struct object_id attr_source; + static int has_attr_source = -1; - if (is_null_oid(&attr_source)) - compute_default_attr_source(&attr_source); - if (is_null_oid(&attr_source)) + if (has_attr_source < 0) + has_attr_source = compute_default_attr_source(&attr_source); + if (!has_attr_source) return NULL; return &attr_source; }