Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1771652rdb; Sat, 2 Dec 2023 08:37:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IGu6VHu9TMb7h3CEnGS/KyK7q10TC+cqT+V5HR556D7i2sMHpWUKWdUbOX2i+xIo5U4gaSJ X-Received: by 2002:a05:6a00:810:b0:6cd:d53c:f5ea with SMTP id m16-20020a056a00081000b006cdd53cf5eamr1891662pfk.6.1701535058292; Sat, 02 Dec 2023 08:37:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701535058; cv=none; d=google.com; s=arc-20160816; b=dcN4iN5ELE5yNOVCW3CJnLzGhlbpsyk2oCiuEOnT4/RJd17dk3BO+4F9vy8ve2sYof fIo3vZMwWrLdEeAmf8hKZtuSENCemFwspfBxnl+Kz9REKlkwDPef1FA/jW6G/KS0bSBO EZmRiHW14adprqMTrQ1FiyqsLaHoE1SNmcfrQP5vRjaqs10PA49GgQAPxvsa+aZ//Cdc NuXmq4aXYy9XQk1vHObGQIQBQhHEKH2XG/QzFnYzTWu2bTUNu5WIBCQJXmaX0onFdsrD ZipxPyFhowCV9YwVRSFXpLmNSm4eUtv+ah7NnWSwDhGzR6T3IXe6VI5Jmi1GUR0LRoGS dEDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=r/5fy0dH2xzqcER0suoHkQQxma1xoWVm6Ksyzy3Xy6A=; fh=rbZRYXxNCmF2FJWxPJCzLykLwSU9bDOS8Y92Gk7eT9I=; b=zgpoAw9c/WfjuELwHg99UDCLhZdbO3N4YcijQBv6+nw/m2sTDv8C34J6LvufHRlfyn FXLTfRh4lh7EkBfZjlj66+SclENcCaIk7gw0d45888JzSrmIbsGMzLRxM+3g6dN4wRdS xnMjka35qSMp9qyirxbsLbLrRbFl1da6IhyMZTlytWq1VrspehXoJmYfYa5O2Tgh3t8f T1/rwPa1YZ998Vhg17CksCzjs6/2B5GdBr/PDFJ4y76sc0EiIdfJurCZJeTU54Ahlw4b zsfEtxBi3lzabTCR4QwXFF8o1qFKzfOtaTNZRDMUyXuXlO832IkHR9jDturg3IF2urnp Y2cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ecfQc3Lw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id v2-20020a655c42000000b005b7ce261d0dsi4869944pgr.402.2023.12.02.08.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 08:37:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ecfQc3Lw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 188E3805D5ED; Sat, 2 Dec 2023 08:37:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229852AbjLBQhS (ORCPT + 99 others); Sat, 2 Dec 2023 11:37:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229671AbjLBQhR (ORCPT ); Sat, 2 Dec 2023 11:37:17 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42DC2114 for ; Sat, 2 Dec 2023 08:37:24 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6cdf3e99621so510591b3a.1 for ; Sat, 02 Dec 2023 08:37:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701535044; x=1702139844; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=r/5fy0dH2xzqcER0suoHkQQxma1xoWVm6Ksyzy3Xy6A=; b=ecfQc3LwBlUzvq3P/+j0euPU5HVDjuPKEaKwf7TNTggezOnaCRom40Qu6l2SDUA7vN WKVapiW8WltxwFX1zPT1yFJh0SdyvbMzbAnsJVPV9awBilvw6a3hZKhdX5a+dct4kJ0N jOxac69bFNVZexKsKNgQezvXDe/Mq9ebwEc2rp0IIOLCZjaczjn2iiuHGGfcyvW/K1xT lV7z9mz0qWidkzycR7coiLPOzB7bCbFY6TrVnkPhAvwLHWxMaQ/xWw2ra4hEdaLcacXq LH16CFFBTEHJ2I978J55tZ/j9T/fSfEcTwwDtP8pIkfH6ZfViaD46nCucu94Xwdh0BfK fj1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701535044; x=1702139844; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=r/5fy0dH2xzqcER0suoHkQQxma1xoWVm6Ksyzy3Xy6A=; b=sa7UQzSo0oSzyh07UFQ+K5XeAXoSJOTwpfk9ax281LwfKlDS+BFHJzrJqOttteXR9z dQFh/3qC7ClKrkDkmY4bue1/EyXX0iXuiwaN1NZfBEf5n5SeBHRCluISZm9zyL1wxAQv PEkgfarY8Tjwir4loH1/wgsNcp+7XXlZ7IfXWZpfDE89kscItzTYp/UFPN2XO84gnV9N 97wYCz5vnkoyTBrVKa7A6cn4uBgPBVtXsVBB88PBx5r2IH3dv/wFij2eAr9ep13j1jvR I9AIZvJhbTXF5u0T9Cs/anmF8bwAYzCdelmkIVQn9p7n0qYXxgDtjgihfJ8IYo+Q6PwD s2sA== X-Gm-Message-State: AOJu0YyKKJRDMlgbf8q8JZ71QYx+d0Hy0CpzN8sPyBCOHc/K3boIaUYJ uzdOh3TuzGJtfr9MebnFpoU= X-Received: by 2002:a17:902:e80b:b0:1cf:a718:384 with SMTP id u11-20020a170902e80b00b001cfa7180384mr29734467plg.6.1701535043733; Sat, 02 Dec 2023 08:37:23 -0800 (PST) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id jc15-20020a17090325cf00b001d07d88a71esm812533plb.73.2023.12.02.08.37.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 08:37:23 -0800 (PST) From: Kuan-Wei Chiu To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, lkml@sdf.org, Kuan-Wei Chiu Subject: [PATCH] lib/sort: Optimize number of calls to comparison function Date: Sun, 3 Dec 2023 00:37:17 +0800 Message-Id: <20231202163717.687578-1-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 02 Dec 2023 08:37:36 -0800 (PST) The current implementation continues the loop when the comparison function returns a non-negative value, which includes the case when it returns 0. However, in scenarios where the comparison function returns 0, further comparisons are unnecessary. By making this adjustment, we can potentially reduce the number of comparisons by approximately 50% in extreme cases where all elements in the array are equal, and the array size is sufficiently large. Signed-off-by: Kuan-Wei Chiu --- lib/sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sort.c b/lib/sort.c index b399bf10d675..1e98a62bb2f3 100644 --- a/lib/sort.c +++ b/lib/sort.c @@ -267,7 +267,7 @@ void sort_r(void *base, size_t num, size_t size, b = c; /* Now backtrack from "b" to the correct location for "a" */ - while (b != a && do_cmp(base + a, base + b, cmp_func, priv) >= 0) + while (b != a && do_cmp(base + a, base + b, cmp_func, priv) > 0) b = parent(b, lsbit, size); c = b; /* Where "a" belongs */ while (b != a) { /* Shift it into place */ -- 2.25.1