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

Kent Overstreet
2024-05-22T23:33:11Z

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@gmail.com> 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.

Yes, and now the standard kernel version is type safe. I suggested it to
Kuan as a good starting project, I find (well picked) refactorings and
cleanups are a great place for people to start.

>
> > --- 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"!

Agreed

> > /* 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.

good thought - Kuan, want to do that too? Since we missed the merge
window, may as well :)