diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..7e0cbd3 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-01-25 - Stack vs Heap in Hot Paths +**Learning:** `drawstatusbar` in `bar.c` allocates and frees a buffer on every redraw. Since the buffer size is bounded by `stext` (1024 bytes), this is unnecessary overhead. +**Action:** Use stack allocation for small, bounded buffers in frequent operations to avoid allocator overhead and fragmentation. diff --git a/bar.c b/bar.c index 8476144..b521107 100644 --- a/bar.c +++ b/bar.c @@ -53,15 +53,15 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { int len; int cmdcounter; short isCode = 0; - char *text; - char *p; + char text_buf[1024]; + char *text = text_buf; len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { - die("malloc"); + if (len > sizeof(text_buf)) { + len = sizeof(text_buf); } - p = text; memcpy(text, stext, len); + text[len - 1] = '\0'; /* compute width of the status text */ w = 0; @@ -88,7 +88,7 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } else { isCode = 0; } - text = p; + text = text_buf; statuswidth = w; w += 2; /* 1px padding on both sides */ ret = x = m->ww - w - getsystraywidth(); @@ -194,7 +194,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); return ret; }