From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756954AbbFPWQx (ORCPT ); Tue, 16 Jun 2015 18:16:53 -0400 Received: from smtprelay0045.hostedemail.com ([216.40.44.45]:47755 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754473AbbFPWQr (ORCPT ); Tue, 16 Jun 2015 18:16:47 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::,RULES_HIT:41:355:379:541:599:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1539:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:2828:2895:3138:3139:3140:3141:3142:3352:3622:3865:3866:3867:3868:3874:4321:5007:6261:7903:10004:10400:10848:11232:11658:11914:12517:12519:12740:13069:13255:13311:13357:13972:21080,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0 X-HE-Tag: toys73_dff882b0a0a X-Filterd-Recvd-Size: 1478 Message-ID: <1434493005.2689.12.camel@perches.com> Subject: Re: [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions From: Joe Perches To: Orestes Leal Rodriguez Cc: linux-kernel@vger.kernel.org, mihai.dontu@gmail.com Date: Tue, 16 Jun 2015 15:16:45 -0700 In-Reply-To: <55806FE8.9030302@gmail.com> References: <55806FE8.9030302@gmail.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.12.11-0ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2015-06-16 at 13:50 -0500, Orestes Leal Rodriguez wrote: > Very small update to strlen and strnlen that now use less cpu > instructions by using a counter to avoid the memory addresses > substraction to find the length of the string. [] > @@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen); > */ > size_t strnlen(const char *s, size_t count) > { > - const char *sc; > + size_t sz = 0; > > - for (sc = s; count-- && *sc != '\0'; ++sc) > - /* nothing */; > - return sc - s; > + for (; count-- && *s++ != '\0'; sz++) > + /* empty */; > + return sz; That's one subtraction at end-of-string vs a register increment for each non-zero byte. smaller isn't worth slower.