From d338d163fc5deb139971763588d3b1efcc0c8c0d Mon Sep 17 00:00:00 2001 From: CodSpeed Bot Date: Sun, 2 Aug 2026 08:41:02 +0000 Subject: [PATCH] perf(callgrind): write cost lines straight to the dump file fprint_cost() runs once per cost line of callgrind.out, i.e. tens of thousands of times for a medium sized profile. It went through CLG_(mappingcost_as_string)(), which builds the line in a freshly allocated XArray grown one character at a time, strdup()s it, prints it and frees both -- roughly a dozen heap operations per line for a string thrown away immediately. Format the event costs directly into the VgFile's output buffer instead, using the same value and zero-run-compression logic. The emitted bytes are identical. CLG_(mappingcost_as_string)() is kept for the non-hot callers (summary/totals lines, log output). --- callgrind/dump.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/callgrind/dump.c b/callgrind/dump.c index b1e7eab53..27f9c6d25 100644 --- a/callgrind/dump.c +++ b/callgrind/dump.c @@ -609,9 +609,38 @@ void fprint_pos(VgFile *fp, const AddrPos* curr, const AddrPos* last) static void fprint_cost(VgFile *fp, const EventMapping* es, const ULong* cost) { - HChar *mcost = CLG_(mappingcost_as_string)(es, cost); - VG_(fprintf)(fp, "%s\n", mcost); - CLG_FREE(mcost); + /* This is the innermost part of the dump: it runs once per written + * cost line, i.e. tens of thousands of times for a medium sized + * profile. Format the events straight into the output file's buffer. + * + * Going via CLG_(mappingcost_as_string)() instead would build the very + * same line in a freshly allocated XArray that is grown one character + * at a time, VG_(strdup)() it, print it and then free both -- roughly + * a dozen heap operations per line, for a string that is thrown away + * immediately. The bytes written are identical. + */ + Int i, skipped = 0; + + if (!cost || es->size == 0) { + VG_(fprintf)(fp, "\n"); + return; + } + + /* At least one entry */ + VG_(fprintf)(fp, "%llu", cost[es->entry[0].offset]); + + for(i = 1; i < es->size; i++) { + if (cost[es->entry[i].offset] == 0) { + skipped++; + continue; + } + while(skipped > 0) { + VG_(fprintf)(fp, " 0"); + skipped--; + } + VG_(fprintf)(fp, " %llu", cost[es->entry[i].offset]); + } + VG_(fprintf)(fp, "\n"); }