Skip to content

Improve chunking algorithm for dynamic imports to recognize already loaded modules #3124

Closed

Description

@lukastaegert

This is extracted from #2407. See also #2575 (comment).

Expected Behavior / Situation

Assume there are is an entry module E that statically imports a shared module S. Assume that E also imports a dynamic entry D that also imports S.

The hope would be that in that situation, Rollup creates two chunks, one containing E and S and one only containing D. This is taking into account that when D is loading, we can be sure that S is already loaded and there would be no gain from putting it into a separate chunk.

Actual Behavior / Situation

S is put into a separate chunk.

Modification Proposal

In general, when marking modules as static dependencies of a dynamic chunk, the algorithm should ignore modules that are already static dependencies of all importing chunks. Or, if an importing chunk is itself dynamic and does not contain the module, then we should check again all chunks that import THAT chunk dynamically. Dynamic cycles should be handled as well.

This would probably need a general change in how the "colouring" algorithm works. Previously, the algorithm would only store a hash for each chunk that is updated as new entry points are traced. The new algorithm would need to retain information about what entry points actually depend on a given module, maybe in form of a Set. On the other hand, this could mean we no longer need the hashes, or we only need them for grouping.


Note by maintainers (by @lukastaegert)

🙋‍♀️ Want to get involved and address this? Even though this is an advanced issue, it can probably be implemented in a very localized way without a lot of interaction with the rest of Rollup.

The algorithm that distributes modules into chunks resides here: https://github.com/rollup/rollup/blob/master/src/utils/chunkColouring.ts

At the moment, the algorithm works by assigning each module an entryPointsHash. Modules with the same hash will then be grouped into chunks.

  1. First it iterates through all manual chunks to assign them and all their static dependencies the same hash. Those modules will be ignored for the rest of the chunking.
  2. Then it iterates through all static entry points. Each of them is assigned a random hash. Then it iterates recursively through all their static dependencies to assign them the same hash. If a dependency already has a hash, it is XORed with the new hash. If a dependency has already been iterated, it is ignored. During this phase we also collect all dynamic dependencies.
  3. Last we repeat this process with the dynamic dependencies we collected as entry points. If in this phase, new dynamic entry points are discovered, they are pushed to the end of the list of dynamic entries and handled as well.

A new algorithm could work similar to the existing algorithm but with the following addition: When assigning hashes for a dynamic entry point, we not only ignore dependencies that have already been iterated over but also dependencies that are "already in memory". So how could we discover these?

Simple scenario: There is only a single dynamic import statement per dynamic entry point, and dynamic entry points do not dynamically load other dynamic entry points

Let us assume that in addition to the hashes, we maintain a Map of modules to Sets that contains for each module the entry points that statically depend on that module. Let us call this Map dependentEntryPointsByModule.

Additionally, we create another Map of modules to Sets that contains for each dynamic entry point the entry points that statically depend on the importing module. Let us calls this Map dynamicDependentEntryPointsByDynamicEntry.

When we find a dynamic import during our iterations, in our simple scenario we can just copy over the Set in dependentEntryPointsByModule of the importing module to dynamicDependentEntryPointsByDynamicEntry for the dynamic entry point.

Then when we are doing the static iteration for the dynamic entry point, we can skip all dependencies for the hash colouring where dynamicDependentEntryPointsByDynamicEntry of our current dynamic entry point is a subset of dependentEntryPointsByModule.

Example 1:

Entry point E statically imports A and dynamically imports D.
D imports A.

dependentEntryPointsByModule: {
  E -> {E},
  A -> {E}
}

dynamicDependentEntryPointsByDynamicEntry: {
  D -> {E} // from E
}

Thus when collecting static dependencies for D we can skip A because {E} is a subset of {E} and the chunks are {E, A} and {D}.

Example 2:

Entry point E1 statically imports A and B, E2 statically imports A.
Furthermore, A dynamically imports D1, B dynamically imports D2.
Last, D1 and D2 both statically import both A and B.

dependentEntryPointsByModule: {
  E1 -> {E1},
  E2 -> {E2},
  A -> {E1, E2},
  B -> {E1}
}

dynamicDependentEntryPointsByDynamicEntry: {
  D1 -> {E1, E2}, // from A
  D2 -> {E1} // from B
}

Now when we iterate the dependencies of D1, we find that we can ignore A but we cannot ignore B as {E1, E2} is not a subset of {E1}. This reflects the scenario where I only loaded the entry point E2 and then later D1. In this situation, E1 and therefore B are not in memory.

