perf list: Avoid repeated strlen when escaping JSON strings
PATCH 1/1 perf list: Avoid repeated strlen when escaping JSON strings
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.
--- tools/perf/builtin-list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-list.c +1 -1
@@ -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" );
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
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
Quoted 6 lines
[ ... ]
Codex assisted with identifying the redundant scan, preparing the change, and running the comparisons.Assisted-by LLMSigned-off-by Qerogram <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · Qerogram
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.
Thanks, applied to perf-tools-next, for v7.4.
- Arnaldo