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;