From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753046AbcHZUBP (ORCPT ); Fri, 26 Aug 2016 16:01:15 -0400 Received: from mail-lf0-f68.google.com ([209.85.215.68]:33737 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751223AbcHZUBO (ORCPT ); Fri, 26 Aug 2016 16:01:14 -0400 Date: Fri, 26 Aug 2016 23:01:20 +0300 From: Alexey Dobriyan To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] smaller strlen() Message-ID: <20160826200120.GA1852@p183.telecom.by> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org gcc prefers "*s++" style code for some reason, doesn't unroll loop condition check once. Kernel strings are small but they aren't of 0 length, so that additional branch was almost never taken. $ ./scripts/bloat-o-meter ../vmlinux-000 ../obj/vmlinux strlen 30 26 -4 strlcpy 71 64 -7 strlcat 120 99 -21 strlcpy() and strlcat() are collateral damage :^) Signed-off-by: Alexey Dobriyan --- lib/string.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/lib/string.c +++ b/lib/string.c @@ -476,11 +476,11 @@ EXPORT_SYMBOL(strim); */ size_t strlen(const char *s) { - const char *sc; + const char *s0 = s; - for (sc = s; *sc != '\0'; ++sc) + while (*s++) /* nothing */; - return sc - s; + return s - s0 - 1; } EXPORT_SYMBOL(strlen); #endif