5 ms·
> Synchronous or asynchronous is something up to the caller to the decide – not to the function itself. async/await aren't strictly necessary, but to avoid the
by naasking 7y ago
> Synchronous or asynchronous is something up to the caller to the decide – not to the function itself.
async/await aren't strictly necessary, but to avoid them you need one of:
1. a sufficiently smart compiler with whole program compilation, or
2. to compile two versions of every function, an async variant and a sync variant, or
3. every async wait captures the whole stack, thus wasting a lot of memory.
Async/await is basically a new sort of calling convention, where the program doesn't run in direct style but in continuation-passing style. This permits massive concurrency scaling with little memory overhead, but there other tradeoffs as per above.
Async/await makes perfect sense for Rust which wants to provide zero-overhead abstractions.
- gpderetta 7y ago4. Pass the incoming continuation as an explicit parameter to the async function and, as long as it never escapes the function (which is equivalent to the async keyword case) or (recursively[1]) any inline child function, the CPS transformation can be performed without full whole program analysis. If the continuation escapes beyond the ability of the compiler to track it, fall back to 3. Annotations can be used to guarantee the 0 overhead optimization. [1] true non-tail recursion is a problem of course, but it is ok for the compiler to give up if it can't statically prove that the recursion depth is less than a documented maximum. edit: rewording
- naasking 7y agoIt sounds like you're suggesting programming in explicit CPS/callback style, which is doable, but doesn't match the goal of the initial post which was to program in direct style and let the callee determine whether it should execute async or sync.
- a1369209993 7y agoI think they're suggesting programming in implicit CPS/callback style. Eg: int bar(int i) { return i+1; } /* bar takes a implicit return address in [esp+0] */ noreturn foo((*ra)(int),int x) { /* where (*)(RetType) is a return address */ int y = bar(x); /* x86 call pushes eip */ ra return bar(y); /* pseudo-tail-call (push ra) */ } int quux(int x) { (*ret)(int); ret = (return); /* get implicit RA */ foo(ret,x); } // unreachable You'd also need to adjust the stack and/or frame pointer though, which is probably going to be the hard part.
- gpderetta 7y agoOh, not at all, I want the compiler to do the CPS transform for me (same as for yield/await) and then hand me the incoming continuation as a first class lexically scoped parameter. For example: convert an internal iterator to an external one (in pseudo C++, yes, yes, I know): // This is a bog standard template function. Cont can be anything // here as long as it is callable. It literally does the same thing // as for_each template<class Cont, class Iter> Cont inside_out(Iter begin, Iter end, Cont yield){ // as long as yield lifetime is confined to the scope of // inside_out; when Cont // ends up being some compiler internal (possibly unique) magic // type it should be possible to CPS convert both inside_out and // for_each. Both rust and C++ monomorphize templates anyway so // instantiating dedicated CPS variant is not a problem. std::for_each(begin, end, [&](auto&& x) { yield(x); }); // for various reasons we need to return the continuation here. A // proper language would tail call to it. return yield; } ... // this is basically call/cc. It uses magic to get the current // continuation, then invokes its first argument with it and the // remaining arguments. The current function need to be // Note: template functions are not first class in // C++, but we take some liberty here. auto cont = continuationify(inside_out, my_vec.begin(), y_vec.end()); while(not cont.done()) std::cout << cont(); if inside_out was not templated, but instead Cont was type erased (a trait object in rust parlance), then continuatinify would need to allocate a dedicated stack and fall back to good old userspace context switching (the compiler is welcome to pierce through the type erasure layer and optimize anyway, but it would not be a guaranteed optimization). Rust is close enough to C++ that it would work in the same way. I've been planning to write a paper for the C++ committee to add fire to the current coroutine controversy and proposing something like the above (which would unify stackless and stackful coroutines).
- jjtheblunt 7y agotyping "async" and "await" are themselves overhead; this contrasts with what the Go compiler abstracts away, isn't it?
- naasking 7y agoGo takes approach #3 that I described.
- __david__ 7y agoGo has a really neat stack allocation technique that makes #3 extremely non-wasteful compared to C and Rust threads. Unfortunately Rust cannot use the same trick as is is llvm based and llvm doesn't support that technique.
- Arelius 7y agoDo you have any links or details about this technique? It sounds interesting and I'd like to learn more.
- bbatha 7y agoGo still needs to fallback to heavy stacks for c-ffi. In fact that’s the main reason rust dropped stackful coroutines, because you lose zero-cost ffi to c.
- kibwen 7y ago> Unfortunately Rust cannot use the same trick as is is llvm based and llvm doesn't support that technique. This is a bit mistaken; ancient versions of Rust (but not so ancient that they weren't using LLVM) were, like Go, everything-is-implicitly-async, and Rust used the same strategy as Go to manage stacks (to the extent that the old language reference listed Go in its (rather long) list of precursors, specifically calling out its split stack approach). The ultimate reason that Rust switched away from Go's approach is for interoperability with C. If your stacks are the size of C stacks, and if your language doesn't feature a pervasive runtime, then interoperability with C is nearly trivial, and this was something that Rust dearly desired (and has benefited greatly from, IMO). Ultimately one should not look at Rust's chosen approach and assume that it represents some grand rebuke of alternative approaches to asynchronicity. If you have different constraints, then a Go-style approach is lovely. For a language at the level of Rust, different tradeoffs may dominate.
- pron 7y agoNone of the things you mention is necessary. In fact, we're adding user-mode threads (based on delimited continuations) to the JVM without doing any of them. Remember that every subroutine must already be compiled into a state machine or you wouldn't be able to return to it. I.e., every subroutine is already compiled in just the right way to be suspended already. The reason some languages run into problems are: 1. They may not have access to the representation the subroutines store their state, as it may be platform dependent (e.g. if you're targeting LLVM you may not have access to that representation). 2. They may not track their pointers, and so find it hard to move chunks of stack around if they have internal pointers into the stack. 3. They may want to enable some specific optimizations in the special (and rare, especially when IO is involved) case of a monomorphic calls to shallow coroutines. This requires that the representation of the subroutine's state be available as a high-level node for the compiler to change.
- AaronFriel 7y agoThe JVM doesn't ever lose access to the bytecode, but Rust compiles down to machine code. The garbage collector and heap-oriented memory allocation without borrowing means you don't have nearly as much "stack" to worry about, and copying references is always OK. For a statically compiled language that produces machine code and prefers stack allocation and has strict constraints on copying references, it is not so easy.
- pron 7y agoBytecode is irrelevant, but yeah, as I wrote, internal pointers into the stack do make moving stacks around difficult.
- AaronFriel 7y agoIt isn't irrelevant for point #2 above yours, which is that the JVM could rewrite and run/JIT two versions of every function. The JVM monomorphizes plenty already, does it not?
- 7y ago
- hinkley 7y agoI was about to say we are essentially talking ourselves back into green threads but I think you may be right about CPS. However, the paradox you have to manage here is this: if you make this sequential/parallel determination pretty high up in the code so that there is a lot of sequential code right after an async part (async can call sync all it wants), then very little of your code is altered, but now you have the green threads problem - one long CPU-bound task that doesn’t yield. At least with Rust, if you’ve partitioned your data properly then there can be several threads of execution running tasks on unrelated data, but in Javascript you will be punished for trying to consolidate the async bits in this fashion.
- deleted 7y ago[deleted]