Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp679866lqe; Sat, 6 Apr 2024 21:12:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVSKl+iUQJ+g6AbzgVlc5N3e26TBYvGpZ0Rd+x1VLtAgN7cNi9Hcv2F+/KTlWbg4q2IiaLXGeiuLGzM1JM3p7aIT7y2tGANwjcIxakC5A== X-Google-Smtp-Source: AGHT+IFKqR0XLyG//ZcGHodVaoXXYWVaN9ljWWlXssAG292TTH3+p94ZSaaVmatuJ5DN09GzX1vw X-Received: by 2002:a05:6a20:e614:b0:1a3:50b7:7b5b with SMTP id my20-20020a056a20e61400b001a350b77b5bmr6885520pzb.60.1712463157849; Sat, 06 Apr 2024 21:12:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712463157; cv=pass; d=google.com; s=arc-20160816; b=Pk+iYALngO+r0pVTv72vxGhHjVsg3s1WoZZhGsp9bkFaMQvuYopcVJ+Rbfmzs2o91U 5JmaoVhqnsWf+xQpIMxvbHqr8EAo5f6HVXys6jfv2xzXVu6G+SjLPILZTzulPo3xuQtP ZxRpeeLvWWefG3DKjsOwS+RqdHFNO6pGecgkvZHjNONcyaCAIFxuGlLw/UxPnfAW4j42 LtI5+o1dg0bDoMr4TnRgNUctj5ufcfmJ2JcAvt1Wh60s2NZvLhK9pvwXurHmIjLx463V fCQSKGx7xTakl6PPNbuOddcudc5k0GFqaDe0GromC5xKv/3IZzLR1sgTmucEZfigTW/o O8Hg== 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=4Akf+5PjShga/Knr2bfSD++VsHqynTRQPRIszKrS9DM=; fh=Rp9wF8a0dww/KpivhMFtvQeG441vyJKH/OwDXFAmwG8=; b=krpPU5S23p5GnT2LJ0MEp23gduHIX4MxfbtOyfEu4Qoez9tqa/BK8YMR5XwgNh0XlX rNU4SdjX9/amseV2p+wt+7UlJZRWVNVH40T6GbuxVoEOlgCZK43EJ3ew8Ixje40XWr5U xUcN6BxV4wihbRHKZ9by6gMQCVLYIbXr80llUWDCO9SwcNrZ5knc3LzzlyJRGkMRQu98 KCZfXlf+pnRpMaDLy5e6EnOcMajxbyHe2Zfw9z0N5Va69UIbSI0uly+AYTbHDU9DCzN3 /fPslxTjmkN5e2kfL4meDt6oo5hX9Wq2EfueER8RknprDe5AClMb0t/ihC79bJMqKy6Q L4MQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=UeQyHtIc; 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-134161-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134161-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r18-20020a632052000000b005dc528a5317si4078509pgm.50.2024.04.06.21.12.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 21:12:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-134161-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=@linux.dev header.s=key1 header.b=UeQyHtIc; 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-134161-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134161-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8467D282576 for ; Sun, 7 Apr 2024 04:12:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 804C55CA1; Sun, 7 Apr 2024 04:12:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="UeQyHtIc" Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 622E7184D for ; Sun, 7 Apr 2024 04:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712463150; cv=none; b=arzsDZSjGYxFL4wINj3PCKjqDgs6BXOzTKnLuoLee1SPpoJjfFrkfqP0HLU+TnPQ4cpR/PX4ePwyQisxdHFzYDCsHuLVF0bMe/WoKS5sFPqh95rQk4LthwcBf4vz91pHh3fJsBB/RRXSJclP/OpKAw6D0i7ss7hSQHTISBNRPTQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712463150; c=relaxed/simple; bh=Z4KqCREDgBjK4L8+1jCUewIR3d1elGDVnhZBpRAtp2A=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mFnvHsYeY3TmScKOrInBlR/nBNrn7Qky2e2x1zAyuAyujf/2hwym+25FA7sA3aLpFnYb7jB6cjo/WBx/+N1EBvXwAEOO/4iMnum4Rb7qfPMsK5iRyFCtIGlOloy1KKWEM+q8+ToZBKwIeIIe9JSzwvjhXA+DurHTOL2PWNExOkE= 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=UeQyHtIc; arc=none smtp.client-ip=91.218.175.179 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, 7 Apr 2024 00:12:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1712463145; 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=4Akf+5PjShga/Knr2bfSD++VsHqynTRQPRIszKrS9DM=; b=UeQyHtIcV8Lc9NLfxaHwq2NA4kugPQURyVr0yc8mvtcEdG89Mx3lf4G5NShmKMKMlpE89B S1N2HFQiycv1UV2Hu/oGKV0I0fJfG3GjA2TmLEJiL+088ODm4rOHH+Sn6UAUkQxbSwyRbD Vhzo++NSvrLpOtgtlCfaAgqwlGuAcmE= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Kuan-Wei Chiu Cc: bfoster@redhat.com, jserv@ccns.ncku.edu.tw, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] bcachefs: Optimize eytzinger0_sort() with bottom-up heapsort Message-ID: References: <2vm32rfcbgy4fap77scebqorlrpaihz3angd6bl2lnpslrcp63@kekiknxt5vup> <20240407033904.719773-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: <20240407033904.719773-1-visitorckw@gmail.com> X-Migadu-Flow: FLOW_OUT On Sun, Apr 07, 2024 at 11:39:04AM +0800, Kuan-Wei Chiu wrote: > This optimization reduces the average number of comparisons required > from 2*n*log2(n) - 3*n + o(n) to n*log2(n) + 0.37*n + o(n). When n is > sufficiently large, it results in approximately 50% fewer comparisons. > > Currently, eytzinger0_sort employs the textbook version of heapsort, > where during the heapify process, each level requires two comparisons > to determine the maximum among three elements. In contrast, the > bottom-up heapsort, during heapify, only compares two children at each > level until reaching a leaf node. Then, it backtracks from the leaf > node to find the correct position. Since heapify typically continues > until very close to the leaf node, the standard heapify requires about > 2*log2(n) comparisons, while the bottom-up variant only needs log2(n) > comparisons. > > The experimental data presented below is based on an array generated > by get_random_u32(). > > | N | comparisons(old) | comparisons(new) | time(old) | time(new) | > |-------|------------------|------------------|-----------|-----------| > | 10000 | 235381 | 136615 | 25545 us | 20366 us | > | 20000 | 510694 | 293425 | 31336 us | 18312 us | > | 30000 | 800384 | 457412 | 35042 us | 27386 us | > | 40000 | 1101617 | 626831 | 48779 us | 38253 us | > | 50000 | 1409762 | 799637 | 62238 us | 46950 us | > | 60000 | 1721191 | 974521 | 75588 us | 58367 us | > | 70000 | 2038536 | 1152171 | 90823 us | 68778 us | > | 80000 | 2362958 | 1333472 | 104165 us | 78625 us | > | 90000 | 2690900 | 1516065 | 116111 us | 89573 us | > | 100000| 3019413 | 1699879 | 133638 us | 100998 us | > > Refs: > BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average, > QUICKSORT (if n is not very small) > Ingo Wegener > Theoretical Computer Science, 118(1); Pages 81-98, 13 September 1993 > https://doi.org/10.1016/0304-3975(93)90364-Y > > Signed-off-by: Kuan-Wei Chiu Thanks - applied