From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 70D083858D20 for ; Wed, 27 Mar 2024 19:59:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 70D083858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 70D083858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711569602; cv=none; b=egN6aWQn5FrjX1ed7u9XBOX2U8/gt+uXt52t3+W7FwaWjoJqIeR8msyHOpH9PLBVAmEeCKTVB4T51WEesiy90tPmUp3KkTVJmWabPbC8peJTkn285QsuAjbZqtqKPDOCLaFy2Mkl8Ko4YeLCjV8RT+XRRE+zY0SWX9VQjiTbDbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711569602; c=relaxed/simple; bh=NuLa4BRbW3GVBnDpcCAUWME+XUDnNrzSjFWK3l6JUZg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Ftmg03l5JRH6qsxa6Wk5ScM6ykhV3jsMURmb6NFWWHl0SYK5buBRryRVrfGXNQM3yUYv590RTdYMNZPfvaz0821u6k39/IBFo6/rD9vDhE/qjpa/lw3c2x/uxeyFUb0psp09GfdbnTKONySrcGPyttu614K42pXXXAjQQQuJQ0Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1e0e89faf47so579935ad.1 for ; Wed, 27 Mar 2024 12:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711569594; x=1712174394; darn=sourceware.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=TNT5UH7oKDViH0AQFFFkCGCIkKY8fJ0SLUNrHXATWyE=; b=RH4LiEIHzjgDjJBS1p9cCGdCeVL5caNlWvEg4fSLw4Ip7HRLNky24eLsi5JEQa1N+J NTtAF+VVR07SW9JG7fIBrvHqjZZaIZOcwvSl+gwlIkjXq03hLT4ih+0iix4FyjgxsXSt LQ5l68myiGJ92T5fhEJp+MxbM/3nMwFPedReRXFoIg9jYHGo5nnFAb8OHdopzYJlv9zf CeUYBSIWf8UM94ZJTvtqwL/7AvmNacOpZQ0AArWl0oxgmNYq7iHavvEnVJeTt3dvxMDK ZRVVilXkNTAc2AiYJcDOp4ZMJT1KbYDLRkt1cwkHkbVxFlS3hjtezLXppTrRZ2Y29oV/ hS0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711569594; x=1712174394; 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=TNT5UH7oKDViH0AQFFFkCGCIkKY8fJ0SLUNrHXATWyE=; b=K1HkNqy9F08Vz2Y7HTGpTgttw0QEtr00l6ldVa3ymf4a+Bx/dd3xv4lHsWBRz9xBIk ha6rc5YLJ3Tns85WJaSX0AkpIhKv7aOFaVAOMLl36ZImBdwZxwNkBqALYRq0rgWytLUi 5MGXEaFsYz7ZSX5vZbS+Rs5wyqNnDvEKSnQOoga8QRAxoOzdYrlk/28WKSfr3jMt04T6 DUGgd5R7lp2iPvs2rodaun4AbXlvoq9AeS+DKC7UHJ2cFDbk0BZe6OVy6whjirnX1cF5 BJ97olF7M4hx+yQQa63CDe/u0eSOnAjFiPO6N6PwIAw6B9JH15q5k4mH19PnQi/Vytw7 cZgw== X-Gm-Message-State: AOJu0YwmyO8pCfj3l9Qfp71WkothBamefyRiq8CUcSrTBjiVB5CpM0SC eo1BuEzaSTzlm2rqRxNbuq1q/lqqsk0r6p4JzK6WkRLzbFxIrSDr X-Google-Smtp-Source: AGHT+IEdoZdabp46MxrR0LqOmzKI7Q8H+kjZ6gEDmWmOMgn+T7PoCxBaC86LZTqMTX7+bG73ZwY3wg== X-Received: by 2002:a17:902:c3c4:b0:1db:ce31:96b1 with SMTP id j4-20020a170902c3c400b001dbce3196b1mr726857plj.6.1711569594221; Wed, 27 Mar 2024 12:59:54 -0700 (PDT) Received: from visitorckw-System-Product-Name ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id jj14-20020a170903048e00b001ddc83fda95sm9390658plb.186.2024.03.27.12.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:59:53 -0700 (PDT) Date: Thu, 28 Mar 2024 03:59:50 +0800 From: Kuan-Wei Chiu To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org, goldstein.w.n@gmail.com, fweimer@redhat.com, zack@owlfolio.org, jserv@ccns.ncku.edu.tw Subject: Re: [PATCH] stdlib: Optimize number of calls to comparison function Message-ID: References: <20231202214839.2634493-1-visitorckw@gmail.com> <20240216070835.GA1028715@sivslab-System-Product-Name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, Mar 27, 2024 at 04:45:35PM -0300, Adhemerval Zanella Netto wrote: > > > On 16/02/24 04:08, Kuan-Wei Chiu wrote: > > On Sun, Dec 03, 2023 at 05:48:39AM +0800, Kuan-Wei Chiu wrote: > >> The current heapsort implementation in the siftdown function follows > >> the standard textbook version, necessitating two comparisons at each > >> level. Transitioning to the Bottom-up heapsort version allows us to > >> decrease the required comparisons to just one per level. On average, > >> this modification significantly reduces the comparison count from > >> 2nlog2(n) - 3n + o(n) to nlog2(n) + 0.37*n + o(n). > >> > >> 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 > >> --- > >> stdlib/qsort.c | 36 ++++++++++++++++++------------------ > >> 1 file changed, 18 insertions(+), 18 deletions(-) > >> > >> diff --git a/stdlib/qsort.c b/stdlib/qsort.c > >> index be01fb5598..f5babef150 100644 > >> --- a/stdlib/qsort.c > >> +++ b/stdlib/qsort.c > >> @@ -132,26 +132,26 @@ static inline void > >> siftdown (void *base, size_t size, size_t k, size_t n, > >> enum swap_type_t swap_type, __compar_d_fn_t cmp, void *arg) > >> { > >> - /* There can only be a heap condition violation if there are > >> - children. */ > >> - while (2 * k + 1 <= n) > >> - { > >> - /* Left child. */ > >> - size_t j = 2 * k + 1; > >> - /* If the right child is larger, use it. */ > >> - if (j < n && cmp (base + (j * size), base + ((j + 1) * size), arg) < 0) > >> - j++; > >> - > >> - /* If k is already >= to its children, we are done. */ > >> - if (j == k || cmp (base + (k * size), base + (j * size), arg) >= 0) > >> - break; > >> + size_t i, j; > >> + > >> + /* Find the sift-down path all the way to the leaves. */ > >> + for (i = k; j = 2 * i + 1, j + 1 <= n;) > >> + i = cmp (base + j * size, base + (j + 1) * size, arg) >= 0 ? j : (j + 1); > >> > >> - /* Heal the violation. */ > >> - do_swap (base + (size * j), base + (k * size), size, swap_type); > >> + /* Special case for the last leaf with no sibling. */ > >> + if (j == n) > >> + i = j; > >> > >> - /* Swapping with j may have introduced a violation at j. Fix > >> - it in the next loop iteration. */ > >> - k = j; > >> + /* Backtrack to the correct location. */ > >> + while (i != k && cmp (base + k * size, base + i * size, arg) > 0) > >> + i = (i - 1) / 2; > >> + > >> + /* Shift the element into its correct place. */ > >> + j = i; > >> + while (i != k) > >> + { > >> + i = (i - 1) / 2; > >> + do_swap (base + i * size, base + j * size, size, swap_type); > >> } > >> } > >> > >> -- > >> 2.25.1 > >> > > > > Since we still retain heapsort as a fallback for mergesort, should we > > reconsider applying this patch? > > > > Thanks, > > Kuan-Wei > > Yes, I think it is work and it looks great to me. Should I resend a new patch, or can you directly apply this one? Thanks, Kuan-Wei