diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..b4fe5fa --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-02-02 - Heap Allocation in Hot Path (drawstatusbar) +**Learning:** The `drawstatusbar` function allocates memory via `malloc` on every call to process the status text. Since the status text is typically small (capped at 1024 bytes by `stext`) and updated frequently (e.g., every second), this creates unnecessary heap churn. +**Action:** Use a stack buffer (Small String Optimization) for typical sizes and fallback to heap only when necessary. This reduces allocator overhead in the rendering loop. diff --git a/bar.c b/bar.c index 8476144..5c63a8d 100644 --- a/bar.c +++ b/bar.c @@ -55,12 +55,15 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { short isCode = 0; char *text; char *p; + char buf[1024]; len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { + if (len < sizeof(buf)) { + p = buf; + } else if (!(p = (char *)malloc(sizeof(char) * len))) { die("malloc"); } - p = text; + text = p; memcpy(text, stext, len); /* compute width of the status text */ @@ -194,7 +197,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p != buf) { + free(p); + } return ret; }