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-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.
11 changes: 10 additions & 1 deletion internal/statusline/statusline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Comment on lines +616 to +621
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Sharing the same map instance across multiple concurrent plugin executions introduces a potential data race. In Go, maps are reference types, and since renderLine executes plugins in parallel goroutines, any native plugin that modifies the Colors map will cause a race condition and affect other plugins running concurrently.

While this optimization reduces allocations, it sacrifices the isolation provided by the previous implementation (which created a fresh map per plugin). To maintain safety, ensure that all native plugins treat the Colors map as read-only. Alternatively, if you want to avoid allocations entirely and ensure safety, consider changing the Colors field in plugin.Input to a struct, which would be passed by value and thus be inherently thread-safe.


func (sl *StatusLine) runPlugin(name string) string {
// Build plugin input
input := plugin.Input{
Expand All @@ -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)
Expand Down
Loading