From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: Robert Pang <robertpang@google.com>
Cc: corbet@lwn.net, colyli@kernel.org, kent.overstreet@linux.dev,
akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org, linux-bcache@vger.kernel.org,
jserv@ccns.ncku.edu.tw, stable@vger.kernel.org
Subject: Re: [PATCH 1/8] lib min_heap: Add equal-elements-aware sift_down variant
Date: Fri, 13 Jun 2025 14:17:58 +0800 [thread overview]
Message-ID: <aEvCljo6GPRRvWuO@visitorckw-System-Product-Name> (raw)
In-Reply-To: <CAJhEC05pmnTd9mROTazKMFzSO+CcpY8au57oypCiXGaqhpA_2Q@mail.gmail.com>
On Thu, Jun 12, 2025 at 10:00:14PM +0900, Robert Pang wrote:
> Hi Kuan-Wei
>
> Thanks for this patch series to address the bcache latency regression.
> I tested it but results show regression still remains. Upon review of
> the patch changes, I notice that the min_heap_sift_down_eqaware_inline
> #define macro in this patch may have been mapped incorrectly:
>
> +#define min_heap_sift_down_eqaware_inline(_heap, _pos, _func, _args) \
> + __min_heap_sift_down_inline(container_of(&(_heap)->nr,
> min_heap_char, nr), _pos, \
> + __minheap_obj_size(_heap), _func, _args)
>
> I changed it to map to its "eqaware" counterpart like this and the
> regression does not happen again.
>
> +#define min_heap_sift_down_eqaware_inline(_heap, _pos, _func, _args) \
> + __min_heap_sift_down_eqaware_inline(container_of(&(_heap)->nr,
> min_heap_char, nr), _pos, \
> + __minheap_obj_size(_heap), _func, _args)
>
> Do you think this correction is appropriate?
>
That's definitely my mistake.
Thanks for testing and pointing it out.
I'll fix the typo in the next version.
Regards,
Kuan-Wei
> Best regards
> Robert Pang
>
> On Wed, Jun 11, 2025 at 6:55 AM Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
> >
> > The existing min_heap_sift_down() uses the bottom-up heapify variant,
> > which reduces the number of comparisons from ~2 * log2(n) to
> > ~1 * log2(n) when all elements are distinct. However, in workloads
> > where the heap contains many equal elements, this bottom-up variant
> > can degenerate and perform up to 2 * log2(n) comparisons, while the
> > traditional top-down variant needs only O(1) comparisons in such cases.
> >
> > To address this, introduce min_heap_sift_down_eqaware(), a top-down
> > heapify variant optimized for scenarios with many equal elements. This
> > variant avoids unnecessary comparisons and swaps when elements are
> > already equal or in the correct position.
> >
> > Cc: stable@vger.kernel.org # 6.11+
> > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> > ---
> > include/linux/min_heap.h | 51 ++++++++++++++++++++++++++++++++++++++++
> > lib/min_heap.c | 7 ++++++
> > 2 files changed, 58 insertions(+)
> >
> > diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h
> > index 79ddc0adbf2b..b0d603fe5379 100644
> > --- a/include/linux/min_heap.h
> > +++ b/include/linux/min_heap.h
> > @@ -292,6 +292,52 @@ void __min_heap_sift_down_inline(min_heap_char *heap, size_t pos, size_t elem_si
> > __min_heap_sift_down_inline(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \
> > __minheap_obj_size(_heap), _func, _args)
> >
> > +/*
> > + * Sift the element at pos down the heap.
> > + *
> > + * Variants of heap functions using an equal-elements-aware sift_down.
> > + * These may perform better when the heap contains many equal elements.
> > + */
> > +static __always_inline
> > +void __min_heap_sift_down_eqaware_inline(min_heap_char * heap, size_t pos, size_t elem_size,
> > + const struct min_heap_callbacks *func, void *args)
> > +{
> > + void *data = heap->data;
> > + void (*swp)(void *lhs, void *rhs, void *args) = func->swp;
> > + /* pre-scale counters for performance */
> > + size_t a = pos * elem_size;
> > + size_t b, c, smallest;
> > + size_t n = heap->nr * elem_size;
> > +
> > + if (!swp)
> > + swp = select_swap_func(data, elem_size);
> > +
> > + for (;;) {
> > + b = 2 * a + elem_size;
> > + c = b + elem_size;
> > + smallest = a;
> > +
> > + if (b >= n)
> > + break;
> > +
> > + if (func->less(data + b, data + smallest, args))
> > + smallest = b;
> > +
> > + if (c < n && func->less(data + c, data + smallest, args))
> > + smallest = c;
> > +
> > + if (smallest == a)
> > + break;
> > +
> > + do_swap(data + a, data + smallest, elem_size, swp, args);
> > + a = smallest;
> > + }
> > +}
> > +
> > +#define min_heap_sift_down_eqaware_inline(_heap, _pos, _func, _args) \
> > + __min_heap_sift_down_inline(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \
> > + __minheap_obj_size(_heap), _func, _args)
> > +
> > /* Sift up ith element from the heap, O(log2(nr)). */
> > static __always_inline
> > void __min_heap_sift_up_inline(min_heap_char *heap, size_t elem_size, size_t idx,
> > @@ -433,6 +479,8 @@ void *__min_heap_peek(struct min_heap_char *heap);
> > bool __min_heap_full(min_heap_char *heap);
> > void __min_heap_sift_down(min_heap_char *heap, size_t pos, size_t elem_size,
> > const struct min_heap_callbacks *func, void *args);
> > +void __min_heap_sift_down_eqaware(min_heap_char *heap, size_t pos, size_t elem_size,
> > + const struct min_heap_callbacks *func, void *args);
> > void __min_heap_sift_up(min_heap_char *heap, size_t elem_size, size_t idx,
> > const struct min_heap_callbacks *func, void *args);
> > void __min_heapify_all(min_heap_char *heap, size_t elem_size,
> > @@ -455,6 +503,9 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx,
> > #define min_heap_sift_down(_heap, _pos, _func, _args) \
> > __min_heap_sift_down(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \
> > __minheap_obj_size(_heap), _func, _args)
> > +#define min_heap_sift_down_eqaware(_heap, _pos, _func, _args) \
> > + __min_heap_sift_down_eqaware(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \
> > + __minheap_obj_size(_heap), _func, _args)
> > #define min_heap_sift_up(_heap, _idx, _func, _args) \
> > __min_heap_sift_up(container_of(&(_heap)->nr, min_heap_char, nr), \
> > __minheap_obj_size(_heap), _idx, _func, _args)
> > diff --git a/lib/min_heap.c b/lib/min_heap.c
> > index 96f01a4c5fb6..2225f40d0d7a 100644
> > --- a/lib/min_heap.c
> > +++ b/lib/min_heap.c
> > @@ -27,6 +27,13 @@ void __min_heap_sift_down(min_heap_char *heap, size_t pos, size_t elem_size,
> > }
> > EXPORT_SYMBOL(__min_heap_sift_down);
> >
> > +void __min_heap_sift_down_eqaware(min_heap_char *heap, size_t pos, size_t elem_size,
> > + const struct min_heap_callbacks *func, void *args)
> > +{
> > + __min_heap_sift_down_eqaware_inline(heap, pos, elem_size, func, args);
> > +}
> > +EXPORT_SYMBOL(__min_heap_sift_down_eqaware);
> > +
> > void __min_heap_sift_up(min_heap_char *heap, size_t elem_size, size_t idx,
> > const struct min_heap_callbacks *func, void *args)
> > {
> > --
> > 2.34.1
> >
next prev parent reply other threads:[~2025-06-13 6:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-10 21:55 [PATCH 0/8] Fix bcache regression with equality-aware heap APIs Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 1/8] lib min_heap: Add equal-elements-aware sift_down variant Kuan-Wei Chiu
2025-06-12 13:00 ` Robert Pang
2025-06-13 6:17 ` Kuan-Wei Chiu [this message]
2025-06-10 21:55 ` [PATCH 2/8] lib min_heap: Add typedef for sift_down function pointer Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 3/8] lib min_heap: add eqaware variant of min_heapify_all() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 4/8] lib min_heap: add eqaware variant of min_heap_pop() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 5/8] lib min_heap: add eqaware variant of min_heap_pop_push() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 6/8] lib min_heap: add eqaware variant of min_heap_del() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 7/8] Documentation/core-api: min_heap: Document _eqaware variants of min-heap APIs Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 8/8] bcache: Fix the tail IO latency regression by using equality-aware min heap API Kuan-Wei Chiu
2025-06-12 1:48 ` [PATCH 0/8] Fix bcache regression with equality-aware heap APIs Andrew Morton
2025-06-12 1:54 ` Andrew Morton
2025-06-13 6:15 ` Kuan-Wei Chiu
2025-06-13 14:26 ` Robert Pang
2025-06-13 18:04 ` Andrew Morton
2025-06-13 23:19 ` Kuan-Wei Chiu
2025-06-14 1:31 ` Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aEvCljo6GPRRvWuO@visitorckw-System-Product-Name \
--to=visitorckw@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=colyli@kernel.org \
--cc=corbet@lwn.net \
--cc=jserv@ccns.ncku.edu.tw \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robertpang@google.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).