From patchwork Sat Sep 7 15:48:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 1982156 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=LVtXH8Vc; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X1HbD1dLPz1y1V for ; Sun, 8 Sep 2024 01:49:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 390D4385C6CA for ; Sat, 7 Sep 2024 15:49:25 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id A8E2A3858C48 for ; Sat, 7 Sep 2024 15:49:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A8E2A3858C48 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A8E2A3858C48 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725724149; cv=none; b=hQPWyUc6x2pVFgwCKYzUKS1FOpLyYeCjpFCcaQ9mLDZaISOqxAD34DZzZ7uH2NC2qJDqR7ldOyrZhq5GVlQCWzr/tgsB2WzyzIkIMNAoqDh1C0b+Xt5ZAzBcraCdpQCR12nrzv+IWoURBtfT6kk0XjxvPO3j8/CePbnkr72bfYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725724149; c=relaxed/simple; bh=oZgiUX4+z8ZyTxC3b9634wS8dQ/U69Yfedut6TGaP6A=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Qx3qbmYsmuylqL+S8gncwNlRJ05t9dGvyHJXoLXtgnmi1nB63wODAngcJ59SpIfW7gx3YRGco0Ry1g9QTnpGFNtgYWRvN0GD/wIXrlQEt8qaKkKfKULKC7tXKAEIB45YtCCnmNqMEUXPAl7CZVNz1wvBZP/AX83R4d3HOkCX18Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-717929b671eso2431037b3a.0 for ; Sat, 07 Sep 2024 08:49:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725724146; x=1726328946; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DrPZXQ9zffSWsy3sy88sROayKZYIF2z9AQTXUcCU8yI=; b=LVtXH8VcxLe6eFCzmENgZi5UMTa7aIQvhyMjJImHQho3S2UgSj5YeUOX0lZeuLBBOZ /Dx6Vbjh330Y6ILT1nJk8lHElKVp0lL5I/nwIMVkj51TahwkRyR2jPdMJZiQxwmOU1D1 HiaBlCgNl40o+g5In6ND8SgBl4UzAeUHFXBxkuGpcDrhr4oaxyG8qPasiVc+2IJRiToM 0/wBfUVZaIWZtrjjhz59/XEJ3qpLXq6tJtmEqrcgajsajwlztVr6gEMDlAb6GiOPGvSA FsMQBZid9KPUwqKLC3abscCGpDC/h6ZGVX5ucMMLu1z1na5DWGwVEQZO02dh1SdBTyPm h2lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725724146; x=1726328946; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DrPZXQ9zffSWsy3sy88sROayKZYIF2z9AQTXUcCU8yI=; b=kS0u6+2GXrtcGo+25LIJvti/lgIpKqgGpvK1mjQVrb3G9QlpWzorTrO7Kfy7BbHDJM Cqsap6nId5yUT49RWI/0bWhqANO1slLlnj86xPrh8ySXk/uGTKL6dEG1hw/70oh+1YRy YwWrtLLmvFbEzSQBCS1Mx370ZRCPUPODed1IYoCTmQtuFVuKocdrrCrjnbm/a+gQ+lAC vF6OFN1RSNSlApQrRyvuUlewv7vY8DPBu71ts1nmMOz4u0g/pLA25AWpyTBdjEhfiGiV GP6cb9VgL06eKXsxmc1mZO4j4PKFelc8CURssaTrPz5ZDmX/h9TTjOcG584AxCF8afTh oMVQ== X-Gm-Message-State: AOJu0YxJaZYh2JkxhMjLliOGvUDMs4VZQk22veP0TjP/ovn3c3myp9rT wLq/yc7g2L1BI56uA+xFNBOP21E94mIbmW3s6hJiVWgHi/6fI5h4oiraGQ== X-Google-Smtp-Source: AGHT+IGg6dc+T+YzjzHuMfzO1eKkJ2EY6UyvkvomNMd06N2rXzb7QOb8b9jZyoaAPIwZSllCDNbMqg== X-Received: by 2002:a05:6a00:4b10:b0:70d:3777:da8b with SMTP id d2e1a72fcca58-718d5f1958fmr5826602b3a.25.1725724145547; Sat, 07 Sep 2024 08:49:05 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58ca745sm1016913b3a.88.2024.09.07.08.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Sep 2024 08:49:05 -0700 (PDT) From: Kuan-Wei Chiu To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, jserv@ccns.ncku.edu.tw, Kuan-Wei Chiu Subject: [PATCH v5 2/2] Optimize bsearch() implementation for performance Date: Sat, 7 Sep 2024 23:48:38 +0800 Message-Id: <20240907154838.2781255-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240907154838.2781255-1-visitorckw@gmail.com> References: <20240907154838.2781255-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Optimize the bsearch() function to improve binary search performance. Although the code size grew by 8 bytes, the new implementation achieves a 15% reduction in execution time on my x86 machine, according to the bench-bsearch benchmark results. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Noah Goldstein --- Changes in v5: - Drop the full benchmark log from the commit message as it is becoming too verbose. bits/stdlib-bsearch.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h index 540d718952..57f060b504 100644 --- a/bits/stdlib-bsearch.h +++ b/bits/stdlib-bsearch.h @@ -20,22 +20,14 @@ __extern_inline void * bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size, __compar_fn_t __compar) { - size_t __l, __u, __idx; const void *__p; int __comparison; - __l = 0; - __u = __nmemb; - while (__l < __u) + while (__nmemb) { - __idx = (__l + __u) / 2; - __p = (const void *) (((const char *) __base) + (__idx * __size)); + __p = (const void *) (((const char *) __base) + ((__nmemb >> 1) * __size)); __comparison = (*__compar) (__key, __p); - if (__comparison < 0) - __u = __idx; - else if (__comparison > 0) - __l = __idx + 1; - else + if (__comparison == 0) { #if __GNUC_PREREQ(4, 6) # pragma GCC diagnostic push @@ -46,6 +38,12 @@ bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size, # pragma GCC diagnostic pop #endif } + if (__comparison > 0) + { + __base = ((const char *) __p) + __size; + --__nmemb; + } + __nmemb >>= 1; } return NULL;