From patchwork Tue Sep 3 06:30:55 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: 1979841 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=kv0t6Emh; 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 4WybPM3wcdz1yfv for ; Tue, 3 Sep 2024 16:31:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A40A385EC29 for ; Tue, 3 Sep 2024 06:31:33 +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 7D4BE3858401 for ; Tue, 3 Sep 2024 06:31:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D4BE3858401 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 7D4BE3858401 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=1725345074; cv=none; b=enBCz68swnqNgaDEqKUDha28Bdz9U7HYyfqfQ+NmDQg2bW4vPVD9uKAsD60AJVVPRl9XV6dUzPnjN23Nbu4PJXdI5FVHhjSSc34Qqoy5YAhrgfEMF3L0NerlYrq4QKl9oKX+2d+47WFOtqQZ1bKHjzrTujMRH+3FDXQh3K7MXzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725345074; c=relaxed/simple; bh=Ihh0vlTQhLMchkmNiulE4YfLSORBz0oRV/07zyJnOdw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vp1oyU79pMJk9UESbCxYph+X8W/zmizErK0wh2GkSXQM2DVkmaKenDlq9IDg5npUn9CeuwK7jYDXhxi8PXsUXGyvXiOMnTD3FtJca8wyftmXa1D9akZMYKWuXRSiEgVGsQtlZamxJyumcUnm1sNW5VJfGQZHzqRY51XKj4lWQug= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-205722ba00cso11850155ad.0 for ; Mon, 02 Sep 2024 23:31:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725345070; x=1725949870; 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=OpHoWha1exCVulDdKRFpoZTgnTDKZvqx5mO0AACNTH4=; b=kv0t6EmhTuq6CpqpbXp/tyLEp2culVH1QEPmaN9ssiSDOdzTmMOxxXezE0pCKQxwwe Gtz9g7+9Fxa3eSsAUpoR22fWBesGG1PrsRNNRu0Imciu6rpqNL1lFrqW9AFfBLn0TQyx 4S1kh0A99wvoPv1s3roF/rswgAc77SMeSp2je0F5Ps+s03z1na74RQMnPPA056wljaoZ I8SDGAN6mLRTJMcKRl0WxUlkpJZUeE5rrxObvovwjlGkEkjSnVvxfjNAuZtvU2ZGnuXv 2S73Nsg1o9ympy45U94jtlS/Qz/aUsT98e/NYVDKXOWAMFyt2/JMP0h3D+Xv51YukJjC qp1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725345070; x=1725949870; 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=OpHoWha1exCVulDdKRFpoZTgnTDKZvqx5mO0AACNTH4=; b=vPKSQTAD6O2YwMhVlfR1oca6WTiVLQEzOQZDXQ+gB9X+aDKBb8WNQ5jhZzxnOgQRjn 6rjDQsgJ+ZNliiVBo6akyQYiBeASRJkFr2B1CIQPpPzGTytUXwN3/0SE68V9wyODASqi a2VGie3b6/DSIvp1g6gYc3IYpg7gGfEXOPL91w3iBYRBQBLQa1DuimpM1kdRCSTtnWLA VAU+SlxRkBF9/YHQIVmiy2Biu94REj7t24MV23LfBU9wo7ZZmZ7NogFm7cRqpO9o43gn xU20JClUBW38jpjkzRuTq6wnIOmw+DPA04QC5PwOA7JLlU7R1aUKuzvKyrGi/SGKK+71 Ay0w== X-Gm-Message-State: AOJu0YyW8pfq7VaCjuoUt+rloqFFkqs6LddMQv5SLL8z+Gtsbl7DtvEw F66nlXGABfubcDPnDA/CthnwzU3BPhGwxJ2XAOVO0nX6VcOWulDQC7WWfQ== X-Google-Smtp-Source: AGHT+IHYCY2aK/ajd9eJsqiKCyVuYZI4xnI7GLHYitn5jtT1J6l7Qe/Z1KktxTqrbRZlCoEWKqNoDg== X-Received: by 2002:a17:902:d2c2:b0:202:38d8:16c with SMTP id d9443c01a7336-2054733519dmr75523095ad.37.1725345070281; Mon, 02 Sep 2024 23:31:10 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-205784d556dsm24803815ad.269.2024.09.02.23.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 23:31:09 -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 v3 1/2] benchtests: Add benchmark test for bsearch Date: Tue, 3 Sep 2024 14:30:55 +0800 Message-Id: <20240903063056.2724742-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240903063056.2724742-1-visitorckw@gmail.com> References: <20240903063056.2724742-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 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, 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~incoming=patchwork.ozlabs.org@sourceware.org Introduce a benchmark test for the bsearch function to evaluate its performance. Example bench-bsearch.out: { "timing_type": "hp_timing", "functions": { "bsearch": { "bench-variant": "default", "results": [121.887] } } } Signed-off-by: Kuan-Wei Chiu --- Changes in v3: - Sort the stdlib-benchset entries in Makefile to resolve CI failures. benchtests/Makefile | 1 + benchtests/bench-bsearch.c | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 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..9ec88af60a --- /dev/null +++ b/benchtests/bench-bsearch.c @@ -0,0 +1,110 @@ +/* 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 (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) +{ + 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] = i; + } + + TIMING_NOW (start); + + for (i = 0; i < iters; ++i) + { + key = i % ARRAY_SIZE; + res = bsearch(&key, arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), comp); + } + + TIMING_NOW (stop); + + TIMING_DIFF (cur, start, stop); + + json_element_double (json_ctx, (double) cur / (double) iters); +} + +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"); + + do_bench(&json_ctx); + + 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 Tue Sep 3 06:30:56 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: 1979842 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=S18xr0au; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WybPN2n3Hz1yfv for ; Tue, 3 Sep 2024 16:31:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8CBED385EC36 for ; Tue, 3 Sep 2024 06:31:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id C43B4385E836 for ; Tue, 3 Sep 2024 06:31:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C43B4385E836 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 C43B4385E836 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725345077; cv=none; b=s/otoXu/GOXvMqRFpRt+iu+xqtFBOI+9G5HSvSZ6qmG+2DcTgDpbtFocj8mkIhWmyWK0OXbBk6DxDlhFTM1UvmAFUQ4i/4NBLvqAgAkaxtALvEwD6zrdeTaRYFngifcI0r659uFrweZ1JJ9SIF446NW+SRo0FDX/xCZvBtALF+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725345077; c=relaxed/simple; bh=G576omggcsbI9xiwVk9P0xeAfsgDYcyHdQOnJLv7ahE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=W2PQGVH3VRs+mK1VEtgyVXXVhE6EMuM2z1cMbfOK5Lp5r4wJP/FIO39vtnocAcT1l59iTuwgoD7qnKqcyXBHkkRhWOTslhyfjY/7+pt78j200h0bk62E5K0GoIIYptQViPWDrRqPWo0HXr0NnCCZrJqXvptInV9HkiRrzAH/odk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1fc47abc040so38907515ad.0 for ; Mon, 02 Sep 2024 23:31:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725345073; x=1725949873; 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=X6X0T+bprDi6l/2EnbSzPhSE+zQ/rRGZX/jNxpj5+iM=; b=S18xr0auo11DG/Q+At5uk8KyczrqwCiItC8Uv8SNifdH6LbZbkB/r7AKUfef3coYRN EauK4qhsPsxkH5A3tMZlDvwKiXWhlkJN3RuZdlG4UqIPjetiB5+4N78m7P2awJ8WSfqH Jw1AZ1Y2jMpdrpkMKXFzLSOyUspDYv9hlZl7oZkTKngEkI4fIxq7bWASFHXaCQwg2qNa 4pob2SvaHxJpS7I5Jcz9KGUT3YnAWCOucHB6H4xrj84MvKqCOA90k5RpWEeUvSNXgXMn 3alUBp+UIaud4E43K02EWl2PqEcaSmb40QrKrp4WRHye54D2VzeDDHjTJXoOD7Gm0jrx 52Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725345073; x=1725949873; 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=X6X0T+bprDi6l/2EnbSzPhSE+zQ/rRGZX/jNxpj5+iM=; b=eMLNTqpKegSpCi25ky5rFb/LT8yumzK71Fh8th+8/YV/QkgewAG4jHwK+UIgVWpYYz k0VuT5hyNmgk/bnpDK36vUz09G2kp0NCG0UmMcFB0TIrp3ssnGrWgzHK19ve22DXyivC peHR5yQIlpTno5p4TkPT7zcOKUy+kM4FNmgUwm+Y6qjPXxNnYe5vA0LX2Fs9tF0VTYZk epa2gZZHf8EkoiMfnUMttBtswxuoDKGAbdTQNQP5KFUxlAi0y31qdjKTdjU4DTjWmP6M mxSZyX7m/nzJnJiO4eccUQWOzP8TUBb48lnlcVKLSflT9KjUpTxRYHQocIMiBCLeUhK/ epRA== X-Gm-Message-State: AOJu0YxUTiQy1lNHji7ZaBdlaPSWZG/6mZu8pwfBSxoj2wgpf4C81dZj +1gUDPAoWPAgGezl83+CxkxwmM+gNc8iNhmZEM4RqbeVGWVCHWKyaMVArw== X-Google-Smtp-Source: AGHT+IGj4fSfAeNjgu2weTn+UaekSeJpBkY7un1B+krXwEHY0JN4CHm73X/FC+6bhn4ve/H/jpr7qw== X-Received: by 2002:a17:902:e950:b0:205:936e:9489 with SMTP id d9443c01a7336-205936e967amr36564595ad.6.1725345073036; Mon, 02 Sep 2024 23:31:13 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-205784d556dsm24803815ad.269.2024.09.02.23.31.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 23:31:12 -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 v3 2/2] Optimize bsearch() implementation for performance Date: Tue, 3 Sep 2024 14:30:56 +0800 Message-Id: <20240903063056.2724742-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240903063056.2724742-1-visitorckw@gmail.com> References: <20240903063056.2724742-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 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~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 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", "results": [121.887] } } } * new: { "timing_type": "hp_timing", "functions": { "bsearch": { "bench-variant": "default", "results": [103.046] } } } Signed-off-by: Kuan-Wei Chiu Reviewed-by: Noah Goldstein --- Although the benchmark results on my x86 machine are promising, I would appreciate assistance in verifying that no regressions occur on other architectures. 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;