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-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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (typo): Use "fall back" (verb) instead of "fallback" in this sentence.

Here it’s used as a verb phrase (“to fall back to heap”), so it should read: “fall back to heap only when necessary.”

11 changes: 8 additions & 3 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down