From 40d6e03c1c60dba2b4afac2c1a41347c41a30e39 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 25 Jan 2026 18:50:36 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Use=20stack=20allocation=20?= =?UTF-8?q?for=20status=20bar=20drawing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces dynamic `malloc` allocation with a fixed-size stack buffer in `drawstatusbar`. The buffer size is set to 1024 to match the global `stext` definition in `instantwm.c`. This optimization reduces heap allocation overhead in the frequent drawing path. Also fixes a potential stack buffer overflow by clamping `len`. --- .jules/bolt.md | 3 +++ bar.c | 13 ++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..7e0cbd35 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-25 - Stack vs Heap in Hot Paths +**Learning:** `drawstatusbar` in `bar.c` allocates and frees a buffer on every redraw. Since the buffer size is bounded by `stext` (1024 bytes), this is unnecessary overhead. +**Action:** Use stack allocation for small, bounded buffers in frequent operations to avoid allocator overhead and fragmentation. diff --git a/bar.c b/bar.c index 8476144a..b5211074 100644 --- a/bar.c +++ b/bar.c @@ -53,15 +53,15 @@ 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; 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[len - 1] = '\0'; /* compute width of the status text */ w = 0; @@ -88,7 +88,7 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } else { isCode = 0; } - text = p; + text = text_buf; statuswidth = w; w += 2; /* 1px padding on both sides */ ret = x = m->ww - w - getsystraywidth(); @@ -194,7 +194,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); return ret; }