3 ms·
wasm functions must have a fixed number of results[1]. Lisp functions may have variadic results. A wasm function call must explicitly pop every result off of
by aidenn0 5y ago
wasm functions must have a fixed number of results[1]. Lisp functions may have variadic results. A wasm function call must explicitly pop every result off of the stack[2].
These rules add significant overhead to implementing:
(foo (bar))
Which calls foo with just the first result of bar, and the number of results yielded by bar is possibly unknown.
In psuedo-assembly for a register machine, implementing this is roughly:
CALL bar
MOVE ResultReg1 -> ArgumentReg1
CALL foo
And this works regardless of how many values the function bar happens to return[3].
Any implementation that is correct for any number of results of "bar" will be slow for the common case of bar yielding one or two results.
1: https://webassembly.github.io/spec/core/syntax/types.html#syntax-functype https://webassembly.github.io/spec/core/syntax/types.html#sy...
2: https://webassembly.github.io/spec/core/exec/instructions.html#function-calls https://webassembly.github.io/spec/core/exec/instructions.ht...
3: Showing only the caller side of things, when the caller doesn't care about extra values hides some complexity of implementation of returning values because you also need to be able to detect at run-time how many values were actually returned. e.g. SBCL on x86 uses a condition flag to indicate if there are multiple values, because branching on a condition flag lets you handle the only-1 value case efficiently.
- titzer 5y agoOk, I see. Yes, there is some overhead if you want not just multiple returns, but variadic returns. Most virtual machines and programming languages make this fairly inefficient because of fixed arity of returns. AFAICT, targeting anything other than machine code is going to cost you in this case.