On the other hand for D2 we can ignore both A and B as E1 and thus B NEED to be loaded before D2.

Slightly more complex scenario: Like the previous scenario, but there can be more than one dynamic import statement loading a dynamic entry point

The only change we need here is that when we find a dynamic import during our iterations, dynamicDependentEntryPointsByDynamicEntry is not just copied from dependentEntryPointsByModule of the importing module but it is a union of dependentEntryPointsByModule of all importing module.

Example 3:

Entry point E1 statically imports A and B, E2 imports A, B, C, E3 imports A and C, E4 imports A. Furthermore, both B and C dynamically import D. D statically imports A, B, C.

dependentEntryPointsByModule: {
  E1 -> {E1},
  E2 -> {E2},
  E3 -> {E3},
  E4 -> {E4},
  A -> {E1, E2, E3, E4},
  B -> {E1, E2},
  C -> {E2, E3}
}

dynamicDependentEntryPointsByDynamicEntry: {
  D -> {E1, E2, E3}, // from B merged with C
}

When iterating static dependencies of D, we can only skip A because {E1, E2, E3} is a subset of {E1, E2, E3, E4}. We cannot skip B or C because if for instance we load E1 and then D, then C is not in memory while if we load E3 and D, then B is not in memory.

Complex scenario: There are dynamic entry points that can dynamically import other dynamic entries

To be able to handle this, we now have to extend dependentEntryPointsByModule to also contain all dynamic entry points that depend on a module. If we do that, our previous solution will already be able to solve some scenarios correctly such as this one:

Example 5

Entry point E dynamically imports D1. D1 statically imports A and dynamically imports D2. D2 statically imports A.

dependentEntryPointsByModule: {
  E -> {E},
  D1 -> {D1},
  D2 -> {D2},
  A -> {D1, D2}
}

dynamicDependentEntryPointsByDynamicEntry: {
  D1 -> {E}, // from E,
  D2 -> {D1} // from D1
}

When iterating static dependencies of D1, we of course have to include A as it is not in memory and {E} is not a subset of {D1, D2}. We can skip it for D2, though, as {D1} is a subset of {D1, D2}.

This logic however will not recognize already loaded modules over several stages. To handle this, we replace the "is subset of" check with "each dependent entry point in dynamicDependentEntryPointsByDynamicEntry for our current dynamic entry is either contained in dependentEntryPointsByModule, or this entry point is itself dynamic and all of its dynamicDependentEntryPointsByDynamicEntry are contained in dependentEntryPointsByModule", recursing for further dynamic entry points if necessary. To avoid infinite loops, this logic should check each entry module, dynamic or not, at most once.

Example 6:

Entry point E statically imports A and dynamically imports D1. D1 dynamically imports D2. D2 statically imports A.

dependentEntryPointsByModule: {
  E -> {E},
  D1 -> {D1},
  D2 -> {D2},
  A -> {E, D2}
}

dynamicDependentEntryPointsByDynamicEntry: {
  D1 -> {E}, // from E,
  D2 -> {D1} // from D1
}

When iterating static dependencies of D2, we can skip A even though {D1} is not a subset of {E, D2} because {E} is a subset of {E, D2}.

Activity

  1. tjenkinson commented on Dec 25, 2019

    @tjenkinson
    Member

    Is there a bug here?

    modulesVisitedForCurrentEntry = new Set(currentEntry.id);

    Shouldn't it be

    modulesVisitedForCurrentEntry = new Set([currentEntry.id]);

    ?

    If currentEntry.id is a string it looks like you'll end up with a set containing an entry for each letter.

  2. shellscape commented on Dec 25, 2019

    @shellscape
    Contributor

    I'd go one further and say it would need to be:

    modulesVisitedForCurrentEntry = new Set([].concat(currentEntry.id));
  3. tjenkinson commented on Dec 25, 2019

    @tjenkinson
    Member

    @shellscape why does it need to be concatenated with the empty array?

  4. tjenkinson commented on Dec 25, 2019

    @tjenkinson
    Member

    Started working on this :)

    WIP PR: #3305

    Not sure it's working properly yet based on the output from some of the tests

  5. lukastaegert commented on Dec 25, 2019

    @lukastaegert
    MemberAuthor

    Yes, there are some brackets missing. Cool you are taking this up, hope to have a look in a few days. Note that the algorithm was just a sketch to get someone interested and never tested, there could be fundamental things I missed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions