Skip to content

⚡ Bolt: Cache color map to avoid per-plugin allocations#90

Closed
himattm wants to merge 1 commit intomainfrom
bolt/cache-colormap-7323222668004272678
Closed

⚡ Bolt: Cache color map to avoid per-plugin allocations#90
himattm wants to merge 1 commit intomainfrom
bolt/cache-colormap-7323222668004272678

Conversation

@himattm
Copy link
Copy Markdown
Owner

@himattm himattm commented May 7, 2026

💡 What: Caches the map generated by colors.ColorMap() lazily within the StatusLine struct using sync.Once.
🎯 Why: runPlugin() previously invoked colors.ColorMap() on every execution, leading to unnecessary repeated map allocations in memory since the color constants are static.
📊 Impact: Eliminates one map allocation per plugin execution within the concurrent rendering loop, reducing garbage collection overhead and heap pressure.
🔬 Measurement: Verify that go test ./... passes to ensure plugin configuration parsing behaves identically. The code is structured similarly to caching the bash plugins (sync.Once).


PR created automatically by Jules for task 7323222668004272678 started by @himattm

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>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes performance by caching the color map in the StatusLine struct using sync.Once to avoid repeated allocations during parallel plugin execution. However, the review identifies a potential data race because the shared map is a reference type; if concurrent plugins modify the map, it will cause thread-safety issues. It is recommended to treat the map as read-only or use a struct to pass color data by value.

Comment on lines +616 to +621
func (sl *StatusLine) getColorMap() map[string]string {
sl.colorMapOnce.Do(func() {
sl.colorMap = colors.ColorMap()
})
return sl.colorMap
}
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.

@himattm
Copy link
Copy Markdown
Owner Author

himattm commented May 7, 2026

Closing as superseded by direct commit 42f3149 on main, which consolidated the StatusLine color-map caching optimization without the generated .jules note.

@himattm himattm closed this May 7, 2026
@himattm himattm deleted the bolt/cache-colormap-7323222668004272678 branch May 7, 2026 15:34
@google-labs-jules
Copy link
Copy Markdown
Contributor

Closing as superseded by direct commit 42f3149 on main, which consolidated the StatusLine color-map caching optimization without the generated .jules note.

Understood. Acknowledging that this work is superseded by a direct commit on main, and stopping work on this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant