Skip to content

Commit 4575109

Browse files
tititiou36brauner
authored andcommitted
seq_file: Optimize seq_puts()
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_putc() or 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 3436 $ git grep seq_puts.*\".\" | wc -l 84 Signed-off-by: Christophe JAILLET <[email protected]> Link: https://lore.kernel.org/r/a8589bffe4830dafcb9111e22acf06603fea7132.1713781332.git.christophe.jaillet@wanadoo.fr Signed-off-by: Christian Brauner <[email protected]> The output for seq_putc() generation has also be checked and works.
1 parent 0a960ba commit 4575109

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

fs/seq_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ void seq_putc(struct seq_file *m, char c)
669669
}
670670
EXPORT_SYMBOL(seq_putc);
671671

672-
void seq_puts(struct seq_file *m, const char *s)
672+
void __seq_puts(struct seq_file *m, const char *s)
673673
{
674674
int len = strlen(s);
675675

@@ -680,7 +680,7 @@ void seq_puts(struct seq_file *m, const char *s)
680680
memcpy(m->buf + m->count, s, len);
681681
m->count += len;
682682
}
683-
EXPORT_SYMBOL(seq_puts);
683+
EXPORT_SYMBOL(__seq_puts);
684684

685685
/**
686686
* seq_put_decimal_ull_width - A helper routine for putting decimal numbers

include/linux/seq_file.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,18 @@ void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
118118
__printf(2, 3)
119119
void seq_printf(struct seq_file *m, const char *fmt, ...);
120120
void seq_putc(struct seq_file *m, char c);
121-
void seq_puts(struct seq_file *m, const char *s);
121+
void __seq_puts(struct seq_file *m, const char *s);
122+
123+
static __always_inline void seq_puts(struct seq_file *m, const char *s)
124+
{
125+
if (!__builtin_constant_p(*s))
126+
__seq_puts(m, s);
127+
else if (s[0] && !s[1])
128+
seq_putc(m, s[0]);
129+
else
130+
seq_write(m, s, __builtin_strlen(s));
131+
}
132+
122133
void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
123134
unsigned long long num, unsigned int width);
124135
void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,

0 commit comments

Comments
 (0)