From 7157954cc8e34ea4eca6b6781065ba48e1ca3e36 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 18 Jan 2026 18:38:15 +0000 Subject: [PATCH] perf(bar): optimize drawstatusbar with stack allocation (SSO) Replaces always-malloc approach with Small String Optimization (SSO). Uses a 1KB stack buffer for typical status strings to avoid allocator overhead in the hot rendering path, falling back to malloc only for larger strings. --- .jules/bolt.md | 3 +++ bar.c | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..fca09d1a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-10-23 - Blocking Animations +**Learning:** The `animateclient` function uses `usleep(15000)` inside a `while` loop, which blocks the single-threaded X11 event loop. This causes the entire window manager to freeze during any window animation. +**Action:** Future optimizations should focus on refactoring this to use an event-based non-blocking animation system, possibly using a timer or `XNextEvent` with timeouts. diff --git a/bar.c b/bar.c index 8476144a..84deaa00 100644 --- a/bar.c +++ b/bar.c @@ -56,9 +56,19 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { char *text; char *p; + /* Optimization: Use stack allocation for common status text lengths (<1024) + to avoid malloc overhead in hot path. Fallback to malloc for larger strings. */ + char buf[1024]; + int use_malloc = 0; + 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"); + } + use_malloc = 1; + } else { + text = buf; } p = text; memcpy(text, stext, len); @@ -194,7 +204,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (use_malloc) { + free(p); + } return ret; }