Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752654AbbF1QoN (ORCPT ); Sun, 28 Jun 2015 12:44:13 -0400 Received: from mail-wg0-f48.google.com ([74.125.82.48]:34197 "EHLO mail-wg0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752368AbbF1QoH (ORCPT ); Sun, 28 Jun 2015 12:44:07 -0400 Date: Sun, 28 Jun 2015 19:44:03 +0300 From: Alexey Dobriyan To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux@rasmusvillemoes.dk Subject: [PATCH] un-improve strrchr() Message-ID: <20150628164403.GA7169@p183.telecom.by> References: <20150628163252.GA1991@p183.telecom.by> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150628163252.GA1991@p183.telecom.by> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1352 Lines: 46 Commit 8da53d4595a53fb9a3380dd4d1c9bc24c7c9aab8 ("lib/string.c: improve strrchr()") changed strrchr() implementation from "rewind to the end and search backwards" to "search forward" optimizing for characher not found case. However, common case is exactly the opposite: string is absolute pathname, c is '/' always to be found. Previous code did 1 branch per character + 1 branch for every character in the last path component. Current code does 2 branches per characher regardless. Patch reverts to previous implementation (sans fixed coding style). Signed-off-by: Alexey Dobriyan --- Cc linux-kernel lib/string.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) --- a/lib/string.c +++ b/lib/string.c @@ -313,12 +313,13 @@ EXPORT_SYMBOL(strchrnul); */ char *strrchr(const char *s, int c) { - const char *last = NULL; + const char *p = s + strlen(s); + do { - if (*s == (char)c) - last = s; - } while (*s++); - return (char *)last; + if (*p == (char)c) + return (char *)p; + } while (--p >= s); + return NULL; } EXPORT_SYMBOL(strrchr); #endif -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/