[PATCH] mempool: optimize get objects with constant n
Morten Brørup
mb at smartsharesystems.com
Wed Jun 7 10:03:48 CEST 2023
> From: Thomas Monjalon [mailto:thomas at monjalon.net]
> Sent: Wednesday, 7 June 2023 09.52
>
> 18/04/2023 14:55, Bruce Richardson:
> > On Tue, Apr 18, 2023 at 01:29:49PM +0200, Morten Brørup wrote:
> > > From: Bruce Richardson [mailto:bruce.richardson at intel.com]
> > > > On Tue, Apr 11, 2023 at 08:48:45AM +0200, Morten Brørup wrote:
> > > > > + if (__extension__(__builtin_constant_p(n)) && n <= cache->len) {
> > > > > + /*
> > > > > + * The request size is known at build time, and
> > > > > + * the entire request can be satisfied from the cache,
> > > > > + * so let the compiler unroll the fixed length copy loop.
> > > > > + */
> > > > > + cache->len -= n;
> > > > > + for (index = 0; index < n; index++)
> > > > > + *obj_table++ = *--cache_objs;
> > > > > +
> > > >
> > > > This loop looks a little awkward to me. Would it be clearer (and perhaps
> > > > easier for compilers to unroll efficiently if it was rewritten as:
> > > >
> > > > cache->len -= n;
> > > > cache_objs = &cache->objs[cache->len];
> > > > for (index = 0; index < n; index++)
> > > > obj_table[index] = cache_objs[index];
> > >
> > > The mempool cache is a stack, so the copy loop needs get the objects in
> decrementing order. I.e. the source index decrements and the destination index
> increments.
> > >
> >
> > BTW: Please add this as a comment in the code too, above the loop to avoid
> > future developers (or even future me), asking this question again!
>
> Looks like this request was missed.
I intentionally omitted it, because I disagree with the suggestion.
Otherwise, reminders that the mempool cache is a stack should be plastered all over the source code, not just here. For reference, this copy loop (without such a reminder) also exists elsewhere in the same file.
Apologies for not responding to Bruce's request earlier.
More information about the dev
mailing list