From 9984741f78d46386023d8302a23f3bdfc8349a2b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 5 Feb 2026 18:43:56 +0000 Subject: [PATCH] perf(bar): use stack allocation for status bar text Optimizes `drawstatusbar` by allocating the status text buffer on the stack instead of the heap. This avoids `malloc`/`free` overhead on every status bar update (which can happen frequently). - Uses `char buf[1024]` (matching `stext` global size) for the fast path. - Falls back to `malloc` only if the text exceeds the buffer size. - Checks against `sizeof(buf)` to prevent overflows. Co-authored-by: paperbenni <15818888+paperbenni@users.noreply.github.com> --- .jules/bolt.md | 3 +++ bar.c | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..1d04f7ee --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-02-05 - Masked Global Variables in C +**Learning:** The global variable `stext` is masked by a function argument of the same name in `drawstatusbar(..., char *stext)`. This requires careful inspection of variable scope when optimizing, as `sizeof(stext)` yields different results (pointer size vs array size) depending on the scope. +**Action:** Always verify variable declarations and scope before applying size-based optimizations or `sizeof` checks in C codebases. diff --git a/bar.c b/bar.c index 8476144a..95ba42ba 100644 --- a/bar.c +++ b/bar.c @@ -56,8 +56,12 @@ 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))) { + if (len < sizeof(buf)) { + text = buf; + } else if (!(text = (char *)malloc(sizeof(char) * len))) { die("malloc"); } p = text; @@ -194,7 +198,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p != buf) { + free(p); + } return ret; }