From nobody Tue Sep 27 18:33:12 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4McStK1Lk8z4YPMH; Tue, 27 Sep 2022 18:33:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4McStK0l29z43Vx; Tue, 27 Sep 2022 18:33:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664303593; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=B0/XwlUHw2x2MHVLrkjZ9ufEZWTUKVv3F8IJycZf3Jo=; b=XXYy/e2W0h8QvHhveGas955h/aSg0ZExPrpkRPP6643z/wkfbAfyDHiRSBNhfNNtckIppk B5bdI2whCrMcBOjwlzuoyb5kaPYvPOGQ04FBADGoktOLUECjGPpKmEVehDDiO2uRtPhEBL stbNTFiCJbaSXAmtId6oLG9V7aHfAf/fcQEu2/KSKv4EoM2yBUavN2aZgGxi0Oz1VhxOTj bzgA/gRWIayYnMjq+U8uXYjTYKT8nsT/Ym27NG4J+GcqJCwl5SSjKDylaMojsKFWB7CG3U ZM9yDGIyXQcO9sz45rMcczWeJV3dr/oq2iLQTx/UzPX/oVDpGZjwa9obCzivGQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4McStJ6tVNz1CXG; Tue, 27 Sep 2022 18:33:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 28RIXCIM062827; Tue, 27 Sep 2022 18:33:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28RIXC77062826; Tue, 27 Sep 2022 18:33:12 GMT (envelope-from git) Date: Tue, 27 Sep 2022 18:33:12 GMT Message-Id: <202209271833.28RIXC77062826@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Doug Moore Subject: git: e96ae5cb05b3 - main - sysctl_search_oid: remove useless tests List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dougm X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e96ae5cb05b3138e106b42649aca2fed71c7fc78 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664303593; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=B0/XwlUHw2x2MHVLrkjZ9ufEZWTUKVv3F8IJycZf3Jo=; b=yeU7FyH+bncdTdJ/0R48JjvGMY2+5ST26mYjv/wyTGST6ZDYkKqdSTVgz8sAIUOB4RUiMf AOMJQWTY/0bmI0qklznGTyQpolwB4U4RpRXJ8lf0Du7/Ht0ZgPQjH4vp9vYv4u47hwufCA q3fSSWUpc8GbLVKNKnR+IfNDD/mubc9uaWiK9hxxYjL6lz9N1EDqoeQeOu8E6jK0qo23Yp lGn8EYUc0QV/r2I53Bl8QsL2vOpCUXzRvnHiYnMnybx9mglIC8uhCDfVARLcoOmYr93ewP osYRyyjZC62gcp6mzmQsv47HHFrcGrcV00fzfXyVIsorNQlTc8zH0/S5iFyGPw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664303593; a=rsa-sha256; cv=none; b=xGInYQFQJ7R0HsIvoKJSzdDk8z+XXTnJtdD8gc6lSv1knyLFcPFGUD2Wx538z9bYV9XY1R SodLWPfDnCS3XWU0W02m1zPFf+wrEVFbW4eYx6L8nzg0RN1+97yuqwN/fbD3uCeFQTIAjs UhZK5HvTZHz+Bc7o/RsiLqFEa2yJUmGJJ/rjD2OKAxFjdJ8s1yi1m2WvWpsXbfhuy3HzIC U94yILAZDi/krFjfE04bUTbq6WVr3UHeaVmFYNnTZvG7JcTQRk9IUY9cYKBWsdL4IrLMXv 1/+XAV3PcbsX0kFbYWi41bBdPj438knaJdRCmXLQyAmo8uPM2mUE2Samfk/2Ow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=e96ae5cb05b3138e106b42649aca2fed71c7fc78 commit e96ae5cb05b3138e106b42649aca2fed71c7fc78 Author: Doug Moore AuthorDate: 2022-09-27 18:30:31 +0000 Commit: Doug Moore CommitDate: 2022-09-27 18:30:31 +0000 sysctl_search_oid: remove useless tests sysctl_search_old makes several tests in a loop that can be removed. The first test in the loop is only ever true on the first loop iteration, and is always true on that iteration, so its work can be done before the loop begins. The upper and lower bounds on the loop variable 'indx' are each tested on each iteration, but 'indx' is changed in one direction or the other only once within the loop, so only one bound needs to be checked. Two ways remain in the loop that nodes[indx] can change (after one of them is put before the loop start), and one of them applies exactly when indx has been incremented, so no separate test for that case requires testing. Restructure and add comments that makes clearer that this is a basic depth-first search. Reviewed by: hselasky Differential Revision: https://reviews.freebsd.org/D36741 --- sys/kern/kern_sysctl.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index a31c8f97daa1..a655e9e50677 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -356,29 +356,34 @@ sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle) SYSCTL_ASSERT_LOCKED(); indx = 0; - while (indx < CTL_MAXNAME && indx >= 0) { - if (nodes[indx] == NULL && indx == 0) - nodes[indx] = RB_MIN(sysctl_oid_list, - &sysctl__children); - else if (nodes[indx] == NULL) - nodes[indx] = RB_MIN(sysctl_oid_list, - &nodes[indx - 1]->oid_children); - else - nodes[indx] = RB_NEXT(sysctl_oid_list, - &nodes[indx - 1]->oid_children, nodes[indx]); - + /* + * Do a depth-first search of the oid tree, looking for 'needle'. Start + * with the first child of the root. + */ + nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children); + for (;;) { if (nodes[indx] == needle) return (indx + 1); if (nodes[indx] == NULL) { - indx--; - continue; - } - - if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) { - indx++; + /* Node has no more siblings, so back up to parent. */ + if (indx-- == 0) { + /* Retreat to root, so give up. */ + break; + } + } else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) { + /* Node has children. */ + if (++indx == CTL_MAXNAME) { + /* Max search depth reached, so give up. */ + break; + } + /* Start with the first child. */ + nodes[indx] = RB_MIN(sysctl_oid_list, + &nodes[indx - 1]->oid_children); continue; } + /* Consider next sibling. */ + nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]); } return (-1); }