Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2155179rdb; Sun, 21 Jan 2024 09:42:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPUq0D6hN399D516IT0DS+DeteX4NR6d4rN+9QCXUS3Y8o0M8lt02F4cYoq9B+KUJRAHLs X-Received: by 2002:a05:6808:1b0d:b0:3bd:b5fe:fe77 with SMTP id bx13-20020a0568081b0d00b003bdb5fefe77mr1090539oib.112.1705858936177; Sun, 21 Jan 2024 09:42:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705858936; cv=pass; d=google.com; s=arc-20160816; b=ker/EVFAp/etT9X+WHFufWBTqOemy7qVZTEImChpj/3F4yCZgd2Q8CxScCpqseah+3 QpD9rAGegBXRMeNFgv13r/KZ7L0n71dRQU1lV+YZuDS9NMVQx0yokXVcqCQO/aTdfnXJ oOY6Iq4fTI9sBDHepgDq7lkCrXX4rnzOcOnFIXHX5i+jf4Xgw+GNDH89oa8VNvTeXUqr 4NGTxIeK1VgJq0qEzmqABevAqi15h2M9b6S2MvMaEFO6/aDW1Znl+2lmG6zd9pu+hael ktaLCI8LRmz0o7rD0jsaYWtdWlly9om3LujI+9qxPwPNYcZY3HNLiy1zfWbdxVkt4Z3g NjCw== 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=zEZ5U22ZGrF+dm4NBxH8Qr8fPO+NuB0IQfzMnXwEl2I=; fh=Qa0Du3tp9W8XPJlojWqG/hiIP9RCqJ6yVRwNsBvO/0A=; b=RnGkIUeYhHPDzvr5L50AnvRYDDUc3oAanbb4D8ySqhrcGKxb2EGNz/7eP001SqRDMQ 2Szz7JdTEygbG1ngptCux8mO35LG7zkN+fpj/vHnc111+kMFH2JjWPUWjkLVXrc9Mhck IpQ1SWLjGRPOxIDfFzHjaKmti+cTrRNvkadswMM2t0I+4g43gtXCiC73IGSU4bkbV1Il cTKSHJ8Ubxd/xNQnTfcsvX1p5DcsDqRm44nnGIzA/4Kuovns8U7cOJbbbfKEMFp9/lQL KJqSURh2aGqhTSsRfV44uv8jyYtSyU+6mXPU8wUnoecK1ep9sdZ7cBm6EROrzE62KT7T n6mg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="SAnE/LP1"; 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-32143-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32143-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bz20-20020a056a02061400b005cdffc3c54dsi7112296pgb.186.2024.01.21.09.42.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 09:42:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32143-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="SAnE/LP1"; 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-32143-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32143-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id CA5C2B218F9 for ; Sun, 21 Jan 2024 17:42:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 60B82381D6; Sun, 21 Jan 2024 17:42:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SAnE/LP1" Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 01C7C381C7 for ; Sun, 21 Jan 2024 17:41:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705858921; cv=none; b=iAG5N3COywF1mGb5dUMgT1pwQ7iyh/uwP6xbbQmB8zBPgJYdyDuCvKDu5uN53nP7kj7NXfsHH5wSGOgINgz7e/JA1XGLDKmHk3/aQf4TWNgLyZJWma+nrfGb3jddoSb8joBo5L5gA12avLHA+B1PbpP5zNTo+2M1G9lOCS8PnBs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705858921; c=relaxed/simple; bh=N2g0NHGKWWZmb/qlzQ163FWOfAKGzGlUYORWKPpiytI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=I+etJqK0Gcp8wKyyFVAHb9qunCyE/IonGo8JqiGrBmpuXI+2tSIC52GZ0IcbwI1CF5thqNjZF7RFjr6HWcxK1/ahaEmvAF71qlPyekNFj2d0UIZVWFXQ3MAxeGk5hJy1HBMEm8dlNTQP1iA/S+D72FPef1eCxvwPEnuSqcz0yqw= 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=SAnE/LP1; arc=none smtp.client-ip=95.215.58.172 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: Sun, 21 Jan 2024 12:41:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1705858918; 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=zEZ5U22ZGrF+dm4NBxH8Qr8fPO+NuB0IQfzMnXwEl2I=; b=SAnE/LP1jwOwzXFkLVsJ3sKoqSTNCqYfTEYCKAmoFk1FmHnZ26Iz4mV15j37lFPDee+j3n sgRxhokF3swjiFCXQMxWTxwVZP3QhEUF+rp94gIAEpt0o4e6y/XV2kDywFSZTWBKXPT5wK JqXTf9UqF55p7CtlVOD/ol70LJ9tJIM= 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 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. 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...