Skip to content

Conversation

aymanbagabas
Copy link
Member

This speeds up color conversion by caching the results of color conversion. Since for each colorprofile, the conversion is the same for each color, we can cache the results of the conversion to improve performance.

This is currently noticeable when you run Bubble Tea v2 examples like examples/splash, examples/space, and examples/doom-fire with a profile that needs conversion.

COLORTERM= TERM=xterm go run ./splash

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

A PR to improve the performance of color conversion by caching its results.

  • Introduces a package-level cache using a map to store conversion results.
  • Modifies the Convert method to check for cached values and memoize new conversions.
  • Adds concurrency control using sync.Once and sync.Mutex to protect cache access.

@aymanbagabas aymanbagabas changed the title optim: cache color conversion perf: cache color conversion Apr 25, 2025
@aymanbagabas aymanbagabas requested a review from Copilot April 25, 2025 16:17
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a caching mechanism to speed up color conversion by storing already computed conversion results.

  • Added a global cache map along with sync.Once and sync.Mutex for initializing and accessing the cache concurrently.
  • Updated the Convert method to check and store conversion results to improve performance in frequently used color conversion scenarios.

This speeds up color conversion by caching the results of
color conversion. Since for each colorprofile, the conversion
is the same for each color, we can cache the results of
the conversion to improve performance.

This is currently noticeable when you run Bubble Tea v2 examples like
`examples/splash`, `examples/space`, and `examples/doom-fire` with a
profile that needs conversion.

```go
COLORTERM= TERM=xterm go run ./splash
```
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a caching mechanism to speed up color conversion by storing previously computed conversions.

  • Added a global cache and mutex to handle concurrent access
  • Updated the Convert method to check and update the cache

@aymanbagabas aymanbagabas requested a review from Copilot April 25, 2025 16:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves performance by caching the results of color conversion to reduce redundant computations, particularly for profiles requiring conversion.

  • Introduces a global cache variable and a read/write mutex for safe concurrent access.
  • Refactors the Convert function to first check for a cached value and then, using a deferred block, cache the conversion result if not previously stored.
Comments suppressed due to low confidence (1)

profile.go:55

  • [nitpick] Consider renaming the return variable 'cc' to a more descriptive name like 'convertedColor' to improve code clarity.
func (p Profile) Convert(c color.Color) (cc color.Color) {

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves performance in color conversion by caching conversion results for each color profile. Key changes include updates to test cases and types in profile_test.go, the addition of a dedicated caching test, and modifications to the Convert method in profile.go to implement caching.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
profile_test.go Updated tests to reflect new types and added a caching test.
profile.go Refactored the Convert method to include caching logic and removed obsolete conversion code.
Files not reviewed (1)
  • go.mod: Language not supported
Comments suppressed due to low confidence (1)

profile.go:52

  • [nitpick] Consider using a more descriptive name than 'cc' (e.g., 'convertedColor') to improve code readability.
func (p Profile) Convert(c color.Color) (cc color.Color) {

@aymanbagabas aymanbagabas merged commit f7fa2ac into main Apr 29, 2025
42 of 54 checks passed
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.

2 participants