-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing strokeStyle and fillStyle of drawing context slow #2011
Comments
Do you think you can create a simple sketch that we can stress test this problem on? Something that maybe calls |
Ok yes I'll put that together now. I've also realized that things are still worse when in TRIANGLES mode because the renderer keeps re-setting the fill and the stroke for each triangle. This has the advantage that you can in one This is trivially fixed by just defining a shape to have a single colour. The more general approach would be similar: caching the stroke and fill and not actually modifying the drawingContext if they've not changed, but it becomes important that nobody externally accesses the drawingContext except through these methods. |
Ok so I've got a tentative fix up as a PR here: #2021 Here's an example sketch where I get huge framerate improvement with this change: https://gist.github.com/wxs/d95a1239fad5cafd8eacf8417d2a137f and here are two profiler screen caps showing the difference as you can see, the original slow version is spending a huge amount of time just getting the current stroke or fill in the |
Nature of issue?
Most appropriate sub-area of p5.js?
Which platform were you using when you encountered this?
Details about the bug:
In profiling a sketch I'm working on that makes heavy use of
vertex
, I found that calls were quite slow, and specifically due to the calls internally to_getStroke
and_getFill
(see screenshot).It seems that its slow for it to access the
fillStyle
andstrokeStyle
of the drawing context. Although I don't know why this would be the case, I found that by locally caching these values, and then updating the cache in every place where they are set, performance improves significantly:this will, of course, fail if someone tries to modify the stroke or fill style outside of these helper functions. There may be another solution by modifying the part of the
vertex
that calls this.If the helper function-based approach looks good, I can probably package up my changes as a PR. Let me know!
The text was updated successfully, but these errors were encountered: