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

[Bug]: module federation plugin makes dev/build/HMR a lot slower #3067

Closed
hverlin opened this issue Jul 22, 2024 · 8 comments
Closed

[Bug]: module federation plugin makes dev/build/HMR a lot slower #3067

hverlin opened this issue Jul 22, 2024 · 8 comments
Assignees
Labels
✨ enhancement New feature or request

Comments

@hverlin
Copy link

hverlin commented Jul 22, 2024

System Info

rsbuild: v1.0.1-beta.3

Details

Module federation plugin seems to make the build a lot slower. HMR in particular becomes unusable.

I have a large internal rsbuild project where we can clearly see the issue. I am not sure exactly what to share as making a reproducer is quite challenging.

Maybe you have some ideas of what could impact the build time so much.

The project is using

  • React plugin
  • less plugin (seems that this plugin has the most impact on the rsdoctor output)
  • It has ~ 20 entries

With the built-in module-federation options, the dev output takes 31.6 s instead of `13.5 s.

What I have noticed is that that once I enable the module federation plugin, a lot of time is spent on sealing chunk optimization (I don't even have time to read that text when module federation is not enabled).

  • Additionally, if I only build a single entry (the largest we have), it takes about the same time to produce the output: 12s (with MF) vs 10s.
  • The HMR time is also impacted by the number of entries and module federation:
    • base (single entries): ~ 1.5s
    • base (all entries): ~ 2.5s
    • with MF (single entry): ~3.25s
    • with MF (all entries): ~ 25s 💥

If I use @module-federation/enhanced, the build time increases even more (even with dts: false).


I tried to reproduce by cloning https://github.com/rspack-contrib/performance-compare/blob/main/rsbuild.config.mjs and updating the configuration as follows

export default defineConfig({
  plugins: [pluginReact()],
  source: {
    entry: Object.fromEntries(
        Array.from({ length: 200 }, (_, i) => [`index${i}`, "./src/index.tsx"])
    )
  },
  moduleFederation: {
    options: {
      name: "app",
      shared: ["react", "react-dom"]
    }
  }
});

however, it does not seem that the number of entries affects rspack and module federation so much. Also tried adding some less files and used the less-plugin, did not make a difference.

Reproduce Steps

Will share if I manage to make a small example. For now, see the description of the issue.

Is there a large project that I could fork to reproduce the issue? The example projects are small, so it might hard to notice

@hverlin hverlin changed the title [Bug]: module federation plugin makes the build twice as slow [Bug]: module federation plugin makes dev/build/HMR a lot slower Jul 22, 2024
@jerrykingxyz jerrykingxyz self-assigned this Jul 23, 2024
@jerrykingxyz
Copy link

@hverlin You can try to analyze the performance using performance profile. https://www.rspack.dev/guide/optimization/profile

@hverlin
Copy link
Author

hverlin commented Jul 24, 2024

Thanks, @jerrykingxyz ! I did not notice this also working with the dev-server. Did not think of checking the output directory for the profile.

Here are the results:

  • without module federation
    image

  • with module federation
    image

The main difference I see is this very long trace for prepare_module_group_map.
https://github.com/search?q=repo%3Aweb-infra-dev%2Frspack%20prepare_module_group_map&type=code

Not exactly sure what else to check. I see that rsbuild is introducing changes to chunks
https://rsbuild.dev/config/module-federation/options#introduction

Some differences I have noticed when running rsbuild inspect:
image
image

@JSerFeng
Copy link
Contributor

Because MF has bugs with splitChunks.chunks set to 'all', so rsbuild will convert this config to function, however this function will be called for every module several times, you can try manually overriding splitChunks.chunks to 'initial'

Doc: https://rsbuild.dev/config/performance/chunk-split#chunksplitoverride

Example:

export default {
  performance: {
    chunkSplit: {
      override: {
        chunks: 'initial'
      },
    },
  },
};

@hverlin
Copy link
Author

hverlin commented Jul 25, 2024

Thanks!

Sadly, it appears that just using chunks: 'initial' makes things slower (Dev-server now takes 45s, HMR 31s.)

I think the reason is that PatchSplitChunksPlugin is still present in the final rspack configuration?

class PatchSplitChunksPlugin implements RspackPluginInstance {

If I manually remove that line from rsbuild, I can see that it then goes back to normal (or omit name in the module federation options)

chain
.plugin('mf-patch-split-chunks')
.use(PatchSplitChunksPlugin, [options.name]);

Based on #1461 (comment), it appears that this is actually required for everything to work properly, right?
(I don't have a good understanding of which options are required - I had similar questions here based on the difference between the module-federation quick start guide and rsbuild module federation guide: module-federation/core#2527)

@JSerFeng
Copy link
Contributor

JSerFeng commented Jul 30, 2024

Sorry for the delay.

Based on #1461 (comment), it appears that this is actually required for everything to work properly, right?

Yes, some cases using all will crash, but initial is fine, and set initial in development mode can get better dev performance, and use the default option that rsbuild currently used to get best production output.

I'll transfer this issue to rsbuild

@JSerFeng JSerFeng transferred this issue from web-infra-dev/rspack Jul 30, 2024
@chenjiahan
Copy link
Member

I will try to fix this in Rsbuild

@chenjiahan
Copy link
Member

@hverlin can you upgrade to the new version and check the performance?

https://github.com/web-infra-dev/rsbuild/releases/tag/v1.0.1-beta.9

@hverlin
Copy link
Author

hverlin commented Aug 2, 2024

Thank you, @chenjiahan! It's much faster now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants