From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.bufferbloat.net (Postfix) with ESMTPS id 6C84D3B2A4 for ; Mon, 8 Apr 2024 07:14:10 -0400 (EDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1e0e89faf47so10965545ad.1 for ; Mon, 08 Apr 2024 04:14:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712574849; x=1713179649; darn=lists.bufferbloat.net; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=fXnqY/hiuIzNOaaXqBgq2NQBkWNEl8jGRoP9ENRrDio=; b=X96iC9R2TXcFKU676lK6gEH5dXJDVZoOw4SMXmRspcvLPNIB9NM4o8AMVq+WD0LTyr b8UHKm5bHsY34Nwueqi+BVUwVWn/zalfpPMsf6YRHrtCgs5Mem3Cgpks3E1V/L/JmgV/ lwpk3tiLFgoY3ewzO0l4OYkg+Yz7lkgvSkxuXxLJrSzvEDbOjEGSs2krOXvBJvvxxG7U rrXs/yxJKEyJ/z/YGnpcqKvaPKf3Dz9PjmBbOvtmnyg9xfmbxRvpxrADmV3sr7lUj25h P00b1uJmH7yaiknricEY9FLj1BG/qINdLebqBHsYNzrBdd6M0h2F1c9CFhRdnSs7zhCS FCWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712574849; x=1713179649; h=in-reply-to:content-transfer-encoding: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=fXnqY/hiuIzNOaaXqBgq2NQBkWNEl8jGRoP9ENRrDio=; b=WfbdGy9O28xE1Y7/MGBGHyR4vYtdR9W96JZ7v1xUMoYWQI+vor6wqRUhEUA8E3yMNU +TOYoaK0p1sI68gVxj24r1ZNzVvx+JOn1BJLhDyRLCyqTXDfXuKbs28BBG2i8z9e9/uu tkZl0JSpQLHz4pGc6uhrRVdkYMioFNLeeQoI1W+MNkcyEKu/hjPtHBKxcgMV2/FFbZSQ kAQKCPNeG8t7ziEGh2HU28WZY3zQ+Y6FNkMIyX91Uzk985i1TE5sfuPA2wIYz0HsQx9J g6S+l+nSgEKhsp4SmW6Fp+e4hA09XyqecL/EYw4fpHrWcG7JeT6fSRtFn72Ujmp/+HY8 KzLw== X-Forwarded-Encrypted: i=1; AJvYcCVgPj7aG4tb+8G6Jg5VhghBWDnM40B9vtSbemUCesokkRIdbwyfmKlsF/+VwgWa2uRtHsuOy/c/pCIcrNfYHOlBIwggOs+R/+P2Jw== X-Gm-Message-State: AOJu0YyUN8l3kEKCFI+KG1yihVINX6MWNki/cl3+yMHPkB3CXYnb3Iz0 bBXsbeaTQJwTZiHUTk6UOK6TYJ3YfI7M6BXucbKKSYOXqTVGUYLA X-Google-Smtp-Source: AGHT+IH+xMcVjhrAfZdMRUUOacUFXNyLoJMz6JNERAY1hW08XRtfGnHXhCDa6A1nFCgE/jjg0ajKkg== X-Received: by 2002:a17:902:ce87:b0:1db:ce31:96b1 with SMTP id f7-20020a170902ce8700b001dbce3196b1mr10607491plg.6.1712574849390; Mon, 08 Apr 2024 04:14:09 -0700 (PDT) Received: from visitorckw-System-Product-Name ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id z5-20020a170903018500b001e45572a253sm1047027plg.14.2024.04.08.04.14.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 04:14:08 -0700 (PDT) From: Kuan-Wei Chiu To: Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= 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 Message-ID: References: <20240406235532.613696-1-visitorckw@gmail.com> <87frvxgnmr.fsf@toke.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87frvxgnmr.fsf@toke.dk> X-Mailman-Approved-At: Mon, 29 Apr 2024 18:18:51 -0400 Subject: Re: [Cake] [PATCH net-next] net: sched: cake: Optimize number of calls to cake_heapify() X-BeenThere: cake@lists.bufferbloat.net X-Mailman-Version: 2.1.20 Precedence: list List-Id: Cake - FQ_codel the next generation List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Mon, 08 Apr 2024 11:14:10 -0000 X-Original-Date: Mon, 8 Apr 2024 19:14:05 +0800 X-List-Received-Date: Mon, 08 Apr 2024 11:14:10 -0000 On Sun, Apr 07, 2024 at 06:10:04PM +0200, Toke Høiland-Jørgensen wrote: > Kuan-Wei Chiu writes: > > > 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. > > Please add an explanation for why this change is correct, and why it is > beneficial. "Improve" and "unnecessary" is way too implicit. > > 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. Thanks, Kuan-Wei