-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Memoize Color Translations #8055
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
Open
robertclaus
wants to merge
2
commits into
main
Choose a base branch
from
memoize-color-translations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+33
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Memoize color specifier parsing, reducing the time to draw marker-heavy SVG `scatter` traces by roughly a third [[#8055](https://github.com/plotly/plotly.js/pull/8055)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,31 @@ const parse = (cstr, silent) => { | |
| return c; | ||
| }; | ||
|
|
||
| // `stroke` and `fill` below run once per data point, and every point of a trace | ||
| // normally repeats the same specifier, so parsing it each time is pure overhead. | ||
| // Memoize the two values they derive from it, keyed on the specifier itself. | ||
| // Only strings are cached, since they are the only specifiers we can key on. | ||
| const MAX_MEMO_SIZE = 1000; | ||
|
|
||
| const memoize = (fn) => { | ||
| const cache = new Map(); | ||
|
|
||
| return (cstr) => { | ||
| if (typeof cstr !== 'string') return fn(cstr); | ||
|
|
||
| let value = cache.get(cstr); | ||
| if (value === undefined) { | ||
| value = fn(cstr); | ||
| // Stop growing rather than evicting: a graph only ever uses a | ||
| // handful of distinct colors, so a full cache means array-valued | ||
| // colors, which repeat too little to be worth tracking. | ||
| if (cache.size < MAX_MEMO_SIZE) cache.set(cstr, value); | ||
| } | ||
|
|
||
| return value; | ||
| }; | ||
| }; | ||
|
|
||
| // TODO: rename to `rgbString` to better describe return value | ||
| /** | ||
| * Convert any color specifier to a normalized `rgb(r, g, b)` string. | ||
|
|
@@ -98,7 +123,11 @@ const parse = (cstr, silent) => { | |
| * @param {*} cstr - Color specifier | ||
| * @return {String} | ||
| */ | ||
| const rgb = (cstr) => formatRgb({ ...parse(cstr), alpha: 1 }); | ||
| const rgb = memoize((cstr) => formatRgb({ ...parse(cstr), alpha: 1 })); | ||
|
|
||
| // The alpha channel of a specifier, memoized for the same reason as `rgb`. | ||
| // Unlike `opacity` this keeps `parse`'s treatment of missing colors (alpha 1). | ||
| const alphaOf = memoize((cstr) => parse(cstr).alpha); | ||
|
|
||
| /** | ||
| * Return the alpha channel of a color (0 if falsy). | ||
|
|
@@ -278,7 +307,7 @@ const contrast = (cstr, lightAmount, darkAmount) => { | |
| * @param {*} cstr - Color specifier | ||
| */ | ||
| const stroke = (s, cstr) => { | ||
| s.style({ stroke: rgb(cstr), 'stroke-opacity': parse(cstr).alpha }); | ||
| s.style({ stroke: rgb(cstr), 'stroke-opacity': alphaOf(cstr) }); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -288,7 +317,7 @@ const stroke = (s, cstr) => { | |
| * @param {*} cstr - Color specifier | ||
| */ | ||
| const fill = (s, cstr) => { | ||
| s.style({ fill: rgb(cstr), 'fill-opacity': parse(cstr).alpha }); | ||
| s.style({ fill: rgb(cstr), 'fill-opacity': alphaOf(cstr) }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively instead of the cash we may benchmark this option of by passing previous stroke and previous fill. Something like this: var prevStrokeSTR;
var prevStrokeRGB;
var prevStrokeAlpha;
function getStroke(cstr) {
if(prevStrokeSTR !== cstr) {
prevStrokeSTR = cstr;
prevStrokeRGB = rgb(cstr);
prevStrokeAlpha = parse(cstr).alpha;
}
return [prevStrokeRGB, prevStrokeAlpha];
}
const stroke = (s, cstr) => {
const v = getStroke(cstr);
s.style({ stroke: v[0], 'stroke-opacity': v[1] });
};
var prevFillSTR;
var prevFillRGB;
var prevFillAlpha;
function getFill(cstr) {
if(prevFillSTR !== cstr) {
prevFillSTR = cstr;
prevFillRGB = rgb(cstr);
prevFillAlpha = parse(cstr).alpha;
}
return [prevFillRGB, prevFillAlpha];
}
const fill = (s, cstr) => {
const v = getFill(cstr);
s.style({ fill: v[0], 'fill-opacity': v[1] });
}; |
||
| }; | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
MAX_MEMO_SIZEreached one could evict first half of the cache.You could split the cache into two separate Maps:
currentCacheandoldCache.currentCachefirst, thenoldCache(and move the item tocurrentCacheif found).currentCache.currentCachereaches the maximum capacity limit, you simply wipeoldCacheentirelyand swap the references:
This provides a near-instantaneous O(1) bulk eviction without any loops.