From patchwork Wed Sep 4 16:01:40 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: 97088 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 145E0386482B for ; Wed, 4 Sep 2024 16:02:53 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 404D13858406 for ; Wed, 4 Sep 2024 16:02:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 404D13858406 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 404D13858406 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725465750; cv=none; b=jXJGCiGsuhIhhkBgfBgaYtNhV8rwb4fscQf6PGV9++Oi86lqnVMxIY4/D/4HHYd55A6Ba7TblGaMOQoyHSt6bCIRKxaqiI5diKFlWXyqSjuvHaKI9nXlXl9uWiKcY4M3HCk3vLWmu/jgHCaJmyjyRMXLMdXWo+ZqNV+mPeKf5/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725465750; c=relaxed/simple; bh=FR+n2LK9StFcXbx7w4mXoAWNiAK2rfrbhG/jRZPyLIc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Qcj0EihlZ+x1iG6fEDhE40RsMCcMN2BJGFh91lIJM7NoFSEOToaVs4QG+WV1Y3i+mlf9x5PZgOmppruRKY1ZSt8mdB2gSycOx4F1lUe7d2UqWGd1BaPuj3+PtuO+hfX26Hh6FnHh4tc4iLJSA300wukxYQrZrP8zNOzMs3fH0Ag= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-2059112f0a7so28100055ad.3 for ; Wed, 04 Sep 2024 09:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725465748; x=1726070548; 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=k/OGZCbA5GLJe2ts9BIu9zt1v9DpbdHwINSg4SgBCoY=; b=EHxdXZH0ZFnFWnpOZly7bfjEjY36ES40lVk/mmHkIH2hPAEuxgCc67wAZFlGzza533 9ebyV6ZFYp6hEKSyNSIc23d4BCFsdqIQkuPozjEaYPtETfZZ6SiThb0RXs/SpdL7BQQo Ws5MXnNHMXqtno6cZKMOAzFGMM5IzUTdeXzYJqZUcArycrXN1WlOXzwP1mJzsWMo8VB/ 83Dlkf7/X386m27O/Vs+MEX/w6rmoe06VxUmDBM/+gYk8yyVjSIOIi1RJygXrt60ZrWc PMZc0tU5mDqW+ZJMHKKCOnZd6NePFubMobtG+H1dq+1zQhXPdcwnKzIDovnObO7U29Y+ TzYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725465748; x=1726070548; 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=k/OGZCbA5GLJe2ts9BIu9zt1v9DpbdHwINSg4SgBCoY=; b=gHDPy74gGSVDb1XngJlpoV0iSsgHE+93W6VGTvmlQAtc1ruFyGyviw2yVa5Ie5HGmA FB4i7USQvkZpda9B+fDzT8nfZADK0QsrOABa78kGacefGBkQ84nBTxEcpPGkMnoCuzOA 1ZpFQ5U12Zd4ETFimdI1NFmQyAdHH1vM/HKnlljBeoSnxRJpjTdnGjN2oaKkIdoW1HXP 4cwyL9F/GeAAVrwPmDKCPg66HUcgfX3AZuQ+FGyuC+6Q6XB8HTehwT1tKOs0NtsbboFu M2XDtekcAs6bmCM9P4UT4tzqRI5wZlHuzA/x6rFUy+lA0rBqj85HNLehYzUGZtcilEkd 7Zng== X-Gm-Message-State: AOJu0Yxa1vXx7/UxdE8mJ69jc9yaV0Q1YCdxDaTNuB0y+BOj992WECv0 ywYuz8bprGAUgPHdx/2QMmv0fbImhlWe7+Jw9XsLDIQdoITqkAondlhOaQ== X-Google-Smtp-Source: AGHT+IH67DP2fN9IYYe4icKtmCYE4gJSAaSFojp7ZQu7NwrBNgP90mbeKOxMR8aCOkY9/Rsu6xmxYg== X-Received: by 2002:a17:902:c40e:b0:202:4640:cc68 with SMTP id d9443c01a7336-2050c48fb65mr195399025ad.59.1725465747630; Wed, 04 Sep 2024 09:02:27 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea38c91sm15193565ad.169.2024.09.04.09.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 09:02:27 -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 v4 2/2] Optimize bsearch() implementation for performance Date: Thu, 5 Sep 2024 00:01:40 +0800 Message-Id: <20240904160140.2743366-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240904160140.2743366-1-visitorckw@gmail.com> References: <20240904160140.2743366-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, T_SCC_BODY_TEXT_LINE 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~patchwork=sourceware.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 benchmark results. code size: * old: text data bss dec hex filename 250 0 0 250 fa ./stdlib/bsearch.o * new: text data bss dec hex filename 258 0 0 258 102 ./stdlib/bsearch.o benchmark: * old: { "timing_type": "hp_timing", "functions": { "bsearch": { "bench-variant": "default", "array-size": 100000, "key-pattern": "ascending", "contained": "yes", "results": [121.735] } } } * new: { "timing_type": "hp_timing", "functions": { "bsearch": { "bench-variant": "default", "array-size": 100000, "key-pattern": "ascending", "contained": "yes", "results": [103.063] } } } Signed-off-by: Kuan-Wei Chiu Reviewed-by: Noah Goldstein --- Changes in v4: - Update the benchmark result format in commit messages. 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;