Repository navigation
Improve chunking algorithm for dynamic imports to recognize already loaded modules - #3305
tjenkinson wants to merge 48 commits into
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3305 +/- ##
==========================================
+ Coverage 93.18% 93.19% +0.01%
==========================================
Files 172 172
Lines 6059 6068 +9
Branches 1804 1807 +3
==========================================
+ Hits 5646 5655 +9
Misses 220 220
Partials 193 193
Continue to review full report at Codecov.
|
…e dynamic module instead because the dynamic chunk now contains the contents of 'main-static.js'
11bef6e to
55ca332
Compare
lukastaegert
left a comment
There was a problem hiding this comment.
Hi, I know this is still WIP. I started adding some of the basic test cases specific to dynamic imports to see what it actually does. One issue is that when there are multiple entry points importing a dynamic chunk, the algorithm generates too few chunks and leads to code that is executed that should not have been executed. I did not yet look into the algorithm, just some early feedback.
| import { value } from './main.js'; | ||
| export { value } from './main.js'; | ||
|
|
||
| console.log('dynamic', value); |
There was a problem hiding this comment.
This looks how it should for the simple case: The dynamic chunk is importing its value from main.js and we avoided the shared chunk. 👍
| import { v as value2 } from './generated-dep2.js'; | ||
| export { v as value2 } from './generated-dep2.js'; | ||
|
|
||
| console.log('dynamic1', value1, value2); |
There was a problem hiding this comment.
In this scenario, we have two entry points that both dynamically import a single module. The imported module shares two dependencies with one entry point and only one with the other.
Unfortunately, this is broken: dep1 should not have been inlined into main1 because now if the user starts executing the code from the other entry point main2, main1 will also be loaded by the dynamic module. This was not the case before bundling.
Instead, all modules need to become separate chunks
- dep1 cannot be inlined into main1 or main2 because it is shared
- dep2 cannot be inlined into main1 because it is not guaranteed to be in memory when the dynamic chunk is loaded
There was a problem hiding this comment.
👍 got it thanks for the simpler test. will take a look
|
Started working on a slightly different approach |
|
@lukastaegert thanks for your solution. There's some tidying to do and the new tests need proper names/descriptions (so not marking it ready for review yet), but I think the solution I have is working. I spent some time looking at the fixture changes with both yours and mine There are quite a few fixture changes I need to spend some more time checking. Also still a few other test failures Some points:
|
This looks indeed broken in my setup, thanks for checking!
What do you mean by "event ordering"?
Let's have a look once everything is in place and see which is picked. In the end, I will also learn something from yours and it is good to have more than algorithm available when it comes to integrating future improvements. |
Indeed there was a check missing for a manualChunkAlias in the loop for dynamic entries that was present in the loop for static entries. I updated the code in the post above to fix it, nicely spotted! |
I meant the I updated all the tests so that the build is green and tidied up the implementation, but need to check they make sense. Both my implementation and yours though pass all the tests in this PR, so it's nice to know functionally they're the same :) |
lukastaegert
left a comment
There was a problem hiding this comment.
Sorry this is taking a while, this is quite a bit to work through. Your implementation is now passing all my simple test cases as well except for a non-trivial one that I added to your PR. I think this would be nice to get right. The situation is when
- we have two entry points that share two dependencies (without a dynamic chunk, this would create three chunks, one for each entry point and one for the two dependencies)
- both entry points contain a dynamic import that only depends on one of the shared dependencies
As the this dependency is already in memory, there is no reason to create another chunk for it, however your algorithm splits the shared chunk.
I added the _expected folder created by my alternative algorithm.
…t is not shared by the dynamic import
bd5fdc3 to
b19ef0d
Compare
|
Unfortunately, performance is indeed a bigger problem. I created a small const FILES = 1000;
const ENTRIES = 5;
const DYNAMIC_ENTRIES = 10;
const DYNAMIC_IMPORTS_PER_FILE = 2;
const STATIC_IMPORTS_PER_FILE = 20;
function getFileName(idx) {
return `file_${idx}`
}
function getRandomFile(start, end) {
return getFileName(start + Math.floor(Math.random() * (end - start)))
}
export default {
input: [...Array(ENTRIES).keys()].map(getFileName),
plugins: [
{
resolveId(id) {
return id;
},
load(id) {
const idx = Number(id.slice(5));
let output = `console.log("${id}");`;
for (let idx = 0; idx < STATIC_IMPORTS_PER_FILE; idx++) {
output += `import "${getRandomFile(ENTRIES + DYNAMIC_ENTRIES, FILES)}";`;
}
for (let idx = 0; idx < DYNAMIC_IMPORTS_PER_FILE; idx++) {
output += `import("${getRandomFile(ENTRIES, ENTRIES + DYNAMIC_ENTRIES)}");`;
}
return output;
}
}
],
output: [
{
dir: 'dist',
format: 'esm'
}
]
};When I run this script with
As a matter of fact when I turn down |
|
Thanks for the extra test and performance script. I see where the issue is and would be interested in adding some caching now there’s a nice way to measure the speed |
|
Rewrote it a bit and it handles the new test case. Still need to check performance now. Also first use of optional chaining :) if (!module.manualChunkAlias && importer?.manualChunkAlias) {
module.manualChunkAlias = importer.manualChunkAlias;
} |
|
Nice, and you are doing great work! Still, I know you have already put a lot of effort in this, but we could also just release the other algorithm for now? The thing is, I would like to have this released soon before 2.0.0 is ready (which will be another 3 or so weeks, but still), and I need this as an argument to finally remove |
|
Sure I don’t want to block this. Fine with releasing the other and if I get this working we could decide whether or not it makes to switch.
|
…modules if there are now more loaded modules than before there's no point processing, given not painting the loaded modules would have no effect given they were already painted in the previous run
cafeb4a to
a0ed839
Compare
|
This has been superseded by the currently published version of Rollup |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Description
This updates
chunkColouringso that when a module is imported, if it is already assigned a colour, and something in the import tree also has that colour, it will remain unchanged, because it means the module will already be in memory.