Re: [RESEND PATCH v5 04/16] lib min_heap: Add type safe interface

From: Kuan-Wei Chiu
Date: Thu May 23 2024 - 11:35:43 EST


On Wed, May 22, 2024 at 04:10:48PM -0700, Andrew Morton wrote:
> On Tue, 14 May 2024 16:47:12 +0800 Kuan-Wei Chiu <visitorckw@xxxxxxxxx> wrote:
>
> > Implement a type-safe interface for min_heap using strong type
> > pointers instead of void * in the data field. This change includes
> > adding small macro wrappers around functions, enabling the use of
> > __minheap_cast and __minheap_obj_size macros for type casting and
> > obtaining element size. This implementation removes the necessity of
> > passing element size in min_heap_callbacks. Additionally, introduce the
> > MIN_HEAP_PREALLOCATED macro for preallocating some elements.
>
> I guess it's a nice change, although it's unclear how valuable all this
> is. Being able to remove the local implementations in bcache and
> bcachefs is good.
>
> > --- a/drivers/md/dm-vdo/repair.c
> > +++ b/drivers/md/dm-vdo/repair.c
> > @@ -51,6 +51,8 @@ struct recovery_point {
> > bool increment_applied;
> > };
> >
> > +MIN_HEAP(struct numbered_block_mapping, replay_heap);
> >
> > ...
> >
> > -struct min_heap {
> > - void *data;
> > - int nr;
> > - int size;
> > -};
> > +#define MIN_HEAP_PREALLOCATED(_type, _name, _nr) \
> > +struct _name { \
> > + int nr; \
> > + int size; \
> > + _type *data; \
> > + _type preallocated[_nr]; \
> > +}
>
> I think that the MIN_HEAP() macro would be better named
> DEFINE_MIN_HEAP(). There's a lot of precedent for this and it's
> unclear whether "MIN_HEAP" actually implements storage. I looked at
> the repair.c addition and thought "hm, shouldn't that be static"!
>
I will update this in the next version.

> > /* Sift the element at pos down the heap. */
> > static __always_inline
> > -void min_heapify(struct min_heap *heap, int pos,
> > +void __min_heapify(min_heap_char *heap, int pos, size_t elem_size,
> > const struct min_heap_callbacks *func)
> > {
>
> It's a separate project, but min_heap.h has some crazily huge inlined
> functions. I expect we'd have a smaller and faster kernel if those
> were moved into a new lib/min_heap.c.
>
Moving these inline functions to lib/min_heap.c to reduce kernel size
seems reasonable. However, I saw Ian mention that perf event's min_heap
is used during context switches. Since I'm unsure about the performance
impact of making these functions non-inline, I'd like to discuss this
further before actually moving them to lib/min_heap.c.