diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..dfca091 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-05 - Cache instance-level static maps to prevent repeated allocations +**Learning:** Repeatedly creating static maps (like ANSI color maps) within concurrent operations (like plugin executions) causes unnecessary allocations and garbage collection overhead. +**Action:** Cache static data maps at the instance level (e.g., in the `StatusLine` struct) using `sync.Once` in a getter method to ensure thread-safety and maintain backward compatibility for tests. diff --git a/internal/statusline/statusline.go b/internal/statusline/statusline.go index ebbc115..2fde67d 100644 --- a/internal/statusline/statusline.go +++ b/internal/statusline/statusline.go @@ -35,6 +35,9 @@ type StatusLine struct { isIdle bool bashPlugins []plugin.Plugin // Cached discovered bash plugins bashPluginsOnce sync.Once + + colorsMap map[string]string + colorsMapOnce sync.Once } // New creates a new StatusLine renderer @@ -49,6 +52,13 @@ func New(input Input, cfg config.Config) *StatusLine { } // discoverBashPlugins discovers bash plugins once and caches them +func (sl *StatusLine) getColors() map[string]string { + sl.colorsMapOnce.Do(func() { + sl.colorsMap = colors.ColorMap() + }) + return sl.colorsMap +} + func (sl *StatusLine) discoverBashPlugins() []plugin.Plugin { sl.bashPluginsOnce.Do(func() { discovered, err := sl.pluginManager.Discover() @@ -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)