mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
perf: Cache normalisedColors output (#57896)
Summary: While looking into something I saw this processColor function , for every color we are calculating normalise Color even though it would have been done earlier. This is a simple approach. Maybe we can add things like LRU. With some simple scripts(Rendering around 1000 cells) I found around 13% faster . ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [GENERAL][FIXED] - use cached results for already normalised colors Pull Request resolved: https://github.com/react/react-native/pull/57896 Test Plan: Tested in RN tester. Reviewed By: christophpurrer Differential Revision: D115779590 Pulled By: Abbondanzo fbshipit-source-id: a915bc641fbaff1055c18ac1efa20552a725102f
This commit is contained in:
1 parent
bc35168d1b
commit
092734772a
1 file changed
+19
@@ -12,6 +12,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const cachedColors = new Map();
|
||||
|
||||
function normalizeColor(color) {
|
||||
if (typeof color === 'number') {
|
||||
if (color >>> 0 === color && color >= 0 && color <= 0xffffffff) {
|
||||
@@ -24,6 +26,23 @@ function normalizeColor(color) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (cachedColors.has(color)) {
|
||||
// Map iteration order follows insertion order, so re-inserting on every
|
||||
// hit keeps the least-recently-used entry first.
|
||||
const cachedColor = cachedColors.get(color);
|
||||
cachedColors.delete(color);
|
||||
cachedColors.set(color, cachedColor);
|
||||
return cachedColor;
|
||||
}
|
||||
const normalizedColor = parseColorString(color);
|
||||
if (cachedColors.size >= 1024) {
|
||||
cachedColors.delete(cachedColors.keys().next().value);
|
||||
}
|
||||
cachedColors.set(color, normalizedColor);
|
||||
return normalizedColor;
|
||||
}
|
||||
|
||||
function parseColorString(color) {
|
||||
const matchers = getMatchers();
|
||||
let match;
|
||||
|
||||
|
||||
Reference in new issue
Block a user