Skip to content

Design goals: fast interpreter, polyfill ease, and preorder versus postorder encoding #239

Closed

Description

@jfbastien

We were recently discussing preorder versus postorder encoding. Different folks (@titzer, @kg, @ncbray, @sunfishcode, @lukewagner) have explored this in the prototype repos and elsewhere. We haven't reached an agreement yet on which is better (considering compression, decoding / interpretation time, implementation complexity).

Postorder may be better for interpreter, preorder for CFG building. We need more experiments and data to figure out what the tradeoffs are.

Once we have this data, we need to answer a bigger question:

  • Is it a goal to create a fast interpreter?
  • Is the polyfill a very short-term stopgap, or will it stay alive for a while?

Activity

  1. added this to the MVP milestone on Jun 30, 2015
  2. kripken commented on Jun 30, 2015

    @kripken
    Member

    I think a fast interpreter is a worthy goal. A lot of code on the web ends up executed exactly once, and just interpreting the code can be faster than JITing it first. So I would expect wasm interpreters to be used in production.

  3. sunfishcode commented on Jun 30, 2015

    @sunfishcode
    Member

    I think a fast interpreter is a worthy secondary goal. WebAssembly as we know it today isn't going to be a great choice for code on the web which is expected to be executed exactly once. JavaScript handles that use case quite well already.

  4. MikeHolman commented on Jun 30, 2015

    @MikeHolman
    Member

    @kripken Agreed. We will definitely be shipping an interpreter, and we have found interpreter perf is pretty good, and is even acceptable for startup on asm.js (we had a baseline JIT but it didn't really buy much over a baseline interpreter). If we can reduce startup time by not having to do a bytecode generation pass on the main thread, I think that is a noble cause.

  5. titzer commented on Jun 30, 2015

    @titzer

    At the minimum you have to do a pass to validate the types, so if that pass
    spits out an internal bytecode along the way, it's pretty close to free.

    I think fast interpretation of direct wire format is going to be a tough
    nut to crack and could drive us away from the primary goals of size and
    fast JIT compilation.

    On Tue, Jun 30, 2015 at 7:41 PM, Michael Holman [email protected]
    wrote:

    @kripken https://github.com/kripken Agreed. We will definitely be
    shipping an interpreter, and we have found interpreter perf is pretty good,
    and is even acceptable for startup on asm.js (we had a baseline JIT but it
    didn't really buy much over a baseline interpreter). If we can reduce
    startup time by not having to do a bytecode generation pass on the main
    thread, I think that is a noble cause.

    —
    Reply to this email directly or view it on GitHub
    #239 (comment).

  6. kripken commented on Jun 30, 2015

    @kripken
    Member

    I agree that interpreting is secondary to baseline or full-optimizing JITing. However it still feels quite important. It would be nice if wasm had an option to annotate a module with "-O0" or "-O2" etc., which PNaCl has now, and then websites could mark code as "-O0" when they know it executes only once, in which case the browser could avoid a large amount of overhead. It would be far better than JS engines can do now, in fact, so it would be a big improvement for the web.

  7. jfbastien commented on Jun 30, 2015

    @jfbastien
    MemberAuthor

    @kripken I'd rather annotate on a per-function basis, instead of whole module.

  8. kripken commented on Jun 30, 2015

    @kripken
    Member

    Agreed, that would be even better.

  9. kg commented on Jun 30, 2015

    @kg
    Contributor

    The polyfill consideration is pretty important and I've seen a mixture of views expressed here, so it's worthwhile to figure out whether there's any agreement on that part of the premise for any pre/post-order decision we make.

    Do we believe that the polyfill will live around a year at most, as some people have expressed? I personally consider that implausible based on the history of internet technology adoption, but it is possible if every involved party pushes really hard. If we collectively think that sort of aggressive adoption schedule is possible and commit to push hard for it, that frees us to not spend too much time worrying about the polyfill when designing the file format. If we can't ensure a compressed time-scale, any format decisions made in favor of an interpreter could compromise polyfill performance for years.

  10. titzer commented on Jun 30, 2015

    @titzer

    From my perspective, it seems likely the polyfill will continue to exist to
    support older browsers, even 2-3 years from now.

    On Tue, Jun 30, 2015 at 8:13 PM, Katelyn Gadd [email protected]
    wrote:

    The polyfill consideration is pretty important and I've seen a mixture of
    views expressed here, so it's worthwhile to figure out whether there's any
    agreement on that part of the premise for any pre/post-order decision we
    make.

    Do we believe that the polyfill will live around a year at most, as some
    people have expressed? I personally consider that implausible based on the
    history of internet technology adoption, but it is possible if every
    involved party pushes really hard. If we collectively think that sort of
    aggressive adoption schedule is possible and commit to push hard for it,
    that frees us to not spend too much time worrying about the polyfill when
    designing the file format. If we can't ensure a compressed time-scale, any
    format decisions made in favor of an interpreter could compromise polyfill
    performance for years.

    —
    Reply to this email directly or view it on GitHub
    #239 (comment).

  11. qwertie commented on Jun 30, 2015

    @qwertie

    I view WebAssembly as something that, if designed well, is a standard (or a precursor to one) that will last over 100 years. With today's self-updating browsers, the life of the polyfill will be very brief in comparison.

  12. paleozogt commented on Jul 1, 2015

    @paleozogt

    @kg Given that "Safari is the new IE", I suspect that the Polyfill will be around for a long time.

  13. MikeHolman commented on Jul 2, 2015

    @MikeHolman
    Member

    For the last couple days at lunch @LouisLaf and I have been going back and forth on the polyfill. One thing we've discussed is having a wasm feature check and then the server can serve up either the polyfilled js (gzipped) or the wasm. Obviously, this will not be quite as small as the gzipped binary, but from the numbers in the FAQ it's still pretty good.

    If we do this, then we no longer need to consider the polyfill when discussing the binary encoding.

  14. kripken commented on Jul 2, 2015

    @kripken
    Member

    It reduces the need for quick translation of the binary encoding to JS, but we would still need a practical way to do that translation which emits efficient JS.

    Relying on separate downloads would make hosting such applications a little more complicated for people, but it's debatable if that's a serious problem or not.

  15. 15 remaining items

  16. lukewagner commented on Jul 8, 2015

    @lukewagner
    Member

    @titzer The reason I'm favoring postorder for expressions isn't direct interpretation, but because it seems slightly more natural for iterative (i.e., non-recursive) decoding of expressions (not control flow).

    @MikeHolman Is that a proposal to include offsets in the raw binary format? I think this would have worse size and complicate/slow general validation for this one case. Assuming 'no', though, and you want offsets, then you'd need to translate to an internal bytecode with offsets (so it's not direct interpretation). I could also imagine an interpreter that JITed with a very very low hotness count (like 2), taking advantage of the fact that most code is dead or run-once and in this case, you might not even need offsets.

  17. rossberg commented on Jul 8, 2015

    @rossberg
    Member

    FWIW, I agree: for a mostly tree-like data structure, post-order serialisation is more natural, and likely also slightly more efficient to decode -- at least that was what we measured a long time ago in a project I was involved in. Though that was for general serialisation; if it's an AST and you also want to be able to create a CFG from that directly then the equation might change somewhat.

    @luke, your suggestion seems dependent on that other question, whether statements should be expressions. If they are, you probably cannot (or don't want to) represent control differently. Would that be a big issue, though? It's easy to extend postorder deserialisation to handle DAGs and general graphs.

  18. titzer commented on Jul 8, 2015

    @titzer

    I don't think postorder expression encoding makes any difference (positive
    or negative) if one chooses a recursive decent parsing strategy. If one
    chooses an LR parser strategy, there are already reduce actions in the
    middle and end of decoding if's, so it's not a big deal to add reduce
    actions for preorder expressions. In fact, that's exactly what the
    v8-native-decoder does.

    On Wed, Jul 8, 2015 at 1:05 PM, rossberg-chromium [email protected]
    wrote:

    FWIW, I agree: for a mostly tree-like data structure, post-order
    serialisation is more natural, and likely also slightly more efficient to
    decode -- at least that was what we measured a long time ago in a project I
    was involved in. Though that was for general serialisation; if it's an AST
    and you also want to be able to create a CFG from that directly then the
    equation might change somewhat.

    @luke https://github.com/luke, your suggestion seems dependent on that
    other question, whether statements should be expressions. If they are, you
    probably cannot (or don't want to) represent control differently. Would
    that be a big issue, though? It's easy to extend postorder deserialisation
    to handle DAGs and general graphs.

    —
    Reply to this email directly or view it on GitHub
    #239 (comment).

  19. creationix commented on Jul 8, 2015

    @creationix

    If we have [if][true-expression][false-expression], the engine needs to find the start of the [false-expression] cheaply (i.e. constant time) without parsing the true-expression.

    I recently implemented an AST based interpreter for microcontrollers. It's not based on wasm bytecodes, but I did it using a binary AST. I tried several styles including luajit inspired fixed-width bytecodes, forth style stack operations and pre-order AST. The pre-order was by far faster in my implementations even without jump offsets in my if/elif/else nodes. I simply have a skip function that quicky skips over exactly one expression worth of bytecode. On laptop my interpreter is able to sum the first 1,000,000,000 integers in about 20 seconds.

    Yes embedding jump offsets after IF/ELIF and before ELIF/ELSE would probably make it faster, but it greatly complicates the bytecode. For my uses (kids learning on microcontrollers) scanning is plenty fast. Just a data point to consider.

  20. creationix commented on Jul 8, 2015

    @creationix

    Also fwiw, in my language, everything including control flow is an expression.

  21. lukewagner commented on Jul 8, 2015

    @lukewagner
    Member

    @rossberg-chromium Even in the statements-are-expressions case, I think it'd be ok to nest pre-/post-order. It would be a problem for a generic traversal algo (that doesn't know anything about the specific node types), but for a custom wasm decoder (that switches on the decoded opcode before moving on to the next), I think I see how it'd work (though of course I'd want to implement to be sure).

  22. kripken commented on Jul 8, 2015

    @kripken
    Member

    @creationix: that's interesting data, thanks. do you have insight as to why preorder was faster in your interpreter? perhaps it was heavily tied to the fact you have fixed-size bytecodes?

  23. bnjbvr commented on Jul 10, 2015

    @bnjbvr
    Member

    I like the idea of an interpreter for the reasons @LouisLaf brought up. In particular,

    1. Secure scenarios where JIT'ing is not permitted. We have instances of this with Xbox for example.

    would be of interest for the Tor browser (which is based on Firefox). Using only a wasm interpreter in addition to a JS interpreter sounds like a possibly acceptable tradeoff for the Tor browser (rather than blocking all JS, as they do, as of today).

  24. creationix commented on Jul 10, 2015

    @creationix

    @kripken I'm not entirely sure why it's so much faster. Perhaps I just programmed better that way.

    With both, I have 7-bit opcodes with 1 bit to signify it's an opcode.

    1xxxxxxx - opcode, literal entry into enum table starting at 128.
    0mxxxxxx mxxxxxxx* - variable length unsigned integer. Numbers under 64 represent themselves.

    I don't use fixed-size codes for the preorder or post-order versions, that was only for the luajit style bytecodes (4 bytes each).

    The preorder version can be seen visually in the IDE for kids I'm working on at http://uscript.creationix.com/ (though I graphically show math ops as infix to make it easier for kids)

    In preorder, there is no length header or even end marker. Every op has a fixed set of operators. For variable length bodies, I have a DO x expression* expression. I use the native C stack as my interpreter stack. It's essentially a recursive descent interpreter. There are no function parameters and instead there are a fixed set of global variables.

    For postorder, it's more like forth, which I expected to be much faster. I still have 7-bit ops and variable length integers, but each op simply consumes items on the stack and/or pushes new items on the stack. I have to pass in a length parameter to eval to know how many token to consume. I hadn't decided if I wanted a length header or end markers. After benchmarking, the pre-order was an order of magnitude faster.

    edit: added screenshot of IDE prototype since it's likely to change in the near future.
    hl-rgb

  25. jfbastien commented on Jul 26, 2016

    @jfbastien
    MemberAuthor

    Conclusion: fast intrp and polyfill aren't primary goals.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions