diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..bf7f31a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-30 - Heap Allocation in Hot Path +**Learning:** `drawstatusbar` in `bar.c` performs a `malloc` and `free` on every call to render the status text. This function is called frequently (e.g., on every status update or window focus change). Since the status text buffer is globally fixed at 1024 bytes, this allocation is unnecessary and adds overhead/fragmentation. +**Action:** Replace `malloc` with a stack-allocated buffer (small string optimization) for bounded strings in frequent rendering paths. diff --git a/bar.c b/bar.c index 8476144..b29e1f4 100644 --- a/bar.c +++ b/bar.c @@ -56,9 +56,14 @@ 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))) { - die("malloc"); + if (len > sizeof(buf)) { + if (!(text = (char *)malloc(sizeof(char) * len))) { + die("malloc"); + } + } else { + text = buf; } p = text; memcpy(text, stext, len); @@ -194,7 +199,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p != buf) { + free(p); + } return ret; }