Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1324764lqe; Mon, 8 Apr 2024 06:03:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUH9F55dlAfle0paTYucIfdzgUSkb1DAiLXaj91r1O6xVe+M6MJa9fHjZKDFDUstb7h5iRbbXdiK84YpUvvO9UlYt0YngAt7Atk9pGeag== X-Google-Smtp-Source: AGHT+IFo3pC5J+mepP7oW50xVKq8chj5vhikAB5+uZnb4IVzDbrQbWd4dqpeeQ85+e/9uKG8sPnE X-Received: by 2002:a17:902:f541:b0:1e4:2a61:5784 with SMTP id h1-20020a170902f54100b001e42a615784mr2149268plf.47.1712581423876; Mon, 08 Apr 2024 06:03:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712581423; cv=pass; d=google.com; s=arc-20160816; b=QnJUrvR+j5ouULkHNV3IRxRsbJlZH9YuGvhu8MCXK2oBlSldTU/a9fAo1TxTto/Ynk yHIr7dxbJJqH6tiFLHN2q++AhkyPja2ywbd5Im4C50x08IgmBDG5YMag2crx/clxF880 ky2u+QoVK5kBH+j9oLm+nh0oLdVSl5qZD9dnGhC/H0++7k7cfUNZsGcNkasFnjOUVuD0 1AbOvpOfHzguCSR8FmexevfEU4wPvmCjj4JwiEjGf03n8tm7joUsW5QjOQOxjJQXVPF8 gIbiAgWLLDHecg0KpeXkHOMqn513jJTAqVAQD2/bFUMLZ6bEnHldFAKCn458JxQMK3jB lPyQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:from; bh=FkXLzyxqjZzUX3ydZJqpehjHFFdMSDLx+4h5DWHTIiY=; fh=vvxEOuI/bAyux/ZRNz2B/z4PRNCqXzvzW1abpKJv0co=; b=e7HKkkY5cC6nsQyMA6EEYswX4x9bBLlG+7eL7/1QuyFdjxmGPQC5UkV4vy7ytTh0Nb 1ffbU+l7AGzK4DLk2rB1Dg17mHJ9OIl2g5l0vD3RfILcRWWOBOreSAiGdLs4XUbJ7QAy vNubItFXjeQG5/jjGRnyIrlwBgKqQQ9Aq5I8mCpj5FizN/oLkEuOTHS7z/Wg7xcqG9Yg jcwTPci0P8i5ButLU+FaOM16qlgLxeUvgksNeB50KugnB0ssMk+Lacan1f9EMmTQtAPI ZhlUNWf++OHU4IT+Ys65cXFqc0qIoRgOY0ppUKNtoN+mNqONrg8ZjznmNXXxiLhVbxKJ ANBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@toke.dk header.s=20161023 header.b=wNVB4yAr; arc=pass (i=1 spf=pass spfdomain=toke.dk dkim=pass dkdomain=toke.dk dmarc=pass fromdomain=toke.dk); spf=pass (google.com: domain of linux-kernel+bounces-135383-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135383-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=toke.dk Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id b2-20020a170902ed0200b001e0af97a0bbsi6240118pld.433.2024.04.08.06.03.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 06:03:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135383-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@toke.dk header.s=20161023 header.b=wNVB4yAr; arc=pass (i=1 spf=pass spfdomain=toke.dk dkim=pass dkdomain=toke.dk dmarc=pass fromdomain=toke.dk); spf=pass (google.com: domain of linux-kernel+bounces-135383-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135383-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=toke.dk Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BD57D28371A for ; Mon, 8 Apr 2024 13:02:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 17BA17E575; Mon, 8 Apr 2024 13:00:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toke.dk header.i=@toke.dk header.b="wNVB4yAr" Received: from mail.toke.dk (mail.toke.dk [45.145.95.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85B487172F; Mon, 8 Apr 2024 13:00:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.145.95.4 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712581238; cv=none; b=fOUO8BzCb2bR5hBQNsAhuHc/vdVIsqCejozfJuBQk144g2DU8dpS5ksw31YLv3QW0/Mjz+TrrLiumppESbd8Zef44SsxOZfiUyLxw/tUuODF2o5qf2v1qLTjpRqiiU/ddG/cYuh/QOmnIkJPFhN0vEOll5chq+3xmgbPi7Hdxqo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712581238; c=relaxed/simple; bh=FkXLzyxqjZzUX3ydZJqpehjHFFdMSDLx+4h5DWHTIiY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=RQcOdFqJH2qcxACnm662f4KxwE+ZttMtBUUrTFTHSr+OIFiptpWG/YoVHvjJ2/RshG/RyFRAb8sLzq0AoAl0nKFBLnEYKoG7yCS+a9YWwtd8A9346uN2KarfZwgz52H0Xfg55mHA0wbv655JWgybAXdonvp5hf7YDItKUcaFvpk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=toke.dk; spf=pass smtp.mailfrom=toke.dk; dkim=pass (2048-bit key) header.d=toke.dk header.i=@toke.dk header.b=wNVB4yAr; arc=none smtp.client-ip=45.145.95.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=toke.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=toke.dk From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=toke.dk; s=20161023; t=1712581229; bh=FkXLzyxqjZzUX3ydZJqpehjHFFdMSDLx+4h5DWHTIiY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=wNVB4yArKpa2PUdlKiLdUKq+m0PD/rqSv51mIeW6FY3daUZxgK8MjoX3api0EigvJ d5z8okWEZnkeGojomYZA7aj65SsZa2dOSApaNxn8tFnlvRvlc/7hgZwq5qAZFZp7pL 1iaq0RtpGeDFMHpwyKAhdlW4p+XbPu3Kc3DuG0drdQvu4h3ol/L5ja8gwcd0Hitm1K T2NtVewABkVOGdCDoZbjpCZVKXITP4DD8t2PTpdUAby5d4tjjTeE6dNCaRZQfI7Ick FgMlzoZJSH9a24rzBvXxDStS9jsl0JRPl77FgvewUknuJ5doB+aFCH1YU8LucGi4yv LXUTQZ5bAez+A== To: Kuan-Wei Chiu Cc: jhs@mojatatu.com, xiyou.wangcong@gmail.com, jiri@resnulli.us, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, jserv@ccns.ncku.edu.tw, cake@lists.bufferbloat.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net-next] net: sched: cake: Optimize number of calls to cake_heapify() In-Reply-To: References: <20240406235532.613696-1-visitorckw@gmail.com> <87frvxgnmr.fsf@toke.dk> Date: Mon, 08 Apr 2024 15:00:29 +0200 X-Clacks-Overhead: GNU Terry Pratchett Message-ID: <87cyr0ggb6.fsf@toke.dk> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Kuan-Wei Chiu writes: > On Sun, Apr 07, 2024 at 06:10:04PM +0200, Toke H=C3=B8iland-J=C3=B8rgense= n wrote: >> Kuan-Wei Chiu writes: >>=20 >> > Improve the max-heap construction process by reducing unnecessary >> > heapify operations. Specifically, adjust the starting condition from >> > n / 2 to n / 2 - 1 in the loop that iterates over all non-leaf >> > elements. >>=20 >> Please add an explanation for why this change is correct, and why it is >> beneficial. "Improve" and "unnecessary" is way too implicit. >>=20 >> pw-bot: cr > > For correctness: > To build a heap, we need to perform heapify operations on all non-leaf > nodes, so we need to find the index of the first non-leaf node. In a > heap, the index of node i, the left child's index is 2 * i + 1, and the > right child's index is 2 * i + 2. The left and right children of node > CAKE_MAX_TINS * CAKE_QUEUES / 2 are at indexes CAKE_MAX_TINS * > CAKE_QUEUES + 1 and CAKE_MAX_TINS * CAKE_QUEUES + 2, respectively. Both > children's indexes are beyond the range of the heap, indicating that > CAKE_MAX_TINS * CAKE_QUEUES / 2 is a leaf node. The left child of node > CAKE_MAX_TINS * CAKE_QUEUES / 2 - 1 is at index CAKE_MAX_TINS * > CAKE_QUEUES - 1, and the right child is at index CAKE_MAX_TINS * > CAKE_QUEUES. Therefore, we know the left child exists, but the right > child does not. Since it's not a leaf node, the loop should start from > it. > > For benefit: > We can reduce 2 function calls (one for cake_heapify() and another for > cake_heap_get_backlog()) and decrease 5 branch condition evaluations > (one for iterating through all non-leaf nodes, one inside the while > loop of cake_heapify(), and three more inside the while loop with if > conditions). The only added operation is an extra subtraction. > > If you're satisfied with the explanation above, I can attempt to > rewrite the commit message and send the v2 patch. Yes, sounds reasonable. Did you measure any real-world performance benefit, or is this purely a theoretical optimisation? Either way, please indicate this in the updated patch description. -Toke