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)