From 8f84ca70acfd276c3db8086b89ec2c2d74f70a4c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 Jan 2026 18:54:22 +0000 Subject: [PATCH] perf(bar): use stack buffer in drawstatusbar to avoid malloc overhead Replaces dynamic allocation with a fixed-size stack buffer (1024 bytes) for status text processing. This eliminates malloc/free calls in the frequent rendering path (drawbar). A fallback to malloc is included for safety if the status text exceeds the buffer size. - Adds `char buf[1024]` stack buffer. - Adds logic to use stack buffer by default. - Adds `use_malloc` flag to handle cleanup correctly. - Adds comment explaining the optimization. Verification: - Build passes with `make clean && make`. - Logic handles both stack and heap paths correctly. Co-authored-by: paperbenni <15818888+paperbenni@users.noreply.github.com> --- bar.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bar.c b/bar.c index 8476144a..876dc059 100644 --- a/bar.c +++ b/bar.c @@ -55,10 +55,18 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { short isCode = 0; char *text; char *p; + /* Optimization: Use stack buffer for status text to avoid malloc overhead */ + 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 +202,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (use_malloc) { + free(p); + } return ret; }