From 991f35e193da0ccda9b8a1cc40ccdfdcaa77db0f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Feb 2026 18:41:06 +0000 Subject: [PATCH] perf: Use stack allocation in drawstatusbar Reduces memory allocation overhead in the frequent status bar update path by using a stack buffer for strings up to 1024 bytes. Fallbacks to malloc for larger strings. Co-authored-by: paperbenni <15818888+paperbenni@users.noreply.github.com> --- .jules/bolt.md | 3 +++ bar.c | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..ce312a03 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-02-01 - Stack Allocation for Status Bar +**Learning:** `drawstatusbar` in `bar.c` is a hot path called frequently. It was using `malloc` for a string buffer (`stext`) that is globally bounded to 1024 bytes. +**Action:** Use stack allocation (`char buf[1024]`) for strings <= 1024 bytes to avoid malloc overhead and fragmentation. Add a fallback to `malloc` for safety. diff --git a/bar.c b/bar.c index 8476144a..aae67621 100644 --- a/bar.c +++ b/bar.c @@ -54,13 +54,18 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { int cmdcounter; short isCode = 0; char *text; - char *p; + char *p = NULL; + char buf[1024]; len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { - die("malloc"); + if (len > sizeof(buf)) { + if (!(text = (char *)malloc(sizeof(char) * len))) { + die("malloc"); + } + p = text; + } else { + text = buf; } - p = text; memcpy(text, stext, len); /* compute width of the status text */ @@ -194,7 +199,8 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p) + free(p); return ret; }