Re: [PATCH] seq_file: Optimize seq_puts()

From: Al Viro
Date: Mon Apr 15 2024 - 17:00:51 EST


On Mon, Apr 15, 2024 at 10:47:59PM +0200, Christophe JAILLET wrote:
> Le 04/01/2024 à 14:29, Christophe JAILLET a écrit :
> > Most of seq_puts() usages are done with a string literal. In such cases,
> > the length of the string car be computed at compile time in order to save
> > a strlen() call at run-time. seq_write() can then be used instead.
> >
> > This saves a few cycles.
> >
> > To have an estimation of how often this optimization triggers:
> > $ git grep seq_puts.*\" | wc -l
> > 3391
> >
> > Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
>
> Hi,
>
> any feed-back on this small optimisation of seq_puts()?

> > +#define seq_puts(m, s) \
> > +do { \
> > + if (__builtin_constant_p(s)) \
> > + seq_write(m, s, __builtin_strlen(s)); \
> > + else \
> > + __seq_puts(m, s); \
> > +} while (0)
> > +

No need to make it a macro, actually. And I would suggest going
a bit further:

static inline void seq_puts(struct seq_file *m, const char *s)
{
if (!__builtin_constant_p(*s))
__seq_puts(m, s);
else if (s[0] && !s[1])
seq_putc(m, s[0]);
else
seq_write(m, s, __builtin_strlen(s));
}