From patchwork Mon Mar 24 20:20:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 109121 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D2EFA3858280 for ; Mon, 24 Mar 2025 20:21:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D2EFA3858280 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=default header.b=gwTUTHcx X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 639073858D33 for ; Mon, 24 Mar 2025 20:20:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 639073858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 639073858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742847642; cv=none; b=URaCZ3f/TIa1KccKq542Nc9/AzxnCVg4dnBzWsZGMj3xsI3DZ6RxjFsMw3oxJCTJ/z0Ro+5jwySQ2WPG1mRA34bc25T8LCU3XH7bG+545d5bspJg6LhntUFASwKFIJe3QVK56qcVBssAnD+gE7p9jaCm/pujy1Nvp/yC6p2Djhk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742847642; c=relaxed/simple; bh=IhnZLK6QBV4g+Uf0z+W4/TKGOvpBMyy5hfrvInnGwCc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SLoxFOVFFgbJ1k2WPTmsgi6AWzqYcsOJMcLbjegCyGHc6Z1esGgjbRrifPcsVNt4Pr2FNMHOsgPCmsI+FfH+W9Zaisi6W2pO/LiRqSfNZUBP/IuVlRZztv1vFFoUP2/8C/BpldNppCSsBRhEEXHDQwRs535kmhRiu53YbXtLkDk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 639073858D33 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 52OKKbet150831 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 24 Mar 2025 16:20:41 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 52OKKbet150831 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=default; t=1742847642; bh=i4eZtw0Pu81sVTA9mYnofBXEFXJmVH20i+KBmgYohwQ=; h=From:To:Cc:Subject:Date:From; b=gwTUTHcxh+wJ5znbA7n4kH15KEuwh3FgXtwBLngxGkVXkCzZbYSuCWeR5KNzlRY6P ljAruP1/q+/rdJ5WHG68rC4A45btrfYKH7oBUyZveGyNkQoi5G1GvSrVzhcEgoBkuL fbsXOBZ3U+pQmIg6bFbWtdygh0ndHhLLtResSuDo= Received: by simark.ca (Postfix, from userid 112) id 2B6E11E114; Mon, 24 Mar 2025 16:20:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Spam-Level: X-Spam-Status: No, score=-3187.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 Received: from simark.localdomain (modemcable238.237-201-24.mc.videotron.ca [24.201.237.238]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 074FF1E0C0; Mon, 24 Mar 2025 16:20:35 -0400 (EDT) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/2] gdb/dwarf: remove unnecessary comparison in cooked_index_entry::compare Date: Mon, 24 Mar 2025 16:20:27 -0400 Message-ID: <20250324202035.3727263-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Mon, 24 Mar 2025 20:20:37 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org From: Simon Marchi I believe that the `(mode == MATCH && a == munge ('<'))` part of the condition is unnecesary. Or perhaps I don't understand the algorithm. The use of "munge" above effectively makes it so that the template portion of names is completely ignored for the sake of the comparison. Then, in the condition, this: a == munge ('<') is functionally equivalent to a == '\0' If `a` is indeed '\0', and `b` is also '\0', then we would have taken the earlier branch: if (a == b) return 0; If `b` is not '\0', then we won't take this branch and we'll go into the final comparison: return a < b ? -1 : 1; So, as far as I can see, there is no case where `mode == MATCH`, where we're going to use this special `return 0`. Regression tested using the various DWARF target boards on Debian 12. Change-Id: I5ea0463c1fdbbc1b003de2f0a423fd0073cc9dec --- gdb/dwarf2/cooked-index.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) base-commit: 49d5323352a910dd99cdd635c4923610cce36d10 diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index 724615f497e3..1a2e19debab5 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -122,11 +122,8 @@ cooked_index_entry::compare (const char *stra, const char *strb, /* When completing, if STRB ends earlier than STRA, consider them as equal. */ - if (mode == COMPLETE || (mode == MATCH && a == munge ('<'))) - { - if (b == '\0') - return 0; - } + if (mode == COMPLETE && b == '\0') + return 0; return a < b ? -1 : 1; } From patchwork Mon Mar 24 20:20:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 109122 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D16AC3858019 for ; Mon, 24 Mar 2025 20:22:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D16AC3858019 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=default header.b=Vj3rr46h X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 944D03858D3C for ; Mon, 24 Mar 2025 20:20:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 944D03858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 944D03858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742847642; cv=none; b=hErcvvuxf3gDGD7wheFsgys6cBGlBxC6wcFCOSHba+ZT/KNha4/3Ahdj5D2z9582tvZr9bRyFaWglEjk0MoL6BhecFHk6jxTbCPpEYsG2VKHz+8edunCbdkSRMA4q/3m1EBeGy9WTsy2i0D2nZolaevAYIIhW3CIUu/SAiz2wUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742847642; c=relaxed/simple; bh=xveUBEPGOXXCa51dtjMmzYDooTQR5ik6tVubMF0WLu4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bMhIv5TcePMAMbIKbCbHLTSBLKCOo3LLESAC1UPRp+XaRnBxACd8dk+hxcT9Cboid6p5u4M2tYXogZniio6IZvU96duvI9xptg40ZK0wul0bGUbRnc6AGdlJ9O7W3+3taaKZuFnUW1V+CYoYosgDsjyXfafMVGp+Hm47n8Cm2/s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 944D03858D3C Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 52OKKbnP150838 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 24 Mar 2025 16:20:42 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 52OKKbnP150838 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=default; t=1742847642; bh=C0LjEA6O+AZA4nvINfDDADru62AOGd+hzBqhzNNTMHU=; h=From:To:Cc:Subject:Date:In-Reply-To:From; b=Vj3rr46hZpTSGRR1hHIbxTThjEYZGZz7gLTNoQaAnJiEb2Hxbkb14CyKROVyo4src /AyH+n6QNOcJRutXRYaH1dpya4JIboafOSt9xTlYJeVUgYnGm2CN/Ey/JkR+/Kbme5 OWsZKBDeEG98Hp9rC6NGK7NN1zpFr069y2uPZiRg= Received: by simark.ca (Postfix, from userid 112) id 7555F1E11B; Mon, 24 Mar 2025 16:20:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Spam-Level: X-Spam-Status: No, score=-3187.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 Received: from simark.localdomain (modemcable238.237-201-24.mc.videotron.ca [24.201.237.238]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 832FF1E0C3; Mon, 24 Mar 2025 16:20:36 -0400 (EDT) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 2/2] gdb/dwarf: use std::equal_range in cooked_index_shard::find Date: Mon, 24 Mar 2025 16:20:28 -0400 Message-ID: <20250324202035.3727263-2-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250324202035.3727263-1-simon.marchi@polymtl.ca> References: <20250324202035.3727263-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Mon, 24 Mar 2025 20:20:37 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org From: Simon Marchi Looking at `cooked_index_shard::find`, I thought that we could make a small optimization: when finding the upper bound, we already know the lower bound. And we know that the upper bound is >= the lower bound. So we could pass `lower` as the first argument of the `std::upper_bound` call to cut the part of the search space that is below `lower`. It then occured to me that what we do is basically what `std::equal_range` is for, so why not use it. Implementations of `std::equal_range` are likely do to things as efficiently as possible. Unfortunately, because `cooked_index_entry::compare` is sensitive to the order of its parameters, we need to provide two different comparison functions (just like we do know, to the lower_bound and upper_bound calls). But I think that the use of equal_range makes it clear what the intent of the code is. Regression tested using the various DWARF target boards on Debian 12. Change-Id: Idfad812fb9abae1b942d81ad9976aeed7c2cf762 --- gdb/dwarf2/cooked-index.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index 1a2e19debab5..c0b906d734b7 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -547,23 +547,28 @@ cooked_index_shard::finalize (const parent_map_map *parent_maps) cooked_index_shard::range cooked_index_shard::find (const std::string &name, bool completing) const { - cooked_index_entry::comparison_mode mode = (completing - ? cooked_index_entry::COMPLETE - : cooked_index_entry::MATCH); - - auto lower = std::lower_bound (m_entries.cbegin (), m_entries.cend (), name, - [=] (const cooked_index_entry *entry, - const std::string &n) + struct comparator { - return cooked_index_entry::compare (entry->canonical, n.c_str (), mode) < 0; - }); + cooked_index_entry::comparison_mode mode; - auto upper = std::upper_bound (m_entries.cbegin (), m_entries.cend (), name, - [=] (const std::string &n, - const cooked_index_entry *entry) - { - return cooked_index_entry::compare (entry->canonical, n.c_str (), mode) > 0; - }); + bool operator() (const cooked_index_entry *entry, + const char *name) const noexcept + { + return cooked_index_entry::compare (entry->canonical, name, mode) < 0; + } + + bool operator() (const char *name, + const cooked_index_entry *entry) const noexcept + { + return cooked_index_entry::compare (entry->canonical, name, mode) > 0; + } + }; + + auto [lower, upper] + = std::equal_range (m_entries.cbegin (), m_entries.cend (), name.c_str (), + comparator {(completing + ? cooked_index_entry::COMPLETE + : cooked_index_entry::MATCH)}); return range (lower, upper); }