From 8e3a265dfbb7e816ceb89377c5e8f24591b2b85f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 19 Jan 2026 19:01:22 +0000 Subject: [PATCH] Optimize drawstatusbar to use stack allocation Replaced malloc/free with a fixed-size stack buffer in `drawstatusbar`. The buffer size matches the global `stext` size (1024 bytes). This removes heap allocation overhead from the status bar drawing path, which is called frequently. Test Plan: 1. `make clean && make` to verify compilation. 2. Verified logic ensures no buffer overflow. --- .jules/bolt.md | 3 +++ bar.c | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..47158eab --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-19 - [Stack Allocation in drawstatusbar] +**Learning:** `drawstatusbar` in `bar.c` uses `malloc` for a temporary string buffer on every redraw. This is a hot path. The source string `stext` is a global fixed-size buffer of 1024 bytes. +**Action:** Replace `malloc` with a stack buffer `char text[1024]` to avoid heap allocation overhead and potential fragmentation. This is safe because the input size is bounded by the global `stext` size. diff --git a/bar.c b/bar.c index 8476144a..37eec5e0 100644 --- a/bar.c +++ b/bar.c @@ -53,15 +53,16 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { int len; int cmdcounter; short isCode = 0; - char *text; - char *p; + char text_buf[1024]; + char *text = text_buf; + char *p = text_buf; len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { - die("malloc"); + if (len > sizeof(text_buf)) { + len = sizeof(text_buf); } - p = text; memcpy(text, stext, len); + text[sizeof(text_buf) - 1] = '\0'; /* compute width of the status text */ w = 0; @@ -194,7 +195,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); return ret; }