Search code examples
lazy-evaluationwebassembly

Lazy evaluation in WebAssembly


Would it be a kind of lazy evaluation if const values are passed through functions instead of creating them in place?

Simple example:

(module
    (func $let3.5 (result f64) f64.const 3.5)
    (func $let2.5 (result f64) f64.const 2.5)

    (func $addLazyNumbers (result f64)
        (call $let3.5)
        (call $let2.5)
        f64.add
    )

    (export "addLazyNumbers" (func $addLazyNumbers))
)

And then call module.exports.addLazyNumbers() in JS (returns 6).

In a more complex example, there would be blocks with br_ifs and some of the values would not be needed.

Or is this just a overhead because the values 3.5 and 2.5 are already in memory after compilation?


Solution

  • Or is this just a overhead because the values 3.5 and 2.5 are already in memory after compilation?

    It is just overhead, yes.

    Engines compile consts directly as part of the executable code, and adding functions in-between only means that now the engine has to do more work both during compilation phase (to encode the new functions), as well as during execution (to actually invoke them and get results back).