113 points · by vg_head · 5y ago · 86 comments
I think that it would be useful to consider the existing (very very very large) body of research on dataflow PL and APIs; e.g. check this paper from 1982 which talks about dataflow, coroutines, for handling file operations:
- https://scholarship.claremont.edu/cgi/viewcontent.cgi?articl...
More recent papers in the field:
- https://hal.archives-ouvertes.fr/hal-02865894/document
- https://library.oapen.org/bitstream/handle/20.500.12657/3772...
and older general overviews of the field:
- http://www.cs.ucf.edu/~dcm/Teaching/COT4810-Spring2011/Liter...
- http://210.47.10.86:8032/200009-3/65492.pdf#page=141
My main concern with this design, even if the code looks quite neat, is that the actual dataflow graph is not reified, but exists "invisibly" as part of the program source through the way co_await calls are sequenced.
This means that it is not possible to do as an user, a static analysis and scheduling of the tasks : it relies on the compiler to perform this. But, this problem is in my experience quite often very use-case-specific and ridden with informations compilers do not possess. Not having the way tasks are interdependent reified in an actual user-manipulable "graph" object means that users won't have the possibility, to, say, perform a topological sort or flow propagation on their async graph in order to extract information on it that could be necessary to perform a domain-specific optimization of said graph (for instance if we know that a set of tasks are dependent on each other in ways that aren't visible on the source code, but are known to the programmers, it would make sense to schedule them on a queue in their own thread).
If you really want you could even convert the graph (fully or partially) to a runtime representation.
The core of the proposals are not about specific classes or functions other than some default algorithm implementations.
Instead they are mostly about defining concepts and protocols so that independent async domains can coexist and iteroperate in the same application.
This is similar to the STL where the most important part are the various iterator concepts and the algorithm interfaces, not the actual algo implementations and the containers.
I would find that to be great, but I don't understand how.
In e.g.
while(i) {
co_await sch.schedule(std::chrono::milliseconds(100));
co_await w.write(--i);
}
how can you get a meaningful "graph" object out of that,
e.g.[wait 100] -> [write 9] -> [wait 100] -> [write 8] -> ...
without actually "running" the code first to go through the co_await, which are the only way to acquire the information of what is to run ?
for instance, let's say that I want to compute "statically" (not in the "at compile time" sense but in the "after knowing the inputs to the program and before actually running the scheduler") how many [write] calls there are to schedule (assuming no cycles / infinite loops).
I don't think coroutines provide (yet) enough introspection to break them down in the same way.
Why it would make sense?
In my experience, dedicated threads only makes sense in very specific use cases. Like dealing with APIs which have thread affinity, or when you want OS to prioritize stuff. Generally, throwing tasks into a shared thread pool is the way to go.
well, yes, those are things that happen fairly often. e.g. there's a lot of work on optimizing code for various arm big.LITTLE things, where you have some algorithm that you want to make sure runs on the "big" cores.
I think for most use cases runtime scheduling is better than compile-time one. Because other processes sharing the hardware, and because tasks take unpredictable time, depends on almost everything including ambient temperature. Not to mention compilers don’t generally know count of cores, outside of a few niches like embedded, iOS and consoles games.
Gesundheit!
> Even though network libraries like NFS and SMB let you treat files on remote machines “as if” they were local, sometimes the connection becomes very slow or goes down, and the file stops acting like it was local, and as a programmer you have to write code to deal with this. The abstraction of “remote file is the same as local file” leaks.
Coroutines look interesting, I just need to get my head around them. All code examples I've seen, just as with ASIO, deal with the happy path only. For example, there's hardly a mention of timeouts and the faff ASIO makes you do to get them to work. If there's no 'read(..., timeout)' function then the API is broken as far as I'm concerned.
Then you'll (probably) need to write your own HTTP parsing code which, when you're half-way through, will make you realise that HTTP is actually not trivial at all to implement correctly. Best of luck writing your own HTTP2/3 engine. Boost.Beast seems like a great attempt at doing it right, but I've never used it. (HTTP needs to die, but that's a rant for another day)
IMO, it's too late for C++ and networking. If you need to read from something simple like a serial device then fine, it's probably ok. If you actually need to talk across the Internet then there are other languages that will do it better than you can.
Having said all this, personally, I know my limits and could never come up with anything better than ASIO.
There's also nghttp2_asio, built on nghttp2, which supports HTTP/2[2].
Overall I agree though that ASIO is a very complex and very low level, and it's a lot of work to use it well.
Sometimes it's easiest just to use libcurl.
[0] https://www.boost.org/doc/libs/1_76_0/libs/beast/doc/html/be...
[1] https://www.boost.org/doc/libs/1_70_0/libs/beast/example/web...
Our use case sends thousands of requests per second per machine.
Same here. I ported a legacy project from linux to windows, and continued to use ASIO (i.e., wrote new code) by inertia since the project already used it. Worst decision ever. If I could start from scratch, I'd just use raw Win32 APIs.
And I agree on HTTP(S). It has become an overcomplicated transport protocol on top of another transport protocol (TCP) and the only (though relevant) problem it solves is that it's allowed through most firewalls by default.
As for HTTP parsing, llhttp does great job. It however lack multipart parsing.
Personally I'd rather use this than something based on Node.js http-parser.
[0] https://www.boost.org/doc/libs/develop/libs/beast/doc/html/b...
[1] https://github.com/nodejs/http-parser/issues?q=is%3Aissue+au...
Like I said, human-readable HTTP needs to die, but it's far too late.
https://twitter.com/Cor3ntin/status/1383875999033565194?s=20
"I am bracing for the inevitable comments about C++ being the most terrible thing since forever :("
2006: "..but something else had happened--and that was back in 1973--[something] which I was very, very unhappy about--and that was the advent of C! It derailed, as far as I was concerned, the advances that I foresaw in compilers. One cannot let the user play with the addresses, and use procedures, and play with pointers--which is pointing to the data--and be able to understand how one can transform the program to run more optimally."
"There was an interesting debate at the SIGPLAN conference in that period--about the relative need for optimization. It was put on the table that it wasn't going to be needed anymore! So. Anyway. You can have your own opinion on that, but I know I have mine, beyond a doubt that it was a huge setback for--in my opinion--languages and compilers--and the ability to deliver performance--easily--to the user."
Well 50 years on we still haven't seen any of these "sufficiently smart compilers", at best they can outsmart C compilers in a few cases with things like generics, so the idea that higher level languages would be faster has been wrong for at least 5 decades. At least you can forgive them for thinking they would 50 years ago, but now it's just ignoring 50 years of reality.
Proper abstractions define exactly what happens in every situation, and design unobservable details out. It is the basis for optimizations that fundamentally cannot observed (except side-channels, like timing and other resource usage). Proper abstractions is why we have fast processors. The ISA is a hard boundary.
<sarcasm>Too bad those shitty engineers in the 70s didn't think of Rust...</sarcasm>
True, Rust didn't exist back then, and yes, some (few!) features in Rust weren't invented and worked out then, like the ownership model.
OTOH the corollary to that statement isn't that C was some shining beacon of good design taking advantage of the then state of the art programming language knowledge. See e.g. https://pastebin.com/UAQaWuWG
C succeeded because a.) Unix, and b.) there were a zillion platforms with a zillion different compilers that could process something like C. They all did weird things and you couldn't actually write portable code for decades, but you could kind of fake it.
Worst contender is Java with
Appendable append(CharSequence csq) throws IOException
where Appendable is also used for a StringBuffer.