microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.26k stars 29.3k forks source link

Explore lazy code loading #164068

Open Tyriar opened 2 years ago

Tyriar commented 2 years ago

As VS Code has grown larger and more featureful, so does the main bundle size that increases the time it takes to load/parse/compile the JS linearly. This isn't an issue for anything not in main bundle (shared proc, exthost, ptyhost, built-in extensions, etc.), but code for features like the terminal, tasks and the welcome page are all loaded in even if they are not used yet.

We have had various discussions about this in the past which I don't think are documented anywhere. Here are some of the things discussed and a brain dump of other thoughts on the topic:

Tyriar commented 2 years ago

@jrieken and I discussed this last week but I don't think there's much action we can take until https://github.com/microsoft/vscode/issues/160416 is explored more? Let me know if you had other things we should look into for October.

jrieken commented 2 years ago

Some simple experiments that I am trying to do in October

jrieken commented 2 years ago

remove all editor-contributions, build, and measure impact on bundle size and load time

I have done the following experiment

In step two I have removed all /contrib-imports from editor.all.ts. This builds VS Code without any editor features, no search, no IntelliSense, etc... 1 I then repeated the steps from above.

In step three I have removed many /contrib-imports from workbench.common.main.ts and workbench.desktop.main.ts. This build VS Code without any workbench contributions2. Again, I repeated the measures from above.

size startup (avg of three runs)
full 11.5 MB 309.66 ms
no editor contrib 11.3 MB 305.66 ms
no editor, workbench contrib2 6.4 MB 255 ms

1 some editor contribution still end up in the bundle because they are referenced by other workbench contributions 2 I was NOT ABLE to get a properly working version of VS Code after these changes. Some contributions are needed (files, explorer, ect) and I didn't dig into what exact set that is. So, while a VS Code window came up it was not functional and the measures are unfair.

Finding, learnings, observations

jrieken commented 2 years ago

much action we can take until https://github.com/microsoft/vscode/issues/160416 is explored more

Not necessarily. AMD is a dead horse but it can do all of this - the only thing that will be simpler with ESM is tree-shaking on a module-level, e.g identify unused module exports and drop them. We do have a custom bundler which we need to blow the dust off that and maybe re-learn how it works.

A tricky thing from the past was knowing how to bundle, e.g some modules are used everywhere and those should be in the initial bundle and not be repeated in subsequent bundles. In the past we realised that most things were in the initial bundle and that contributions were super tiny. That's what might us drop the whole approach. However, by now we have plenty more contributions and this might start to pay off.

jrieken commented 1 year ago

We are now down the rabbit hole exploring to migrate from AMD to ESM. While this isn't needed to do bundling and lazy code loading we will benefit from being able to reuse existing bundlers (instead of rolling our own AMD-based solution).

I am snowplowing this for now, if there is urgent need for code splitting: the keyboardLayouts/layout.contribution-modules are a sample of how we are doing it already today