Skip to content
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(resolve): skip for virtual files #12638

Merged
merged 2 commits into from
Mar 29, 2023
Merged

Conversation

ArnaudBarre
Copy link
Member

Integrations links: Ruby & Laravel

@ArnaudBarre ArnaudBarre self-assigned this Mar 29, 2023
@stackblitz
Copy link

stackblitz bot commented Mar 29, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@ArnaudBarre ArnaudBarre added the performance Performance related enhancement label Mar 29, 2023
Comment on lines 150 to 153
// This virtual file from the React plugin is hardcoded in integrations
// like Ruby & Laravel so adding this instead of migrating both React
// plugins to use /virtual:
id === '/@react-refresh'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about hardcoding this, don't we already resolve this id in a pre plugin?

https://github.com/vitejs/vite-plugin-react/blob/a1b4de121b8baa75b21db35b8c77099f336a8bd2/packages/plugin-react/src/index.ts#L359-L363

That means it wouldn't hit here if it resolved there first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SWC plugin is not pre. But maybe I can split the plugin in two so that the client is pre and the rest is not pre.
I think I will do the same because it's not easy to add pre-process of other plugin for example: https://github.com/vitejs/vite-plugin-react/pull/122/files?w=1#diff-885677e87d0c22a62efc8688d62685a305a3eb34469ddf0d2857da84cf417630R7

Comment on lines 148 to 149
// When injected directly in html/client code
id.startsWith('/virtual:') ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure you can inject this directly in html/client and work 🤔 Do you have an example of this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unoCSS for example: https://github.com/unocss/unocss/blob/main/packages/vite/src/devtool.ts#L141
(here it's not using the "/virtual:" prefix but it could)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means it could be anything though, and we shouldn't have a special preference for /virtual:?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should provide and encourage using a prefix that both:

  • doesn't trigger fs calls if the plugin is not "pre"
  • is a valid url and doesn't need to be pre-process by Vite

Without this line, the line you need to use /@id/__x00__@unocss/devtools which is not great compare to /virtual:@unocss/devtools

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of \0 helps because the rollup ecosystem expects that when handling virtual modules (there are checks in rollup plugins when dealing with source maps for example). Uno should use \0 in the resolved id here.

I think that converting \0 to /@id/__x00__ is making folks shy away from using the proper virtual marker. We could change our scheme in Vite 5 (or before?) to something that looks better. For example /@virtual/ or /@id/virtual:. @antfu what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine adding this condition. I still think we should keep suggesting the use of \0 (and to me changing our encoding could help there, I may do a PR in Vite 5 about this)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree to remove it until we agree on a pattern.
But when you look at profiling, import parsing & transformation starts to show up and I think it would be good to move to import patterns that are more ESM friendly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by ESM friendly? Having to encode \0 for the browser shouldnt have a noticeable perf hit, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The perf hit is in parsing/rewriting every bit of JS we're sending to the browser.
And rewriting all import implies a lot of string manipulation where JS is not really fast.
I can't find a specific entry for that in a profile right now, but this is one of the main bottleneck on rds.

But when you look at a profile, you see a lot of small things that are related to import transformation (new URL which is really slow in node, regex, ensureVersion, isOptimisedDep, ...)

If we rethink the patterns, we should go for things that does't need serialization and can directly go to as fs.read call when hitting back the server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can avoid resolving the URLs though. We at least need to check them once. My point is that \0 vs /virtual: doesn't affect this in particular. We still need to improve things while resolving, even after all we did in the past weeks, there seems to be a lot more to gain from making vite:resolve leaner. new URL is a good example, we should check if we can replace that by something faster.

About this PR, let's keep the new /virtual: condition, and remove the `'/@react-refresh' and we can merge it?

@patak-dev patak-dev merged commit 9e13f5f into main Mar 29, 2023
@patak-dev patak-dev deleted the perf/resolve-skip-virtual branch March 29, 2023 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants