Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp458911rdb; Mon, 22 Jan 2024 09:17:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IE6SKta1aKcem4uUQYdpq+zpQdcDYD37BNGeG7SFlMAZfzZfcJ3Sd0LIVS9jt/kn48chn/l X-Received: by 2002:a05:6512:3d01:b0:50f:ffc0:98b with SMTP id d1-20020a0565123d0100b0050fffc0098bmr357693lfv.118.1705943874306; Mon, 22 Jan 2024 09:17:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705943874; cv=pass; d=google.com; s=arc-20160816; b=qvesm1eidyDzJSuG9HW3GAfaHiGmSjAo9piIxcKsYNndSfH+h05+mmyolzvfKLSAbW MoxG4erCJsMfCzVdpwisSum3iVxRqM6UZoQJ0gy0nQfJNgWsSErnQiv/ViRlXAEl5+pF kCRooEOMiWO11BuP3EdeHPQrprle5K5pnCHJSzH6TJRpckf+BfIYyM5Z4MekYlihus6W xF8KUwTly8PxcVOLxd4lHEQiw/NsuALurGs4xsm2jm56rchVmogJyerowr4rkc3UV0rD ROsMMl9pZ+X1nkKZWYraESLeAyH51rweFB4J4H4+kcbiBqV2lZZpJmgcYeKy2vvVeyoD 7xoQ== 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:dkim-signature:date; bh=55SCyM8CEbCM9qBiiJ38WDhwyohhGRMvyGZuunU3L6U=; fh=Qa0Du3tp9W8XPJlojWqG/hiIP9RCqJ6yVRwNsBvO/0A=; b=fthRd0YHSjUyrlxqdnhVdSDI4zX7bpm/XWajntM/I7I/RN6EcldDFbXyoBRt/Qxrlh 5WDN3II2kKI9oWMKlg/t3rfY+9SxLJq6AzeZGChV+pyjA2abLvAeHiFDgzP4TJ4EvrLe 8YStn/otz1vqw2RtBCYlvqyJHLyCBjq6WfVhTu11P8Y0fXamv/PGy2IH1lRG/rgiIyLZ Tv6HEg+U/xEdIX8EdtA5WBdJPkmvQrpZ32bha4ERy3abZ0r++rXr/n1IVY4IUc8PJnk1 F036XDbZFxCERgldAUsl6Res2vLmpdwQcUFppqg+PPCjg1sHK8O7ZoRBX+wAI6VwYuy/ 3prw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=uGXmZNt2; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-33658-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33658-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id gs9-20020a170906f18900b00a2809452f2fsi10721175ejb.826.2024.01.22.09.17.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 09:17:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33658-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=uGXmZNt2; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-33658-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33658-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 0CFC81F23006 for ; Mon, 22 Jan 2024 17:17:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 70842657C6; Mon, 22 Jan 2024 16:06:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="uGXmZNt2" Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (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 34A15657BB for ; Mon, 22 Jan 2024 16:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705939606; cv=none; b=Q6eoAe6Yh/RJ7HNw1AjIPl8Tw1Wl/brdI85PuLc6OdHHneOnweQ1plKYMYvhHPDfQSzEOL0tOpsZ4/oJ23s+7gd5POvQOYl1NdipKtHts7d1k4j7VZlgbWQmf9fMMRsiC3vkxuZ22YsnnEVxxXpX3puVC3FgcJuSoe4WoMDok4Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705939606; c=relaxed/simple; bh=fsmtq2XF8bbAr6rNw2kj7QtYSt4c9bDE/aZE2TVghmY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=M8mJyFsdnNzIZXMYB9RymLtryphboHZdeH/PhpofoMp6zIuzUnPLXgOxNsbQeC98Mv8lZJ73wuEe0Z7938ygSLDMhZ//rJH9y2WaqREVry3X8epDBC3epJPmI7ThbXsaUd9LbqZ3eteTonSz/FR+WL8N1hdj+Gehox0uAKsxYxI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=uGXmZNt2; arc=none smtp.client-ip=95.215.58.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Mon, 22 Jan 2024 11:06:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1705939602; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=55SCyM8CEbCM9qBiiJ38WDhwyohhGRMvyGZuunU3L6U=; b=uGXmZNt2uUm+IaoPPbYv6wnl01a+haxm2bkGN0EhzSumcgRoACV/fmma7oNeW9V49ceyMA 8pHpHtR1I0PLvtFfmVlDlr/mQDU+osZtvXyyfGrWLGopCx0vhxGFBnc7sQPAie2waB++Is 4ZDqikiFimgmbYabsvGMEJtowwtFqY4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Kuan-Wei Chiu 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: X-Migadu-Flow: FLOW_OUT On Mon, Jan 22, 2024 at 11:06:54PM +0800, Kuan-Wei Chiu wrote: > 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. min_heap.h has the same functionality, different interface - updating the callers for an interface change is fine. > > 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]. Zero length arrays are deprecated as VLAs, but this isn't a VLA - we're not storing anything there, the variable is just so that macros have access to the type. > 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. Sounds like a fun cleanup project :)