Skip to content

Fixes #9000#9014

Open
NalinDalal wants to merge 2 commits into
processing:mainfrom
NalinDalal:main
Open

Fixes #9000#9014
NalinDalal wants to merge 2 commits into
processing:mainfrom
NalinDalal:main

Conversation

@NalinDalal

Copy link
Copy Markdown
Contributor

Resolves #9000

set() on p5.Graphics was significantly slower in 2.x vs 1.11.x because
_getRGBA did a full to('srgb') + structuredClone + map() on every
call, even when the color was already in RGB mode and being reused in a
tight loop.

Changes:

  1. Cache _getRGBA results
    • Added #rgbaCache Map on Color, keyed by maxes string
    • First call computes and stores; subsequent calls with same maxes
      return a shallow copy of the cached array
    • Cache is invalidated in setRed, setGreen, setBlue, setAlpha,
      and the _color setter
  2. RGB-mode fast path
    • When this.mode === RGB, coords are already sRGB 0-1, so skip the
      to('srgb') conversion and use [...this._color.coords, this._color.alpha]
      directly
    • Non-RGB modes still go through colorjs conversion

AI usage disclosure: I used Kilo (an AI coding assistant) to help
implement and review this change. I understand and take responsibility for
every line of code in this PR. See [AI_USAGE_POLICY.md].

PR Checklist

@ksen0

ksen0 commented Jul 24, 2026

Copy link
Copy Markdown
Member

To show that this fixes the original issue, please report results from the linked benchmark test, at minimum. Thanks!

@NalinDalal

Copy link
Copy Markdown
Contributor Author

Confirmed the fix in 88e1550 — benchmark (100×100 buffer, 50 iterations, Brave):

  • Average: 2.41 ms
  • Min: 1.90 ms
  • This matches the v1.11.12 baseline of ~1.85 ms, down from the reported 12.79 ms in 2.3.0.
Screenshot 2026-07-25 at 12 59 39 PM

Hope this resolves your query @ksen0

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.

[p5.js 2.0+ Bug Report]: Large slowdown in p5.Graphics.set() compared to v1.11.x

2 participants