diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..ce312a0 --- /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 8476144..aae6762 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; }