-
Notifications
You must be signed in to change notification settings - Fork 981
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[react-native]: Memoized Blocks Component to free up UI thread. #3814
Conversation
🦋 Changeset detectedLatest commit: 5533720 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
View your CI Pipeline Execution ↗ for commit 5533720.
☁️ Nx Cloud last updated this comment at |
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.
Awesome work @yash-builder
)); | ||
|
||
// Optional: Give the component a display name for better debugging | ||
RenderBlock.displayName = 'RenderBlock'; |
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.
🎯 suggestion:
Should we append block.id
to the displayName as a identifier ?
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.
We can surely do that. But in this scenario I used this identifier for inspecting the component in React Devtools
}; | ||
|
||
// Moved outside and memoized | ||
const RenderBlock = memo(({ |
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.
❔question:
We have used memo
over Block component ( to get RenderBlock ) and then composed the RenderBlock
with React.useCallback
. Could you explain the logic here and how it works internally ?
- what happens if we use
memo
/useCallback
in both places ?
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.
Great question!
- If Only
memo
Is Used:
RenderBlock
will avoid re-renders, butFlatList
may still recreate the renderItem function on every render. This may lead to potential performance bottleneck. - If Only
useCallback
Is Used:
TherenderItem
function will be stable, butRenderBlock
will re-render if its props are shallowly different, even when not required.
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.
Very well done! I'm Rewriting here what I sent you in Slack so we don't lose track of it.
One important thing is not to use overrides/*
unless as a last resort because the file will quickly go out of sync with the original Blocks.lite.tsx
, forcing us to maintain multiple copies of the same logic.
Here is a Loom showing how I'd tackle the ~3-4 changes in your PR and incorporate them into Mitosis plugins instead:
https://www.loom.com/share/94f1767796bd487bb48be41d9046e01f
Also, you need to add a changesets entry, which you can do by following https://github.com/BuilderIO/builder/blob/main/packages/sdks/PUBLISHING.md
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.
Code looks good! You're still missing the changesets
entry https://github.com/BuilderIO/builder/blob/main/packages/sdks/PUBLISHING.md#1--add-changeset
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.
🎉
Description
Suspense
:Added
React.Suspense
to defer the rendering of theContent
component until it is fully loaded.Memoization
:React.memo
to memoize computationally expensive operations within theBlocks
component. To prevents unnecessary recalculations of processed blocks during re-renders, reducing the load on the UI thread.Screenshot
Before:
After: