[PATCH perf-tools-next] perf list: Avoid repeated strlen when escaping JSON strings
From: Qerogram <hidden>
Date: 2026-09-10 12:48:52
Also in:
lkml
Subsystem:
performance events subsystem, the rest · Maintainers:
Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Linus Torvalds
The %S conversion in fix_escape_fprintf() calls strlen(s) in the loop condition while escaping each character. GCC 14.2.0 retains this call inside the loop in a normal DEBUG=0 (-O3) perf build, repeatedly scanning the whole string. Test the current character for NUL instead. This processes the same bytes without rescanning the string. Escaping and buffer growth are unchanged. Full-command wall-clock measurements on an Apple M5 Pro host running an aarch64 Linux container with GCC 14.2.0 and glibc 2.41 gave these medians: perf list --json metrics Before (ms) After (ms) Default catalog 0.5969 0.5855 PERF_CPUID=0x00000000410fd830 0.7818 0.7033 Each workload used 50 randomized before/after pairs. Each sample averages the time per invocation across five fresh processes, with stdout sent to /dev/null and no single-CPU pinning. PERF_CPUID selects the shipped V3 catalog on the same host; these are not measurements on V3 hardware. The measurements include process startup and do not establish a gain on other machines or perf commands. These observations come from one short run. Relinking also changes code layout: .text grows from 2,013,172 to 2,017,236 bytes in this build. The timings do not isolate scan removal from the changed layout. The existing list shell test passes for both binaries. Source-extracted functions linked to perf's strbuf implementation pass 906 equivalence cases with ASan and UBSan. Full-command outputs are byte-identical. Codex assisted with identifying the redundant scan, preparing the change, and running the comparisons. Assisted-by: LLM Signed-off-by: Qerogram <redacted> --- tools/perf/builtin-list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
index 50f69c2c0d51..4118424cb364 100644
--- a/tools/perf/builtin-list.c
+++ b/tools/perf/builtin-list.c@@ -325,7 +325,7 @@ static void fix_escape_fprintf(FILE *fp, struct strbuf *buf, const char *fmt, .. case 'S': { const char *s = va_arg(args, const char*); - for (size_t s_pos = 0; s_pos < strlen(s); s_pos++) { + for (size_t s_pos = 0; s[s_pos] != '\0'; s_pos++) { switch (s[s_pos]) { case '\n': strbuf_addstr(buf, "\\n");
--
2.55.0