-
-
Notifications
You must be signed in to change notification settings - Fork 237
perf(react): improve react compiler preset so that slightly more modules are filtered out #1138
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
sapphi-red
wants to merge
1
commit into
main
Choose a base branch
from
perf/react-filter-out-more-modules-that-doesnt-contain-hooks
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.
Open
Changes from all commits
Commits
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think the only way to define a function without a whitespace before is
const A,B= ...which I don't know why you would do that in anyworld for defining hooks or componentsBut I think the better tradeoff is always running on
jsx|tsx|mdx(in a normal codebase, you have like 98/99% of jsx files that contrains components, so not sure trying to get a 2% improvement with a regex is worth it) and run onjs|tsonly with when code matches\suse[A-Z0-9](Vite always ban using JSX inside JS, nobody writes manualjsxcalls and plugin should not rename file ids once transform from jsx to jsThe only issue with the approach if for people having custom DSL that compile to React, but I think these frameworks should provide their own react compiler preset
Also can you put the link from the PR into the source code? I think it's good to have it as a reference if someone challenges it 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.
If it's possible to have
,before the name, I think we can use[\s,]. I guess\sand[\s,]won't have much perf difference.It's currently difficult to achieve, but it would be easier to do once Vite implements composable filters (vitejs/rolldown-vite#605).
Uh oh!
There was an error while loading. Please reload this page.
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.
After some AI checking, I think this is pretty safe and code not matching this regex will certainly break a lot a React rules:
(?:const|let|var|function)\s+(?:[A-Z]|use[A-Z0-9])(The way to break it is using
new Functionor putting your component in a class method, both sounds horrible)Speaking of classes, I checked and the playground doesn't touch class component, so no need to match this
Edit: Ok no actually this would be valid IMO but the playground doesn't touch it:
I think the React compiler is only looking for top-level functions, and this sounds reasonable, so we can make an more aggressive regex
Uh oh!
There was an error while loading. Please reload this page.
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.
I guess
new Functioncannot be handled by the compiler anyways. Classes (& class methods) are skipped here.In
compilationMode: 'all', it seems to only look for top-level ones. But I guess it does otherwise.Something like this won't match the regex you suggested, while the compiler optimizes:
playground
This case matches this part.
That said, I think we can ignore this case if the speed up is big enough.
Uh oh!
There was an error while loading. Please reload this page.
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.
It feels quite unexpected that the component is optimized only if wrap a in a specific function.
We could also add
["']react['"]to the regex in that case, it should cover a lot of extra cases like this while still skipping files that are unrelated to React