From 4ee80204d979d5909e4e60f29ba4a0beda498842 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 3 May 2026 02:30:49 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20color=20map=20in=20?= =?UTF-8?q?StatusLine=20to=20reduce=20allocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/bolt.md | 3 +++ internal/statusline/statusline.go | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..bd11eb1 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Cache static maps at struct level to avoid allocation in performance-sensitive areas +**Learning:** In performance-sensitive areas (like color mapping for the status line where multiple sections run in parallel), avoid repeated map allocations per plugin execution. Generating the color map per execution causes unnecessary GC pressure. Global variables carry risk of shared mutable state and require expensive deep copying. +**Action:** Cache static data maps at the instance level (e.g., in the `StatusLine` struct) using `sync.Once` during initialization (e.g. within an accessor method) to ensure thread-safety, prevent nil map panics, minimize allocations, and preserve backward compatibility with struct literals. diff --git a/internal/statusline/statusline.go b/internal/statusline/statusline.go index ebbc115..a44542c 100644 --- a/internal/statusline/statusline.go +++ b/internal/statusline/statusline.go @@ -35,6 +35,16 @@ type StatusLine struct { isIdle bool bashPlugins []plugin.Plugin // Cached discovered bash plugins bashPluginsOnce sync.Once + colorsMap map[string]string + colorsOnce sync.Once +} + +// getColors returns the color map, initializing it once if needed +func (sl *StatusLine) getColors() map[string]string { + sl.colorsOnce.Do(func() { + sl.colorsMap = colors.ColorMap() + }) + return sl.colorsMap } // New creates a new StatusLine renderer @@ -634,7 +644,7 @@ func (sl *StatusLine) runPlugin(name string) string { ContextWindowSize: sl.input.Context.ContextWindow, }, Config: sl.getPluginConfig(name), - Colors: colors.ColorMap(), + Colors: sl.getColors(), } // Try native plugin first (much faster - no subprocess)