From patchwork Tue Oct 18 23:19:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1691740 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Sx7CKmTh; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MsVHL6lgWz23jk for ; Wed, 19 Oct 2022 10:21:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 332C73857366 for ; Tue, 18 Oct 2022 23:21:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 332C73857366 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666135292; bh=QLYf2t8uZJScMAunEK0mJZeQMtIfs8O9tj5QAcGnu8g=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Sx7CKmThw0o9sjKH68miVwrdZvOirfn+Kpc2HuFmP9pZ3uXgdUSM+Z9zt3io2EZv6 5mwogVv2GGunE6TREXp4BRr4fsvLvOrdlJWUfpQj2I3z9SCAwS64M2jJ5iS0IZG/d9 mO9tiG+3718WM97qVMeT1YORluH+by/4veeXdQLg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by sourceware.org (Postfix) with ESMTPS id 915A03858283 for ; Tue, 18 Oct 2022 23:19:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 915A03858283 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-132af5e5543so18671254fac.8 for ; Tue, 18 Oct 2022 16:19:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=QLYf2t8uZJScMAunEK0mJZeQMtIfs8O9tj5QAcGnu8g=; b=I42RjBBxUeg5WtNK1ZAksq1bo9MJvxDg3+byCmFPSlC7n5U3JkxeKDEN0LnKY2ai9K RCCG/4paKOU4EIFrgzep5noz5+cfOSxQ3py5ybhL/1Qsr2qmUdGF14Jmng414OsXJlvA 3GkblQ93lqBthoc6VOf4shjbontupkgwTXcbhyp+KEslkBlb4LTaFyyL6ZdHbKmMnmPs gqX1HdJsPXEjcE9ySn5k3JessivPB1Pxk2hIEpDVLsdEoyPKo2eCuhnQmQe8YnHdTNRF cmS2z3WVOLn0cn/wDiST2nfpvaAUDqpNTpWSh3uM4/gEfHjDBDeV/r92qLj9i/yAm8V7 JiIw== X-Gm-Message-State: ACrzQf2Gng7lmdqSSHrGVPgTGq+ty8eBYbUHyioyNlhoPzR/o9Lm7MVT skK3DRt0zAIK6cOuATljM1zLOA/zPAi+Yw== X-Google-Smtp-Source: AMsMyM68whe5gwk1beorpHHBxO/21TNZpGcE+n5hj0y8MEW4HBGWYRL3VipzxnGD3rSQ/TBL5XT8AQ== X-Received: by 2002:a05:6870:6587:b0:131:d289:c928 with SMTP id fp7-20020a056870658700b00131d289c928mr19587045oab.75.1666135190578; Tue, 18 Oct 2022 16:19:50 -0700 (PDT) Received: from noah-tgl.lan (2603-8080-1301-76c6-27cf-8854-3909-9373.res6.spectrum.com. [2603:8080:1301:76c6:27cf:8854:3909:9373]) by smtp.gmail.com with ESMTPSA id x27-20020a056870a79b00b001372c1902afsm6787119oao.52.2022.10.18.16.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 16:19:50 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 7/7] Bench: Improve benchtests for memchr, strchr, strnlen, strrchr Date: Tue, 18 Oct 2022 16:19:38 -0700 Message-Id: <20221018231938.3621554-7-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221018231938.3621554-1-goldstein.w.n@gmail.com> References: <20221018024901.3381469-1-goldstein.w.n@gmail.com> <20221018231938.3621554-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 1. Add more complete coverage in the medium size range. 2. In strnlen remove the `1 << i` which was UB (`i` could go beyond 32/64) 3. Add timer for total benchmark runtime (useful for deciding about tradeoff between coverage and runtime). --- benchtests/bench-memchr.c | 77 +++++++++++++++++++++++++----------- benchtests/bench-rawmemchr.c | 30 ++++++++++++-- benchtests/bench-strchr.c | 35 +++++++++++----- benchtests/bench-strnlen.c | 12 +++--- benchtests/bench-strrchr.c | 28 ++++++++++++- 5 files changed, 137 insertions(+), 45 deletions(-) diff --git a/benchtests/bench-memchr.c b/benchtests/bench-memchr.c index 0facda2fa0..2ec9dd86d0 100644 --- a/benchtests/bench-memchr.c +++ b/benchtests/bench-memchr.c @@ -126,7 +126,7 @@ do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int test_main (void) { - size_t i; + size_t i, j, al, al_max; int repeats; json_ctx_t json_ctx; test_init (); @@ -147,35 +147,46 @@ test_main (void) json_array_begin (&json_ctx, "results"); + al_max = 0; +#ifdef USE_AS_MEMRCHR + al_max = getpagesize () / 2; +#endif + for (repeats = 0; repeats < 2; ++repeats) { - for (i = 1; i < 8; ++i) + for (al = 0; al <= al_max; al += getpagesize () / 2) { - do_test (&json_ctx, 0, 16 << i, 2048, 23, repeats); - do_test (&json_ctx, i, 64, 256, 23, repeats); - do_test (&json_ctx, 0, 16 << i, 2048, 0, repeats); - do_test (&json_ctx, i, 64, 256, 0, repeats); - - do_test (&json_ctx, getpagesize () - 15, 64, 256, 0, repeats); + for (i = 1; i < 8; ++i) + { + do_test (&json_ctx, al, 16 << i, 2048, 23, repeats); + do_test (&json_ctx, al + i, 64, 256, 23, repeats); + do_test (&json_ctx, al, 16 << i, 2048, 0, repeats); + do_test (&json_ctx, al + i, 64, 256, 0, repeats); + + do_test (&json_ctx, al + getpagesize () - 15, 64, 256, 0, + repeats); #ifdef USE_AS_MEMRCHR - /* Also test the position close to the beginning for memrchr. */ - do_test (&json_ctx, 0, i, 256, 23, repeats); - do_test (&json_ctx, 0, i, 256, 0, repeats); - do_test (&json_ctx, i, i, 256, 23, repeats); - do_test (&json_ctx, i, i, 256, 0, repeats); + /* Also test the position close to the beginning for memrchr. */ + do_test (&json_ctx, al, i, 256, 23, repeats); + do_test (&json_ctx, al, i, 256, 0, repeats); + do_test (&json_ctx, al + i, i, 256, 23, repeats); + do_test (&json_ctx, al + i, i, 256, 0, repeats); #endif + } + for (i = 1; i < 8; ++i) + { + do_test (&json_ctx, al + i, i << 5, 192, 23, repeats); + do_test (&json_ctx, al + i, i << 5, 192, 0, repeats); + do_test (&json_ctx, al + i, i << 5, 256, 23, repeats); + do_test (&json_ctx, al + i, i << 5, 256, 0, repeats); + do_test (&json_ctx, al + i, i << 5, 512, 23, repeats); + do_test (&json_ctx, al + i, i << 5, 512, 0, repeats); + + do_test (&json_ctx, al + getpagesize () - 15, i << 5, 256, 23, + repeats); + } } - for (i = 1; i < 8; ++i) - { - do_test (&json_ctx, i, i << 5, 192, 23, repeats); - do_test (&json_ctx, i, i << 5, 192, 0, repeats); - do_test (&json_ctx, i, i << 5, 256, 23, repeats); - do_test (&json_ctx, i, i << 5, 256, 0, repeats); - do_test (&json_ctx, i, i << 5, 512, 23, repeats); - do_test (&json_ctx, i, i << 5, 512, 0, repeats); - - do_test (&json_ctx, getpagesize () - 15, i << 5, 256, 23, repeats); - } + for (i = 1; i < 32; ++i) { do_test (&json_ctx, 0, i, i + 1, 23, repeats); @@ -207,6 +218,24 @@ test_main (void) do_test (&json_ctx, 0, 2, i + 1, 0, repeats); #endif } + for (al = 0; al <= al_max; al += getpagesize () / 2) + { + for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i) + { + for (j = 0; j <= (384 / sizeof (CHAR)); + j += (32 / sizeof (CHAR))) + { + do_test (&json_ctx, al, i + j, i, 23, repeats); + do_test (&json_ctx, al, i, i + j, 23, repeats); + if (j < i) + { + do_test (&json_ctx, al, i - j, i, 23, repeats); + do_test (&json_ctx, al, i, i - j, 23, repeats); + } + } + } + } + #ifndef USE_AS_MEMRCHR break; #endif diff --git a/benchtests/bench-rawmemchr.c b/benchtests/bench-rawmemchr.c index b1803afc14..dab77f3858 100644 --- a/benchtests/bench-rawmemchr.c +++ b/benchtests/bench-rawmemchr.c @@ -70,7 +70,7 @@ do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_ch size_t i; char *result; - align &= 7; + align &= getpagesize () - 1; if (align + len >= page_size) return; @@ -106,7 +106,6 @@ test_main (void) { json_ctx_t json_ctx; size_t i; - test_init (); json_init (&json_ctx, 0, stdout); @@ -120,7 +119,7 @@ test_main (void) json_array_begin (&json_ctx, "ifuncs"); FOR_EACH_IMPL (impl, 0) - json_element_string (&json_ctx, impl->name); + json_element_string (&json_ctx, impl->name); json_array_end (&json_ctx); json_array_begin (&json_ctx, "results"); @@ -137,6 +136,31 @@ test_main (void) do_test (&json_ctx, 0, i, i + 1, 23); do_test (&json_ctx, 0, i, i + 1, 0); } + for (; i < 256; i += 32) + { + do_test (&json_ctx, 0, i, i + 1, 23); + do_test (&json_ctx, 0, i - 1, i, 23); + } + for (; i < 512; i += 64) + { + do_test (&json_ctx, 0, i, i + 1, 23); + do_test (&json_ctx, 0, i - 1, i, 23); + } + for (; i < 1024; i += 128) + { + do_test (&json_ctx, 0, i, i + 1, 23); + do_test (&json_ctx, 0, i - 1, i, 23); + } + for (; i < 2048; i += 256) + { + do_test (&json_ctx, 0, i, i + 1, 23); + do_test (&json_ctx, 0, i - 1, i, 23); + } + for (; i < 4096; i += 512) + { + do_test (&json_ctx, 0, i, i + 1, 23); + do_test (&json_ctx, 0, i - 1, i, 23); + } json_array_end (&json_ctx); json_attr_object_end (&json_ctx); diff --git a/benchtests/bench-strchr.c b/benchtests/bench-strchr.c index 54640bde7e..aeb882d442 100644 --- a/benchtests/bench-strchr.c +++ b/benchtests/bench-strchr.c @@ -287,8 +287,8 @@ int test_main (void) { json_ctx_t json_ctx; - size_t i; + size_t i, j; test_init (); json_init (&json_ctx, 0, stdout); @@ -367,15 +367,30 @@ test_main (void) do_test (&json_ctx, 0, i, i + 1, 0, BIG_CHAR); } - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.0); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.1); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.25); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.33); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.5); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.66); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.75); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 0.9); - DO_RAND_TEST(&json_ctx, 0, 15, 16, 1.0); + for (i = 16 / sizeof (CHAR); i <= 8192 / sizeof (CHAR); i += i) + { + for (j = 32 / sizeof (CHAR); j <= 320 / sizeof (CHAR); + j += 32 / sizeof (CHAR)) + { + do_test (&json_ctx, 0, i, i + j, 0, MIDDLE_CHAR); + do_test (&json_ctx, 0, i + j, i, 0, MIDDLE_CHAR); + if (i > j) + { + do_test (&json_ctx, 0, i, i - j, 0, MIDDLE_CHAR); + do_test (&json_ctx, 0, i - j, i, 0, MIDDLE_CHAR); + } + } + } + + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.0); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.1); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.25); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.33); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.5); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.66); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.75); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 0.9); + DO_RAND_TEST (&json_ctx, 0, 15, 16, 1.0); json_array_end (&json_ctx); json_attr_object_end (&json_ctx); diff --git a/benchtests/bench-strnlen.c b/benchtests/bench-strnlen.c index 13b46b3f57..82c02eb6ed 100644 --- a/benchtests/bench-strnlen.c +++ b/benchtests/bench-strnlen.c @@ -195,19 +195,19 @@ test_main (void) { for (j = 0; j <= (704 / sizeof (CHAR)); j += (32 / sizeof (CHAR))) { - do_test (&json_ctx, 0, 1 << i, (i + j), BIG_CHAR); do_test (&json_ctx, 0, i + j, i, BIG_CHAR); - - do_test (&json_ctx, 64, 1 << i, (i + j), BIG_CHAR); do_test (&json_ctx, 64, i + j, i, BIG_CHAR); + do_test (&json_ctx, 0, i, i + j, BIG_CHAR); + do_test (&json_ctx, 64, i, i + j, BIG_CHAR); + if (j < i) { - do_test (&json_ctx, 0, 1 << i, i - j, BIG_CHAR); do_test (&json_ctx, 0, i - j, i, BIG_CHAR); - - do_test (&json_ctx, 64, 1 << i, i - j, BIG_CHAR); do_test (&json_ctx, 64, i - j, i, BIG_CHAR); + + do_test (&json_ctx, 0, i, i - j, BIG_CHAR); + do_test (&json_ctx, 64, i, i - j, BIG_CHAR); } } } diff --git a/benchtests/bench-strrchr.c b/benchtests/bench-strrchr.c index 7cd2a15484..3fcf3f281d 100644 --- a/benchtests/bench-strrchr.c +++ b/benchtests/bench-strrchr.c @@ -151,7 +151,7 @@ int test_main (void) { json_ctx_t json_ctx; - size_t i, j; + size_t i, j, k; int seek; test_init (); @@ -173,7 +173,7 @@ test_main (void) for (seek = 0; seek <= 23; seek += 23) { - for (j = 1; j < 32; j += j) + for (j = 1; j <= 256; j = (j * 4)) { for (i = 1; i < 9; ++i) { @@ -197,6 +197,30 @@ test_main (void) do_test (&json_ctx, getpagesize () - i / 2 - 1, i, i + 1, seek, SMALL_CHAR, j); } + + for (i = (16 / sizeof (CHAR)); i <= (288 / sizeof (CHAR)); i += 32) + { + do_test (&json_ctx, 0, i - 16, i, seek, SMALL_CHAR, j); + do_test (&json_ctx, 0, i, i + 16, seek, SMALL_CHAR, j); + } + + for (i = (16 / sizeof (CHAR)); i <= (2048 / sizeof (CHAR)); i += i) + { + for (k = 0; k <= (288 / sizeof (CHAR)); + k += (48 / sizeof (CHAR))) + { + do_test (&json_ctx, 0, k, i, seek, SMALL_CHAR, j); + do_test (&json_ctx, 0, i, i + k, seek, SMALL_CHAR, j); + + if (k < i) + { + do_test (&json_ctx, 0, i - k, i, seek, SMALL_CHAR, j); + do_test (&json_ctx, 0, k, i - k, seek, SMALL_CHAR, j); + do_test (&json_ctx, 0, i, i - k, seek, SMALL_CHAR, j); + } + } + } + if (seek == 0) { break;