4 ms·
i think that if someone is really interested in this topic, they'd do well to read mark s. miller's doctoral dissertation on it, 'robust composition': http://ww
by kragen 3y ago
i think that if someone is really interested in this topic, they'd do well to read mark s. miller's doctoral dissertation on it, 'robust composition': http://www.erights.org/talks/thesis/ http://www.erights.org/talks/thesis/
this is relevant because miller explains why he designed promises the way he did, although since he wrote the dissertation in 02006, he couldn't yet explain how he adapted them for incorporation into the javascript standard. following his dissertation, he was on the ecmascript committee for a number of years, which is how we got e-style promises in javascript, although obviously that also involved the committee as a whole being convinced that they were a good idea. (much of the dissertation is actually about object-capability security, but chapters 13–19 are about promises.)
the great benefit of promises is that you eliminate data races, where one procedure is correct only if a particular piece of mutable data remains unchanged, while another concurrently runnable procedure changes it, so the interleaving of their accesses can result in a failure. consequently, you don't need locks or queues to communicate between different concurrent procedures; you can just use a shared data structure with no locking. this is because promises are an alternative to threads. if you're familiar with async rust, you will note three glaring problems with this:
1. rust eliminates data races through the type system, so the great benefit of promises doesn't exist in this context
2. tokio runs multiple tasks concurrently, so if you do have data races, promises won't save you
3. so you do in fact use channels and locks in async rust
so i am puzzled about why you would ever want to use promises in rust, and i guess this post is an attempt to answer that question. it explains why people have this bizarre misconception that promises are just an efficiency hack, because in rust that's all they are, and those people have never used promises in javascript or e or the other children of e such as spritely goblins.
this bears repeating. before reading this post i didn't realize this, but rust async/await is just an efficiency hack. even the section of this post entitled 'i don't want fast threads, i want futures' never brings up, as far as i can tell, any correctness or flexibility advantages for async rust, only efficiency advantages ('to multiplex arbitrarily many perfectly-sized tasks on a single thread', 'much better performance characteristics' etc.) there's brief lip service to concision ('concurrency-related boilerplate about spawning threads', which i don't think actually exists) before returning to the main theme of performance. then it proposes some truly astoundingly terrible ideas about automatically introducing race conditions if you await with a lock held, in the name of being 'even more optimal', at which point we're well into 'mongodb is web scale. if /dev/null is web scale i will use it' territory
a terminological quibble: i wish people wouldn't refer to promises as 'futures'; a future is, or anyway originally was, a different approach to the concurrency problem, one where reading from a perfectly normal variable would block your thread until that variable's value had been computed (allowing other threads to run). this approach does not have the advantages of promises. promises are what async rust implemented, just under the name of 'futures'. really, though, confusing terminology seems to be the least of the problems with rust's implementation of the paradigm
- Rusky 3y ago> 1. rust eliminates data races through the type system, so the great benefit of promises doesn't exist in this context I don't know about this one. Programmers who use async Rust often talk about how they appreciate that `await` is explicit because it lets them coordinate state using cooperative scheduling. This is maybe a bit higher-level than just data races, but it is about avoiding certain kinds of race conditions. > it explains why people have this bizarre misconception that promises are just an efficiency hack, because in rust that's all they are I'm not sure about this either. The post argues that they are more than an efficiency hack in Rust, too- that intra-task concurrency is a useful control flow structure and promises are what enable it. That intra-task concurrency happens to share a thread is a performance benefit but that's incidental to its flexibility benefits. > promises are what async rust implemented, just under the name of 'futures'. Well, it's not like Rust's promises are exactly like JavaScript's promises either. JavaScript promises are continuation/callback based; Rust's are poll-based. In a sense they're like your earlier definition of "futures," except that the future API lets its caller decide how to block when the result is not yet available.
- kragen 3y agoi appreciate the feedback, and you could be right; do you have an example of how you can coordinate state using await? i don't agree with your implication that data races are a low-level thing. they can be, but the canonical example of a data race is a lost write to a bank account, which is not just layer 7 (the application layer) or layer 8 (the user) but layer 9 (the economic production system that employs the user) > JavaScript promises are continuation/callback based; Rust's are poll-based this is obviously important for the implementation of a so-called async runtime such as tokio, but as i see it, from the point of view of most of the user code, this is an implementation detail. i was talking about the semantics promises provide to things like an http client, not how those semantics are implemented > the future API lets its caller decide how to block it's not clear whether in this phrase you are referring to the real future api or to what rust confusingly calls 'futures'. in case you mean the former, i want to clarify that, in the futures interface defined in 'multilisp: a language for concurrent symbolic computation', (halstead, 01985, https://dl.acm.org/doi/pdf/10.1145/4472.4478 https://dl.acm.org/doi/pdf/10.1145/4472.4478), you cannot distinguish a future from the value that it eventually produces, and therefore the caller cannot decide how to block; it doesn't even know it's blocking. as halstead explains: > An operation (such as addition) that needs to know the value of an undetermined future will be suspended until the future becomes determined, but many operations, such as assignment and parameter passing, do not need to know anything about the values of their operands and may be performed quite comfortably on undetermined futures. The use of futures often exposes surprisingly large amounts of parallelism [now conventionally called 'concurrency'] in a program, as illustrated by a Quicksort program given in Figure 1. (...) > These “futures” greatly resemble the “eventual values” of Hibbard’s Algol 68 [39]. The principal difference is that eventual values are declared as a separate data type, distinct from the type of the value they take on, whereas futures cannot be distinguished from the type of the value they take on. This difference reflects the contrast between Algol’s philosophy of type checking at compile time and the Lisp philosophy of run-time tagging of data. i would claim that, since halstead's paper has been cited 1658 times in google scholar, including 25 times in the last year, his definition of 'future' is very much in current use, and so it is sort of an act of intellectual vandalism to promote a conflicting but confusingly similar definition