-
Notifications
You must be signed in to change notification settings - Fork 22
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
Perf: Cache styles to avoid creating new object style every execution #250
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- packages/mobile/src/styles/builder/builder.ts (1 hunks)
Additional comments not posted (7)
packages/mobile/src/styles/builder/builder.ts (7)
180-180
: Sorting definitions improves cache consistency.Sorting the definitions ensures that the order of input doesn't affect the cache key, leading to consistent key generation.
182-185
: Key generation logic is sound.The key generation logic concatenates sorted definitions into a single string, which is used as the cache key. This approach is effective for ensuring unique keys for different style combinations.
186-187
: Check for existing cached styles.The code checks if the styles are already cached using the generated key. If found, it returns the cached styles, improving performance by avoiding redundant calculations.
190-193
: Merge base definitions into styles.The loop merges base definitions into the
styles
object. Ensure that theget
method returns valid style objects.
197-200
: Merge conditional definitions into styles.The loop merges conditional definitions into the
styles
object if they are not null, undefined, or true. Ensure that theget
method handles these cases correctly.
204-204
: Cache the computed styles.The computed styles are cached using the generated key, ensuring that future requests for the same styles are served from the cache.
205-205
: Return the computed styles.The method returns the computed styles, either from the cache or newly computed.
const allDefinitions = [ | ||
...(definitions || []), | ||
...(conditionalDefinitions || []), | ||
].filter((definition) => typeof definition === "string"); |
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.
Ensure definitions are not null or undefined.
The code concatenates definitions
and conditionalDefinitions
and filters out non-string values. Ensure that definitions
and conditionalDefinitions
are not null or undefined before concatenation.
- const allDefinitions = [
- ...(definitions || []),
- ...(conditionalDefinitions || []),
- ].filter((definition) => typeof definition === "string");
+ const allDefinitions = [
+ ...(definitions ?? []),
+ ...(conditionalDefinitions ?? []),
+ ].filter((definition) => typeof definition === "string");
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const allDefinitions = [ | |
...(definitions || []), | |
...(conditionalDefinitions || []), | |
].filter((definition) => typeof definition === "string"); | |
const allDefinitions = [ | |
...(definitions ?? []), | |
...(conditionalDefinitions ?? []), | |
].filter((definition) => typeof definition === "string"); |
); | ||
let styles = this.cached.get(keyStyle); | ||
if (styles) return styles; | ||
|
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.
Ensure styles initialization.
Before merging styles, ensure that the styles
variable is properly initialized.
- let styles = this.cached.get(keyStyle);
- if (styles) return styles;
+ let styles = this.cached.get(keyStyle) || {};
+ if (Object.keys(styles).length > 0) return styles;
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let styles = this.cached.get(keyStyle) || {}; | |
if (Object.keys(styles).length > 0) return styles; |
On every render, the
flatten
method will be executed, returning the appropriate styles fromStyleSheet.flatten
. However,StyleSheet.flatten
creates and returns a new object on each execution, as seen here:https://github.com/facebook/react-native/blob/8408b8bc96db15e265ca65fce7875ee65dcfdcec/packages/react-native/Libraries/StyleSheet/flattenStyle.js#L29
To avoid this, we can cache the created styles and reuse them if the same style is needed again. This approach provides the same benefits as using
StyleSheet.create
or declaring a styles object outside of the render method.Summary by CodeRabbit