On Fri, May 01, 2026 at 10:13:15PM +0100, David Laight wrote:
> On Fri, 1 May 2026 22:40:17 +0800
> Qian-Yu Lin <[email protected]> wrote:
> 
> ...
> > Yes. I measured compile time of kernel/trace/ring_buffer_benchmark.o
> > after make clean on an x86_64 machine running Ubuntu 24.04 LTS:
> > 
> >   - Original _______STR:                 49.8s
> >   - v1 with __UNIQUE_ID (compiler.h):    53.5s
> >   - compound literal (no extra include): 33.2s
> 
> That difference looks far to big to me.
> And the times are far too large to be measuring the actual compile time.
> 

You're right, my earlier measurements included dependency rebuilds
after make clean. I re-measured using touch to isolate the actual
compile time of ring_buffer_benchmark.o on x86_64:

  - Original ___STR:                        1.757s
  - v1 with __UNIQUE_ID (compiler.h):       1.836s
  - sizeof __stringify (your suggestion):   1.781s

> > 
> > I propose using a compound literal in v2, which eliminates the local
> > variable entirely and requires no extra include:
> > 
> > #define trace_printk(fmt, ...)                          \
> > do {                                                    \
> >     if (sizeof((char[])                             \
> >         {__stringify((__VA_ARGS__))}) > 3)      \
> >         do_trace_printk(fmt, ##__VA_ARGS__);    \
> 
> There has to be a better way to align that code.
> Although you should be able to use:
>       if (sizeof __stringify((__VA_ARGS__)) > 3)
> (I've omitted one set of parenthesis for clarity)
> 
> You could change __stringify() to work with __VA_ARGS__ the you don't need
> the extra (); this works fine:
> #define _x(...) #__VA_ARGS__
> #define x(...) _x(__VA_ARGS__)
> #define z abcd
> int a = sizeof x(z, v); /* 8 */
> See: https://godbolt.org/z/zo4h4nr9b
> 
> -- David
> 

Yes, this works. I verified with objdump on the
samples/trace_printk module that all four cases branch correctly:
__trace_bputs, __trace_puts, __trace_bprintk, and __trace_printk.

I'll use this form in v3 since it's simpler than the compound literal.

> >     else                                            \
> >         trace_puts(fmt);                        \
> > } while (0)
> > 
> > This fully eliminates the shadowing risk without any compile overhead.
> > 
> > Qian-Yu
> > 
> > > 
> > >   
> > > >  #include <linux/compiler_attributes.h>
> > > >  #include <linux/instruction_pointer.h>
> > > >  #include <linux/stddef.h>
> > > > @@ -84,15 +85,18 @@ do {                                                
> > > >                         \
> > > >   * let gcc optimize the rest.
> > > >   */
> > > >  
> > > > -#define trace_printk(fmt, ...)                         \
> > > > +#define ___trace_printk(fmt, str, ...)                         \
> > > >  do {                                                   \
> > > > -       char _______STR[] = __stringify((__VA_ARGS__)); \
> > > > -       if (sizeof(_______STR) > 3)                     \
> > > > +       char str[] = __stringify((__VA_ARGS__));        \
> > > > +       if (sizeof(str) > 3)                    \
> > > >                 do_trace_printk(fmt, ##__VA_ARGS__);    \
> > > >         else                                            \
> > > >                 trace_puts(fmt);                        \
> > > >  } while (0)
> > > >  
> > > > +#define trace_printk(fmt, ...) \
> > > > +       ___trace_printk(fmt, __UNIQUE_ID(str), ##__VA_ARGS__)
> > > > +
> > > >  #define do_trace_printk(fmt, args...)                                  
> > > > \
> > > >  do {                                                                   
> > > > \
> > > >         static const char *trace_printk_fmt __used                      
> > > > \  
> > >   
> 

Reply via email to