Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 10 additions & 3 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down