Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp8076317rwi; Tue, 25 Oct 2022 02:04:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM43V2DVKxJIlETzKp9TgDNSFCM8Lalbr25pj5tvgSOXQD5EFuyAp4qj6YFyzq9xezgkhpYn X-Received: by 2002:a05:6402:2546:b0:45d:8bff:7afc with SMTP id l6-20020a056402254600b0045d8bff7afcmr34072780edb.302.1666688645276; Tue, 25 Oct 2022 02:04:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666688645; cv=none; d=google.com; s=arc-20160816; b=XcCIxj61baz2k46FJAO9TSBl7iEAZF8fGmBJaR7GDpLQ26KwYGZXD5wseqJ63wSy6P YTQFOdf7vVP6m0a1M3xoNHonoorqbYRLvtve53NpMwl6akt5TQ8llIUhuc/ULql4/0c/ A767BOnZhPTBLSVOw7KxyyBw5idxBrfpmdAG7g9lfSL6PPfdnet7Ea2vhui+niE1Qh67 BCoXm5krLiLVuRqoubUFd9rb15jIWhc4LjDsSVffXl+icvOY22BZXX3Gsbu+bSW1063p cNMWABDgqDluD4j0k5ttw2PUN/mT2b6cbExoGbQi/PlY7Gj4eCY7wIUfYB2nlVyhdLbx P2sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=JZfId2D7R3IsHmX8oa6uBuFcosPjWoQjftQaCLnfakY=; b=q9WkhC7hcbygPF0eApvq1Cre4YJ70KAzbOrZTv6Ev9WAx7vK2Dswq9yxlo5nO6QcoG mzvofzVlNZm9AlBW0lKVOX8EVgXKXQC00zR26+rJOzjxgbURipYi33WoC/r77w4GZd7c DRUxOV7//WtBSUiDNGnaatFloX7TGd3Rdmm4dMjCxW4Yyf5fgsGizloRkC8tg4VE4GSp Nc2uZ0+QZg6YXFn8w5AjytGaM/B8D0J2dO53csfC4S1C+ZBD9mGG+5DQbGYaocnnfrFi zg04mL+nkQG6bhVpM6463hqLBUtR2ewbe4bs3Qo8b6Vdkc0gfjI7gXTzUkm/Y2+BOU2M 1big== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mXVFmSyW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc10-20020a170907168a00b007a7d37e4681si2503754ejc.845.2022.10.25.02.03.39; Tue, 25 Oct 2022 02:04:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mXVFmSyW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231625AbiJYIBQ (ORCPT + 99 others); Tue, 25 Oct 2022 04:01:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230078AbiJYIBO (ORCPT ); Tue, 25 Oct 2022 04:01:14 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75C344456A for ; Tue, 25 Oct 2022 01:01:13 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id h10so8049539qvq.7 for ; Tue, 25 Oct 2022 01:01:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JZfId2D7R3IsHmX8oa6uBuFcosPjWoQjftQaCLnfakY=; b=mXVFmSyWnNqvqnRUkMT88INCUvTR2N5j51gVt/4VJ3d3/YqrM9yGzsjgkukLkhFYvS zgipHr2s2TtUM0xRCPU+lymLZstHJ1V2Ge3rbY9u9fs1v9CTk0uWmB+9C3cWFqV4eDGl 3sLAfZLRDSjGvxhNmwUMY3TeQXECZgRmefZ/A5Si5B6YAWfSLoJJyliEVgE9lONMuMEk eBC5Fo3d17dDOeSxrq0fQwDYYPakCu+/6Cv+cDneEhfIWFffsOE43hi3aQtAtJktr9KJ klezc3Dkk9oC+WCJo3c2M1eCCA199dRGaEk/0NvsIME7CznP8mkzY2EZsLWYDcgpz4X5 OXVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JZfId2D7R3IsHmX8oa6uBuFcosPjWoQjftQaCLnfakY=; b=A/i+Nh3EEI2lQPvaGQ0C38L/G8Hq6s/qAHFwMSYf9bmHiSEzVbDFxxv5xcvUIeOy3Z imz8IG7XXQpf+gkM+g0d5MYmHzbuUaYBKXLJ5gUE0mNszOyRYG+1QDaP9+4Ky2DL0Bku IPsuBVmFVFrp5jP5iGVXOXOrKSJ1+K9KuSo9d1EHkOqSaa2Z0aq0Bano+QzcF9Zsxh9t zIfvcMkSzMZ+U3HdItdYKQifk5ScyNDz4bAn2+bDJ4igOddURh+mPPMRj9TnsB8Q1cTQ tj+sj83nttIO1867IIYa/KXBS69o6l6HhS7zHbWAvRYfVmxnhvuGh6g+Ur3xR6ByckKl WLzA== X-Gm-Message-State: ACrzQf0m0KdsXMUBLZmwW0s4p0irXlFRpfbEY3cHkiHAGB7s4bC2sYkA cu12PksPi6qM4UC/0oskHoeO0+spSLLxFA223POhrYxEHFI= X-Received: by 2002:a05:6214:29ee:b0:4b1:c1d2:6635 with SMTP id jv14-20020a05621429ee00b004b1c1d26635mr31224127qvb.82.1666684872509; Tue, 25 Oct 2022 01:01:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andy Shevchenko Date: Tue, 25 Oct 2022 11:00:36 +0300 Message-ID: Subject: Re: [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if chars match To: Nathan Moinvaziri Cc: "linux-kernel@vger.kernel.org" , Andy Shevchenko Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 25, 2022 at 4:46 AM Nathan Moinvaziri wrote: > > From fcb0159ee74908f92adc34143657d8ca56e9a811 Mon Sep 17 00:00:00 2001 > From: Nathan Moinvaziri > Date: Mon, 24 Oct 2022 16:37:59 -0700 > Subject: [PATCH] lib/string.c: Improve strcasecmp speed by not lowering if > chars match. Why is the above in the commit message? > With strings where many characters match exactly each character is needlessly > converted to lowercase before comparing. This patch improves the comparison > by only converting to lowercase after checking that the characters don't match. > > The more characters that match exactly the better performance we expect versus > the old function. > > When running tests using Quick Benchmark with two matching 256 character > strings these changes result in anywhere between ~6-9x speed improvement. > > * We use unsigned char instead of int similar to strncasecmp. > * We only subtract c1 - c2 when they are not equal. Nobody can take it. Please, read Submitting Patches documentation and fix your contribution accordingly. ... > do { > - c1 = tolower(*s1++); > - c2 = tolower(*s2++); > - } while (c1 == c2 && c1 != 0); > - return c1 - c2; > + c1 = *s1++; > + c2 = *s2++; > + if (c1 != c2) { > + c1 = tolower(c1); > + c2 = tolower(c2); > + if (c1 != c2) > + return (int)c1 - (int)c2; > + } > + } while (c1 != 0); You tell us that this is more preformant, but have not provided the numbers. Can we see those, please? Note, that you basically trash CPU cache lines when characters are not equal, and before doing that you have a branching. I'm unsure that your way is more performant than the original one. -- With Best Regards, Andy Shevchenko