From patchwork Mon Sep 9 02:19:16 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: 97320 X-Patchwork-Delegate: dj@redhat.com 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 E0708385E82D for ; Mon, 9 Sep 2024 02:20:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 2F451385C6C7 for ; Mon, 9 Sep 2024 02:19:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F451385C6C7 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 2F451385C6C7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1029 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725848387; cv=none; b=B8F6NQy1HWlCNPk+71EHd6ZzkzsEXLk+XUkcs+ekke7/2A9U/2/lmDuXTX5X/IouSoUbqWryWqZIiyrlliFhCNVTSuroLMRBrPTIlpAdNK8nJ8ohkgLDG7N69IaRJCUtWXuo3aYu+3iDRpb9e/BSsel22QLs+Tm6ehf5VxgMs7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725848387; c=relaxed/simple; bh=svIGTcUl70LNWRTZptwk3+BjVjiq6gw903jd7wBuLos=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vExJUJAdbvSIIENhjx1L46LKvkEk7K+iF/Dq0wXGjqH9R//ez1bPuIzlfF7rLmKHt+d28hIE2iBjsGv50Xjha0mQLTpOjJNWXftYzf6YrabkMVh49/cYpD0w57NOYVCrhHtNYDq6YpmwHAqWW/3gzOrX69soq8fu9nENmnjhJ/A= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2d8a744aa9bso2464576a91.3 for ; Sun, 08 Sep 2024 19:19:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725848383; x=1726453183; 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=YMAyWeLkW7aOcoqS0XHzCvnApOPUd766M5L/ebhWhUI=; b=C8aQpcoQ+6Tv32rkt8Kky6nMUo5vNJY4ZTlq7o/dK8Q29aT+qRLHTU8frfvv2Lj+rx CmBkhxZZzdRIqQEY36RYAZBK8sOtemZgUEil+AK6FqLKkx+Ef//H4Kibos4oqcQMVfgc 5IKoIh9cG4asRIlLWs7+C6yBTCWQvrcgSKN0DFTzxK8mXnaBZ2aKXWBlP3BtlnaIBv08 88Uz4B/IynhNf6K3FqV8X+cN7k3nkvjvmBoAF3FKAMyhTX10vaMwiBjYRCd5GATm8rQJ gjybSxnNKvzfQTq0rMfml6GEYkTNpRDp0pKfXw6gtEKauRhlF5OmIyYardTZ9UmAaFAw +afQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725848383; x=1726453183; 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=YMAyWeLkW7aOcoqS0XHzCvnApOPUd766M5L/ebhWhUI=; b=uCzDfx5UHv0Ug6ofCq/nBu/hsB5OuBRzOtLNiHF4+o1NJ6jDP8nub3WeeFaujc/9KL W63MVDbHvIObiKiCTomD/66E4Y7rbC8eiVBRXvldLfQMiXkKf8ReujovAyB0oh4eB8Qm J5BepGZtScMgquuFuwclzp2pAJUCFt7QtlFVVuIJPZyvLvvTFNliE2vrI0wbTdR5SSOM cDxbJHeg8UPwKrN4qe98BSTUIPxf+v0fZoC49Iz2kpn4YZVJvIQIK1Mfb8VRpHTur3+L MaNX+tRlLYvres13ZDz/g/wsURX7mDEOvKioDVPHWVCaC4fliCcc1WIh85rXtBkMptzY 8DVg== X-Gm-Message-State: AOJu0YzcVoZaS6g2IZAP9YFx3p0Xk8x2f099ZFFVtoKelO1Ym7mLMulU RdftSIluky1GLTeUKPcf2Dbilz9fqY7YNCLmKVJNgMPfuWB73jsujmFBwg== X-Google-Smtp-Source: AGHT+IEiFnApL0qxzJsgAQ4iNde6yqqp25hM1vz3/DYPrR9HiW4Pf8PyqJAXnZii2tRvslSTmaozXw== X-Received: by 2002:a17:90a:f188:b0:2d8:6f66:1ebf with SMTP id 98e67ed59e1d1-2dad501b555mr9069409a91.20.1725848382733; Sun, 08 Sep 2024 19:19:42 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2dadc1104a3sm5398524a91.45.2024.09.08.19.19.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Sep 2024 19:19:42 -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 v6 1/2] benchtests: Add benchmark test for bsearch Date: Mon, 9 Sep 2024 10:19:16 +0800 Message-Id: <20240909021917.3159077-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240909021917.3159077-1-visitorckw@gmail.com> References: <20240909021917.3159077-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, KAM_SHORT, 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~patchwork=sourceware.org@sourceware.org Introduce a benchmark test for the bsearch function to evaluate its performance. Signed-off-by: Kuan-Wei Chiu --- Changes in v6: - Add 'nmemb' field to the 'results' array. benchtests/Makefile | 1 + benchtests/bench-bsearch.c | 137 +++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 benchtests/bench-bsearch.c diff --git a/benchtests/Makefile b/benchtests/Makefile index d228e9e68a..955b766756 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -253,6 +253,7 @@ hash-benchset := \ stdlib-benchset := \ arc4random \ + bsearch \ random-lock \ strtod \ # stdlib-benchset diff --git a/benchtests/bench-bsearch.c b/benchtests/bench-bsearch.c new file mode 100644 index 0000000000..8f88a1929c --- /dev/null +++ b/benchtests/bench-bsearch.c @@ -0,0 +1,137 @@ +/* Measure bsearch functions. + Copyright (C) 2022-2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define TEST_MAIN +#define TEST_NAME "bsearch" + +#define ARRAY_SIZE 100000 +#define LOOP_ITERS 100000000 + +/* Directly including leads to the use of an inline version + of bsearch(), which may cause our test cases to be optimized away by + the compiler due to predictability. To address this, we should + include directly and replace __extern_inline + with __attribute__((noinline)) to ensure the compiler does not + inline the function. Additionally, we need to add some macros + required for compilation. */ +#include +#define __extern_inline __attribute__((noinline)) +#define __GNUC_PREREQ(x, y) 0 +typedef int (*__compar_fn_t) (const void *, const void *); +#include +#undef __extern_inline +#undef __GNUC_PREREQ + +#include "json-lib.h" +#include "bench-timing.h" + +int arr[ARRAY_SIZE]; + +static int +comp_asc (const void *p1, const void *p2) +{ + int x1 = *(int *) p1; + int x2 = *(int *) p2; + + if (x1 < x2) + return -1; + if (x1 > x2) + return 1; + return 0; +} + +static int +comp_desc (const void *p1, const void *p2) +{ + int x1 = *(int *) p1; + int x2 = *(int *) p2; + + if (x1 > x2) + return -1; + if (x1 < x2) + return 1; + return 0; +} + +static void +do_bench (json_ctx_t *json_ctx, bool ascending, bool contained) +{ + size_t i, iters = LOOP_ITERS; + timing_t start, stop, cur; + int key; + volatile __attribute__((__unused__)) void *res; + + for (i = 0; i < ARRAY_SIZE; ++i) + { + arr[i] = (ascending ? i : ARRAY_SIZE - 1 - i) << 1; + } + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "array-size", ARRAY_SIZE); + json_attr_string (json_ctx, "key-pattern", ascending ? "ascending" : "descending"); + json_attr_string (json_ctx, "contained", contained ? "yes" : "no"); + json_attr_string (json_ctx, "simple", "yes"); + json_attr_uint (json_ctx, "nmemb", ARRAY_SIZE); + + TIMING_NOW (start); + + for (i = 0; i < iters; ++i) + { + key = (i % ARRAY_SIZE << 1) + !contained; + res = bsearch(&key, arr, ARRAY_SIZE, sizeof(arr[0]), ascending ? comp_asc : comp_desc); + } + + TIMING_NOW (stop); + + TIMING_DIFF (cur, start, stop); + + json_attr_double (json_ctx, "timing", (double) cur / (double) iters); + json_element_object_end (json_ctx); +} + +int +do_test (void) +{ + json_ctx_t json_ctx; + + json_init (&json_ctx, 0, stdout); + + json_document_begin (&json_ctx); + json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); + json_attr_object_begin (&json_ctx, "functions"); + json_attr_object_begin (&json_ctx, TEST_NAME); + json_attr_string (&json_ctx, "bench-variant", "default"); + json_array_begin (&json_ctx, "results"); + + for (int ascending = 0; ascending < 2; ++ascending) + { + for (int contained = 0; contained < 2; ++contained) + { + do_bench(&json_ctx, ascending, contained); + } + } + + json_array_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_document_end (&json_ctx); + + return 0; +} + +#include From patchwork Mon Sep 9 02:19:17 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: 97321 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 410E1385C6C7 for ; Mon, 9 Sep 2024 02:20:51 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by sourceware.org (Postfix) with ESMTPS id DA457385B50D for ; Mon, 9 Sep 2024 02:19:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA457385B50D 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 DA457385B50D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725848404; cv=none; b=mkGRHlZFOCsCWJWPV5V/7DCtVFijfrc/nfZkUSSQ1PA5SIYMxpH1/4rmQMKQJDn3FKyCG0NsH+kibIn3tjP+zTX3fPIBZTtGHxTcTEyT/RflMoQZqPrF+lO7MEIRrsI1+Ayv729P79XMXG43saIlm398CuCxVtwfdNGPHHAXMD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725848404; c=relaxed/simple; bh=+PMq0WmUhhCBldZpmDcTncE2DsxHM46DQxwYeZ9Bm18=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FlMYA9p9rnPMrHvRej+0nTpNatzq9yIPpwsEKTymP2M1hQ+SCk26zCvnwPftl2MeFuXqp6yqfmNM7OmZ4JDmK2cQRTPCwtOnu1cpBCwWKYGHdToj0P18ThF/UXb1S5Rh6d+DsFVT7sb84d61+V0gmxD1Pdle9aDmN+HDOxZZHFw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-2d1daa2577bso2786223a91.2 for ; Sun, 08 Sep 2024 19:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725848393; x=1726453193; 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=oXp5VvEhd/1TXz0+pOsI6nLYKbjjichS1ivegvVw6RM=; b=gIrefIr0Xu2wwmSGftWADb4/+vRXCY1Qrideq61vqgGZPaMe+p/rk8/WPmMQh9Aw49 EQFjVSECvUVriq6toBlnp/F4Ors/DroT4l0Z27LpFqRkTJiRdnr2E30rBDbq5ZKFfaP1 2jlif84nTuxTl9weCEJ7nMkRR3E3Dgqfs9vRT5y9lDmhedhS5x4MdPL6uvRVaWdx0fq0 3Fu4LIbPBMrbMKhK3B6dDJayw41qXjxXFWvN6CfbVYUMTMUUkdFCjsKfVOvc3m4n9UE9 8C8QS0veDCaQPtavr3r2/wGdYKTTxxUkrJBNFguc9W57nb9XquVYcfePk5bDJLY2M34b JXaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725848393; x=1726453193; 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=oXp5VvEhd/1TXz0+pOsI6nLYKbjjichS1ivegvVw6RM=; b=TWWzwY4UJjVEHAVmzYXyX1Lz+Q8zxA2pYWg7nvsyQV4Vkt2g+uiu1VxDn8t7SZp0Hp d8iUXoo2Wj21GsNCOCYf2hsu89vOWuSPGS86sZhSFhXjd+sM794kxrD/N5km6kxlbTyl ttojz6YoDV+sGiTG1S10kinfXa/oOFW9UUZGUTQwasuUlM2WWyutybQDcWWKe0Vkn9rQ 4A1W+AGnCaDGopZuiuYnL65mhfvcICT16csPAA/4kc5hN2UAJqtKkQPIjf0ntF9bH9VP rs+EIqFs1aqPDl8Y4G6j9Agy+iOcPBknj5pBCnLmd2qhaDfjuAsCMHh7Jf3PNdYZTNjs ae/A== X-Gm-Message-State: AOJu0YzKuu+M7cD33Ua98Ug2WSlSHR1xxKoauGcarlKiUBeeYVs4SgpW ZefCRVX9j8Z3cfC0EiHbz7l+veHvaAtMhewZ4sTPKBhe0NwKSeZ0aMl+5w== X-Google-Smtp-Source: AGHT+IH9Vof/taLUPSmph7q0juIc1EkfFZb30xEIQndaxiZ2UJv/vdX+hSW0nrSdAajCWwJTJC0EWw== X-Received: by 2002:a17:90a:8c8c:b0:2cb:e429:f525 with SMTP id 98e67ed59e1d1-2dafd09713amr7404950a91.33.1725848392712; Sun, 08 Sep 2024 19:19:52 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2dadc1104a3sm5398524a91.45.2024.09.08.19.19.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Sep 2024 19:19:52 -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 v6 2/2] Optimize bsearch() implementation for performance Date: Mon, 9 Sep 2024 10:19:17 +0800 Message-Id: <20240909021917.3159077-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240909021917.3159077-1-visitorckw@gmail.com> References: <20240909021917.3159077-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 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~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 the bench-bsearch benchmark results. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Noah Goldstein --- 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;