whatwg/loader

[discussion] deterministic execution in turns

caridy opened this issue · 7 comments

notes:

  • during the loading process, we might have clusters of dependencies that are ready to be executed.
  • order is important for deterministic reasons.
  • running in multiple turns seems to be ok (if the order is deterministic).
  • top level await should be taken into consideration.

Indeed. We had to figure this out for HTML Imports. In realistic network condition, waiting until everything loads is unlikely to result acceptable performance characteristics.

For modules I don't think we have a choice. You simply cannot execute a module before its dependencies have been executed. So you cannot execute any part of the module tree before all its children have been executed.

@domenic this discussion is not really about the "imported" module, it is about parts of the dependency tree that can be executed in turns. E.g.:

while doing import A:

A -> B -> C, while B -> D and C -> E

If B and D are in "link" stage, and the pipeline is just waiting for C, and E to be fetched, we could, theoretically, evaluate B and C, get them to "ready" stage, without any observable difference in the runtime while maintaining the deterministic aspect of the module graph since this is equivalent to:

import B, then import A;

Today, this is not the case since we need to get every module in the graph in link stage before proceeding to the evaluation phase.

Note: -> means "depends on".

Ah, I see. The execution model for whatwg/html#443 does not have that problem; it executes everything as soon as its dependencies are ready.

that is correct!

It's a bit more complicated than my above comment implied sadly. See whatwg/html#443 (comment). Basically my above comment is correct about the current whatwg/html#443 PR, but that state of affairs is actually due to a bug that gives us semantics that are something like "defer except execution order is not preserved".

If you preserve execution order (per normal defer semantics) than you get a problem similar to the one described in this bug where large/slow trees that are earlier in the source order can block small/fast trees later in the source order.

If you want the kind of incremental as-soon-as-it-loads tree execution style then you want something more like async.

Anyway, more discussion over there, I just wanted to update this thread since my above comment was incorrect.

I don't think this is related to whatwg/html#443. We will be able to introduce execution in turn for any dependency of a top level module imported via script tags without changing the way you interact with the loader (from what I can read in whatwg/html#443). It is really all about the loader, and when it invokes evaluation in certain module records.