Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 11 additions & 1 deletion internal/statusline/statusline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getColors method is missing a docstring. Additionally, the existing comment for discoverBashPlugins (currently on line 54) has become misplaced due to the insertion of this new method. Please add a proper docstring for getColors and ensure the discoverBashPlugins comment is moved back to its correct position above that method.

// getColors returns the cached color map, initializing it on first call.
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()
Expand Down Expand Up @@ -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)
Expand Down
Loading