From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by sourceware.org (Postfix) with ESMTPS id 9D4F03858CD1 for ; Wed, 20 Dec 2023 22:24:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D4F03858CD1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=SystematicSW.ab.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=SystematicSW.ab.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9D4F03858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=216.40.44.17 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703111056; cv=none; b=FbPWFTKKpIASYr/wMAE1haPHyNYzdBv4eYmsXvqKI802nMZgiDD0xx7RQoAUXQ6lcyjwl1oARYmr0C0d9tTGR/gDelzYVEMdmqLnZWRHIqCIs/mWD8bXNv61xOlUDIno5n+MiJ3dTAnsyeVo0um5mvYQblqE0c4p94RRLVbAOb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703111056; c=relaxed/simple; bh=99LWQ7VOTFGsw+4DETRBVXGaFVUDZcfZ+Va7HruC794=; h=Message-ID:Date:MIME-Version:Subject:To:From; b=QXG1w6Vks/s9sDJqb8bqjyykfd6cZ2NOWre1asAuw0Ic++nssX4O391tmDPuy85p+SPTQzAmuznQV+ZRo8Y6LZU6EZF5TGE68ZN7inpsyb+JcpDRuAqXM4vkAhMtDHRYNoJ1wvFcBilg7rVpF+A8klIlpyGiKJL9nG8+ItIdX/o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from omf02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 337E340547 for ; Wed, 20 Dec 2023 22:24:14 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: Brian.Inglis@SystematicSW.ab.ca) by omf02.hostedemail.com (Postfix) with ESMTPA id B26F68000F for ; Wed, 20 Dec 2023 22:24:11 +0000 (UTC) Message-ID: <52a680da-61ad-486d-aaa4-d2800320791f@SystematicSW.ab.ca> Date: Wed, 20 Dec 2023 15:24:10 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Reply-To: newlib@sourceware.org Subject: Re: [PATCH] newlib: libc: Improved the readability of strcspn with minor optimization Content-Language: en-CA To: newlib@sourceware.org References: <20231215083101.5643-1-zengxiao@eswincomputing.com> <70b09ef7-fe4a-4669-ad27-2bd6c8bdca61@foss.st.com> <202312161730500900107@eswincomputing.com> From: Brian Inglis Organization: Systematic Software In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: B26F68000F X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,BODY_8BITS,GIT_PATCH_0,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Rspamd-Server: rspamout02 X-Stat-Signature: t78i6iset5rbi56zymzq1dmfdp5nekt6 X-Session-Marker: 427269616E2E496E676C69734053797374656D6174696353572E61622E6361 X-Session-ID: U2FsdGVkX1/AeS9trbkj7+Tvi2rHOfnNkhkXBCOLrPk= X-HE-Tag: 1703111051-291801 X-HE-Meta: U2FsdGVkX19wCsFCSP4pQ7eHX4fvCInkgwgege0jqbmSiUEZZeTXxkwKyODxDJqERPUBbL9msFWaRFgKg1qE2wEDZu+s9Pd2KCcNLq3p8NQhBDjcKOzY8qV03D7cW7gSYA8hptGhODfSZ5E7cqWW1vPlGMuved442VeA2DDRnQGZZPk6MUqq8aDGC8wymsS/dXpJWEs+hmTrhhhyWyTSsj5heQfjTJNQWsFU9ZrPG9bchTrDT7wf7sPKNmzav9qVX7GXrDvIweUsjvzzHz97OsmCYZzx6AruXlPE1HhddN8rJ3NQUCyxL9zI1UzIKwczs0pToEq1MEmdOdccUCdHmVFJFBfFZSFDugxPsG7p3Mw1rC5CPo0YvGolAqtpVLr7 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-12-19 21:24, Jeff Johnston wrote: > Patch merged to master. > On Sat, Dec 16, 2023 at 4:31 AM Xiao Zeng wrote: > 2023-12-15 18:28  Torbjorn SVENSSON wrote: >> On 2023-12-15 09:31, Xiao Zeng wrote: >>> Signed-off-by: Xiao Zeng > >>> --- >>> newlib/libc/string/strcspn.c | 6 ++---- >>> 1 file changed, 2 insertions(+), 4 deletions(-) >>> >>> diff --git a/newlib/libc/string/strcspn.c b/newlib/libc/string/strcspn.c >>> index abaa93ad6..8ac0bf10c 100644 >>> --- a/newlib/libc/string/strcspn.c >>> +++ b/newlib/libc/string/strcspn.c >>> @@ -37,12 +37,10 @@ strcspn (const char *s1, > >>         for (c = s2; *c; c++) > >>   { > >>     if (*s1 == *c) > >> -        break; > >> +        goto end; > >>   } > >> -      if (*c) > >> -    break; > >>         s1++; > >>       } > >> - > >> +end: > >>     return s1 - s; > >>   } >> Just looking at this small snippet of code, I would say that the previous >> code and your suggestion won't do the same thing. >> >> Do you have unit tests that confirm that the behavior is identical with the >> current implementation and your suggested change? >> >> When I run your suggestion, I get return value 0, but with the current >> implementation it's 3 for this call: strspn("129th", "1234567890"). > After applying this patch, provide a comparison of assembly code under the > risc-v architecture, with default compilation parameters used in both of > them: These "micro-optimizations" improve code generation by a few instructions on a single (RISC-V) target at a single optimization level of a single compiler and version, but what is the cost in execution time and the cache imoact? Using gotos throw away potential optimizations in modern compilers, where goto-less code may have control and/or data flow optimized, with branches altered or eliminated, depending on target instruction sets and compiler supported optimizations selected and implemented. For example, in these small functions with few branches, conditional execution instructions could be generated, eliminating branches, cache, and lookaside buffer impacts, possibly allowing inlining. Who knows what impacts this has on all of the other targets, compilers, versions, and optimization levels? Should we even consider making these kinds of non-bug-fix minor changes to non-target specific sources, unless there are algorithm changes with demonstrated benefits across multiple targets, compilers, versions, and optimization levels? -- Take care. Thanks, Brian Inglis Calgary, Alberta, Canada La perfection est atteinte Perfection is achieved non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add mais lorsqu'il n'y a plus rien à retirer but when there is no more to cut -- Antoine de Saint-Exupéry