Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp407457rdb; Mon, 22 Jan 2024 07:52:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHmz837J6zjoO77KbMHPnMyFBEahv8Cn1zR+66c/KiBwnQ4d1fwetYe8xPxMwjg+f/i/UOd X-Received: by 2002:a17:902:ec92:b0:1d7:481c:2066 with SMTP id x18-20020a170902ec9200b001d7481c2066mr3012256plg.128.1705938760801; Mon, 22 Jan 2024 07:52:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705938760; cv=pass; d=google.com; s=arc-20160816; b=FaoLgKgGH9da8czmV0WGxnLT4gtYLWfTm9BhwlANjQBE14AcYhE7h6Y/2fNMS4QI0t 9JBNpLN9N0Il+NP2yVRnn4PorsSYBijPu1Fftsq2A7f83c3vuTL1iQf2QB2pev27duvZ wkSlXt3AerAlApPBVxz6n0FWAb8K1BGaVgefYyGK0qtq/T1S2lXbiL5pgpqRd7H4LsuH p+1BGOhQxlLKAUre7pXQ9fQ1mN56dC2uZanAsiaJssQyy4hFIZw/nRvkTU9gvLPge1L1 51Lrnqq0C5CSe6pFufTIzGH66w5Gs7R6Jt9ZNB1KQOSWbxFkkqHD6fPY2B1KGYXbXvBT +lTA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=pB8swrgfuo82HJIoA7WtzOzLJAOPh3SgeZ4ldwd4D6U=; fh=dPzrzbuoM9cZSjEIqIfqPcQiHaiS0aLFWsDyfwhygxM=; b=fLoFHIJSB0UPE3a2Dhg2LPW5MSbh/RHAfE7iQ8CSiz68XNQ4VKd1aRUnz8suZYF0Xd frdhdgJp58DRBySG1VaADDAbw5orE1IrfRwdekTSO6T1H0611jpiH8158axyEogB48fo OLuU/SojG/x3+A/jmAKoA64NveW2SS5Z8hneA0NP2hM3LSL1L5eCqTrMpx9w7AECsBTj 6ku1UKlrdQ/si0diF8XtVGEKC0vX/TQiB+koRBVhL/8wlO+eMkKo8RRMu2v+FEFBdw/d pQjF4qySSTAI4mjl3w1PWlB6ScTF5lrJX/1Vf0A28bEnKZdfwwb4MGaxM57P7sya3vZI d9hg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DCO8JNh+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-33371-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33371-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id jc5-20020a17090325c500b001d5ee05186csi8238134plb.78.2024.01.22.07.52.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 07:52:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33371-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DCO8JNh+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-33371-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33371-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 F0AC928555E for ; Mon, 22 Jan 2024 15:50:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5B8576D1BA; Mon, 22 Jan 2024 15:07:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DCO8JNh+" Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B719495D9; Mon, 22 Jan 2024 15:06:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705936020; cv=none; b=LzRYYQK2+VunwFDw6La6wxfmfUGVeSbfi7OYD/8Rju0tjPu5UWux3wuP1njvyGtVQq67kkZbtPz4Wt7aOA4281rhN7uxaEr56oKATOncOixDE9pirhbPRouPVtnrxXRybdkOWjkVCPRVZBb/s3HhbW4uyGS28JQ2a2aS3k2U+kY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705936020; c=relaxed/simple; bh=4UjfoZOWKhz9CUcE2OAu1F/mEKNnn+MN6pIwNPWP0kE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dJTz7LG5Z7SPibbTDRNnIsPrAXSP9I8aQZpWTjLguI59B0gIZFK2uFW6EZhi29khyXTTgq0kIE++UxNaStPaHW6CrqpxQaXyZWbk/KfRo4M66AtXKYjCNS4VOBhRIdZK0j38YPt+18VpqFtmAKskXcICuWoHmI/HuPEX9NjQrf4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DCO8JNh+; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d7517fe9d4so836105ad.0; Mon, 22 Jan 2024 07:06:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705936018; x=1706540818; 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=pB8swrgfuo82HJIoA7WtzOzLJAOPh3SgeZ4ldwd4D6U=; b=DCO8JNh+kOCXQkxFVcIdv8AcW4E/R8N5fKeFvWpNMb+7COl2LdAgDEXLWoZYn9hdQB I1TTWhw/23Uh5CvgsXZH6Fbd/P4FIylo4IH0wJS5vxvsyNwICHtrpzu5Dex4gQ6nzWpc BhgB5CPJl3OLZqIH8Eyhhh3DNT2uP1HHtGNiq0vjdndC9xhgwdE4uqo/qIs8ZuTqSbYV tQqo9WQERPxePqEhTwdhAgLV0+dcnutFDUWh2CraCjnPWCrrtkHe3fJ026G6qfr9Hx/a /ZGFM2AYQwPJpz4NzOAqg+MfG8ocONBSTD6mQmowPZS3/TVVBY6n2Q5CNEi+RfIicdzf xDfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705936018; x=1706540818; 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=pB8swrgfuo82HJIoA7WtzOzLJAOPh3SgeZ4ldwd4D6U=; b=L8yVn5/VjB7kLjt4jxyODeAFodMFAsMpnKslNcSuX/uYikvETGz9WRS01CCiJCi1jv dtTK+3lLbNo1qBrz7SkrEN12ST/6A1xiwc971hmXgVuwACZfEEVDDtnC4IGKSA2aNG6f 4Btkmc6LhPlp/VYLqDfm/TSpehnYBo2zIUTEs31E/9OuxNWfWD/wY9HZoafscjGDVCRr s029z9wAOEf+CXzbc6II9t9rgN/SqlsUtU175l/sEnExIAIWDjQAP4ltNaY0gHXkZWHo lUQDPk7C3Ar1O2rQbNMS5InXa2swgS+DTnB2Q9jXCl+rT6vKuwOkCC4NAdCW2jD1nNt/ ucxg== X-Gm-Message-State: AOJu0Yz20wVZuVHul23rDwmjfsXs8kXgd0FXq4HAS0mdS1s4mcPXxGjO x9R3HBRIuzLXy+D/0bV7eTuskz8Tg4My+q4xRMLP7zSYFEFIhRnq X-Received: by 2002:a17:903:54a:b0:1d5:f667:10a1 with SMTP id jo10-20020a170903054a00b001d5f66710a1mr8439165plb.6.1705936018228; Mon, 22 Jan 2024 07:06:58 -0800 (PST) Received: from visitorckw-System-Product-Name (IP-216-168.cs.nctu.edu.tw. [140.113.216.168]) by smtp.gmail.com with ESMTPSA id t1-20020a1709028c8100b001d7505a6894sm2020913plo.230.2024.01.22.07.06.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 07:06:57 -0800 (PST) Date: Mon, 22 Jan 2024 23:06:54 +0800 From: Kuan-Wei Chiu To: Kent Overstreet Cc: colyli@suse.de, bfoster@redhat.com, jserv@ccns.ncku.edu.tw, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org Subject: Re: [PATCH 0/5] Optimize number of comparisons for heap/heapsort implementaion Message-ID: References: <20240121153649.2733274-1-visitorckw@gmail.com> 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=us-ascii Content-Disposition: inline In-Reply-To: On Sun, Jan 21, 2024 at 12:41:55PM -0500, Kent Overstreet wrote: > On Mon, Jan 22, 2024 at 12:55:51AM +0800, Kuan-Wei Chiu wrote: > > On Sun, Jan 21, 2024 at 11:21:06AM -0500, Kent Overstreet wrote: > > > On Sun, Jan 21, 2024 at 11:36:44PM +0800, Kuan-Wei Chiu wrote: > > > > Hello, > > > > > > > > The existing implementations of heap/heapsort follow the conventional > > > > textbook approach, where each heapify operation requires approximately > > > > 2*log2(n) comparisons. In this series, I introduce a bottom-up variant > > > > that reduces the number of comparisons during heapify operations to > > > > approximately log2(n), while maintaining the same number of swap > > > > operations. > > > > > > > > Thanks, > > > > Kuan-Wei > > > > > > > > Kuan-Wei Chiu (5): > > > > bcachefs: Optimize eytzinger0_sort() using bottom-up heapsort > > > > bcachefs: Introduce parent function for sort_cmp_size() > > > > bcachefs: Optimize sort_cmp_size() using bottom-up heapsort > > > > bcachefs: Optimize number of comparisons in heap_sift_down > > > > bcache: Optimize number of comparisons in heap_sift > > > > > > > > drivers/md/bcache/util.h | 23 +++++---- > > > > fs/bcachefs/util.c | 109 ++++++++++++++++++++++++++------------- > > > > fs/bcachefs/util.h | 23 +++++---- > > > > 3 files changed, 98 insertions(+), 57 deletions(-) > > > > > > Good stuff > > > > > > While we're looking at this code, we should be doing some cleanup too - > > > there's no reason for the heap code to be duplicated in bcache and > > > bcachefs anymore, and it'd also be nice to get fs/bcachefs/eytzinger.h > > > moved to include/linux and bcache converted to use it. > > > > > > I also would not be surprised if there's another heap implementation in > > > include/linux; we'll want to check for that and if there is decide which > > > is worth keeping. > > > > > Yes, we have 'min_heap.h' in include/linux. > > So that has the advantage of more readable code - functions instead of > macros - whereas my version has the type safe interface. > > We could combine the two approaches, and put a type-safe interface on > top of the min_heap.h code with some small macro wrappers - see > generic-radix-tree.h for an example of how that's done. Without modifying the interface provided by min_heap.h, it seems challenging to implement the functionality of heap_add due to the relationship with heap_setbackpointer. Additionally, when looking into the code in generic-radix-tree.h, should we replace type[0] with type[]? This is because zero-length arrays are deprecated language features mentioned in document [1]. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays [1] > > min_heap.h has only one user though? I don't think I can quite believe > that's the only other code in the kernel using a heap, there must be > more open coded out there... I'm not sure why, but it seems that in the kernel, other places using the heap implement their own subsystem-specific solutions rather than utilizing a generic heap interface. For instance, kernel/sched/cpudeadline.c and net/sched/sch_cake.c both have their own implementations.