Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1942674rdb; Tue, 5 Sep 2023 09:24:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHglGS179AHDVhyU6VexNxNsoS0LYzKV8CbszoZUiLfnDiId2N4zo5F9w6T0CD7BWgfN+4H X-Received: by 2002:a17:906:3045:b0:9a6:426f:7dfd with SMTP id d5-20020a170906304500b009a6426f7dfdmr226702ejd.66.1693931055522; Tue, 05 Sep 2023 09:24:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693931055; cv=none; d=google.com; s=arc-20160816; b=ZoUAdMHO5hc702on+69+b58cES1Wf5u1wTqbxhClbtLHdDU35X1c05FepuOpIiQjYl 9t8wCoHZICgKLsSBJ0bIZ3Z6x7lGRXfH+aMbV7vu35WUxW0UsO3S7ZDyP4bEz3D9UPtk JyeDEKBNyOa+DWQrUw3tSsK/azMfW4m3/095uX1NfGjGuCUd0vESBvnsblsL+NuwhkIK FfaSQilKNieSixtit6WGkkodZrl9Cru8CINik1T0kiMD8iXtShbCscDsXi4sH9rOVX3n 2K/sVo3may1mtytJBnS7ScmuJZe/m7dE9TWD+3JNZzubRSBizH3hUBbYz7dPU6wQQME+ n4ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=puAECqiDJbB9jeRDIyNPM7w1+Ch2B9Tc82Z85RGyYE4=; fh=SJQqTHoPh8FTV37ZPDrYh5G0JmX12oC9vdP9hnYRAcs=; b=RZ30AUxGeyFPJPBCguR8mmsgiMDY1tA5vUF9voLk6BwLneW+outp2bKhO9gntIZ5KL n8lBDmCefTQi8xpC7hNwbQTNTmNAdsQXjzqSqYbqPoHrHLatpmWIzIN3H6ea4LizlIiv Pn1CzTPZ+MNExaIDVhg+zDLAMoX7IfYzPUM5bm2VKvalTduknBm13lZ8m1JOgMRbHuBc e+h52y36SzPD+fdzL51AHOml6hs9SruHpSFeTFRE8cADIQR/btK36dNWtlkyiQnnp3hj 2OMIjkIthxvOWu5FxSZw9GUfVr2ECjGpVTOqsuCkHLAbPVQVa0LYjpNmmIZzvq96pyYS fzwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Z2vMApRV; 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 m21-20020a1709060d9500b0099d9641d45dsi8119185eji.566.2023.09.05.09.24.07; Tue, 05 Sep 2023 09:24:15 -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=20221208 header.b=Z2vMApRV; 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 S1347261AbjICUKc (ORCPT + 12 others); Sun, 3 Sep 2023 16:10:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345287AbjICUKb (ORCPT ); Sun, 3 Sep 2023 16:10:31 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D77DAAD; Sun, 3 Sep 2023 13:10:28 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-68a32506e90so168749b3a.1; Sun, 03 Sep 2023 13:10:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693771828; x=1694376628; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=puAECqiDJbB9jeRDIyNPM7w1+Ch2B9Tc82Z85RGyYE4=; b=Z2vMApRVlWDAbTw6OB1vbSTm2zadWHyxSL92d5faj26UM/lznjGNjo3GyPkavMJQL/ la/B6gipMkfLo8wHcJUHSMOERQ5JqEtzIqMvH5soElQHYUtDoH+if27zOtdfKvpY1Tm3 v13Knx5OCPZfluUU0ZPdreQchDPdmm3wbtZhdDulVuLzDwTZL2uX1DRX/OehyYn7Pi1c AtLgplLclEqU4r/vYXmlU6K6D7Im5dFTAF1KPGVZH0MtokJo66CxG8ZH1Ow7csx1rqkf YPIkt8W0kBKL2YLRZ8XgyRNMw/jEDPuEV24Uf7Gb52TwBCaUc4fRNeVmTh3BxkbzZcFg Pmnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693771828; x=1694376628; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=puAECqiDJbB9jeRDIyNPM7w1+Ch2B9Tc82Z85RGyYE4=; b=hGSJEKYslo7mP5X4iph3FzpO1Q+QJ5a4ybD/dS3gKV9Deiz3w6B/z3g0X9th2FAogh HCh6dUCtV3vqGKdm+kBkoJ6qr0HZ7YFnCbglkQhehE94Sa4iJn4uJBglEyRqMSuuWy8N JNAOxjQvipInm8MxPTz2LbPKayRHx05XWnJN4o0xWEvMxQwZBAsA9gE2ZIniIUbR7hkX CgrzhTUFasb4gnb9uEWd+LJaBe9lxOqS8tPHkKlVCAh4VKHBEEsVxOC4vGfq85SCR3Al Sz9M4UALEua3hOd1SDScCeOBll7zAYXRC+8LP95UiQXSaj551zkS9SoA3UjPhqnwe5ps 0aDg== X-Gm-Message-State: AOJu0YwcmGgS3Sem01KdqrKox7psLRaPHVJfFfjRwN5QuD7i3hceiFQM x9tndcUu2EWCOzBFBrZJXsk= X-Received: by 2002:a05:6a20:7d97:b0:145:3bd9:1377 with SMTP id v23-20020a056a207d9700b001453bd91377mr11035893pzj.5.1693771828186; Sun, 03 Sep 2023 13:10:28 -0700 (PDT) Received: from sivslab-System-Product-Name ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id 15-20020aa7914f000000b0068bc2432ee2sm6028823pfi.51.2023.09.03.13.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 13:10:27 -0700 (PDT) Date: Mon, 4 Sep 2023 04:10:24 +0800 From: Kuan-Wei Chiu To: Roman Mamedov Cc: song@kernel.org, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] md/raid5: eliminate if-statements in cmp_stripe() Message-ID: <20230903201024.GD2692213@sivslab-System-Product-Name> References: <20230903095059.2683850-1-visitorckw@gmail.com> <20230903183058.1301b149@nvm> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230903183058.1301b149@nvm> 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_BLOCKED,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 Sun, Sep 03, 2023 at 06:30:58PM +0500, Roman Mamedov wrote: > On Sun, 3 Sep 2023 17:50:59 +0800 > Kuan-Wei Chiu wrote: > > > Replace the conditional statements in the cmp_stripe() function with a > > branchless version to improve code readability and potentially enhance > > performance. > > The new code will always do two comparisons and a subtraction (3 > instructions in total), whereas the old version could return after just 1 > comparison, or after 2 comparisons. So depending on the data values it is 3x > to 1.5x as much operations performed than before, there unlikely to be any > enhancement of performance. > > Also IMO the previous version is more easily readable. > The reason behind my proposed changes was to eliminate conditional branches in the code. While the original code could occasionally achieve early returns, many compilers, such as x86-64 gcc 13.2 compiling with -O2 flag, still generate branch instructions. Processors typically have deep pipelines, and a branch prediction miss can result in a high penalty. Therefore, even though early return might not be possible, the new branchless version of code could still offer efficiency improvements. > > The new code calculates the result using a subtraction of > > comparison results, making it more concise and avoiding conditional > > branches. This change does not alter the functionality of the code. > > > > Signed-off-by: Kuan-Wei Chiu > > --- > > drivers/md/raid5.c | 6 +----- > > 1 file changed, 1 insertion(+), 5 deletions(-) > > > > diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c > > index 4cb9c608ee19..b14d7ba38f0f 100644 > > --- a/drivers/md/raid5.c > > +++ b/drivers/md/raid5.c > > @@ -1035,11 +1035,7 @@ static int cmp_stripe(void *priv, const struct list_head *a, > > struct r5pending_data, sibling); > > const struct r5pending_data *db = list_entry(b, > > struct r5pending_data, sibling); > > - if (da->sector > db->sector) > > - return 1; > > - if (da->sector < db->sector) > > - return -1; > > - return 0; > > + return (da->sector > db->sector) - (da->sector < db->sector); > > } > > > > static void dispatch_defer_bios(struct r5conf *conf, int target, > > > -- > With respect, > Roman -- Best regards, Kuan-Wei Chiu