From aa7e9724f0da929e34d5f320185c02904052605f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 30 Jan 2026 18:51:15 +0000 Subject: [PATCH] perf(bar): replace malloc with stack buffer in drawstatusbar Refactors `drawstatusbar` to use a 1024-byte stack buffer for status text instead of heap allocation. This avoids frequent malloc/free calls during bar updates. - Adds `char buf[1024]` stack buffer. - Falls back to `malloc` only if text exceeds buffer size (safety). - Updates cleanup logic to conditionally free. - Adds journal entry in `.jules/bolt.md`. Co-authored-by: paperbenni <15818888+paperbenni@users.noreply.github.com> --- .jules/bolt.md | 3 +++ bar.c | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..bf7f31ae --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-30 - Heap Allocation in Hot Path +**Learning:** `drawstatusbar` in `bar.c` performs a `malloc` and `free` on every call to render the status text. This function is called frequently (e.g., on every status update or window focus change). Since the status text buffer is globally fixed at 1024 bytes, this allocation is unnecessary and adds overhead/fragmentation. +**Action:** Replace `malloc` with a stack-allocated buffer (small string optimization) for bounded strings in frequent rendering paths. diff --git a/bar.c b/bar.c index 8476144a..b29e1f49 100644 --- a/bar.c +++ b/bar.c @@ -56,9 +56,14 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { char *text; char *p; + 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"); + } + } else { + text = buf; } p = text; memcpy(text, stext, len); @@ -194,7 +199,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p != buf) { + free(p); + } return ret; }