From 21fa0be2714ece22d720f18d0436898ddf08ccb4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 02:20:34 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20color=20map=20to=20?= =?UTF-8?q?avoid=20per-plugin=20allocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, the `colors.ColorMap()` function creates and returns a new map for every plugin invocation. Because plugins are evaluated frequently and potentially concurrently in a loop, this leads to unnecessary memory allocations and garbage collection overhead. This change caches the color map lazily within the `StatusLine` struct using `sync.Once`. Impact: Eliminates one map allocation per plugin execution. Measurement: This is a micro-optimization preventing repeated map instantiations for static data, resulting in less heap pressure and faster plugin execution. Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/bolt.md | 3 +++ internal/statusline/statusline.go | 11 ++++++++++- 2 files changed, 13 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..95c39ed --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-07 - Avoid Per-Execution Map Allocations +**Learning:** Repeatedly creating map structures (like `colors.ColorMap()`) during per-plugin execution within a parallel loop causes excessive memory allocation and performance overhead. +**Action:** Cache static map data at the instance level (e.g., `StatusLine` struct) during initialization using `sync.Once` to ensure thread-safety, minimize allocations, and maintain backward compatibility. diff --git a/internal/statusline/statusline.go b/internal/statusline/statusline.go index ebbc115..0c6de07 100644 --- a/internal/statusline/statusline.go +++ b/internal/statusline/statusline.go @@ -35,6 +35,8 @@ type StatusLine struct { isIdle bool bashPlugins []plugin.Plugin // Cached discovered bash plugins bashPluginsOnce sync.Once + colorMap map[string]string + colorMapOnce sync.Once } // New creates a new StatusLine renderer @@ -611,6 +613,13 @@ func (sl *StatusLine) getConfigBool(key string, defVal bool) bool { return v } +func (sl *StatusLine) getColorMap() map[string]string { + sl.colorMapOnce.Do(func() { + sl.colorMap = colors.ColorMap() + }) + return sl.colorMap +} + func (sl *StatusLine) runPlugin(name string) string { // Build plugin input input := plugin.Input{ @@ -634,7 +643,7 @@ func (sl *StatusLine) runPlugin(name string) string { ContextWindowSize: sl.input.Context.ContextWindow, }, Config: sl.getPluginConfig(name), - Colors: colors.ColorMap(), + Colors: sl.getColorMap(), } // Try native plugin first (much faster - no subprocess)