5 ms·
Sorry but can someone explain why C++ by itself can't be good enough for a web framework? OKCupid wrote one back in the day. ALSO: Your example is much slower
by EGreg 9y ago
Sorry but can someone explain why C++ by itself can't be good enough for a web framework? OKCupid wrote one back in the day.
ALSO: Your example is much slower than the same server written in Node.js, haha. Because the language doesn't support evented programming. The language may be fast, but the paradigm of blocking while waiting for I/O is slow!
- pjc50 9y agoAhem. Back in 2001 I was working on what was at the time the fastest webserver in the world: Zeus. It was written in C++ and built around select(), which gave it effectively evented programming. One process per core. Primarily for serving static content, although there was a fastcgi interface and, god help you, IBM Websphere integration. There were a few quirks to the codebase. No STL; instead our own containers and length-based string classes. There was also a "stringrange" type class, which referred to a range of a longer string. So it parsed in the entire HTTP header as an octet array, then created objects to refer to individual headers. Since the input header was immutable this worked beautifully. Considerable effort was put into avoiding unnecessary copies. It also had at least a dozen target platforms; Linux, SunOS, IRIX, and architectures from ARM to Itanium. I'm not sure on how many of those we ever sold a license.
- EGreg 9y agoI remember that server! It's really cool stuff, and select() can definitely be used in C++. But there is no real evented ecosystem for mysql, pgsql, websockets, and all the other types of I/O unless it is all done through sockets. I think all languages can do an event loop, it's just a question of the ecosystem. Javascript is designed around async and Node.js was heavily promoting it from the beginning. So the default pattern is async. That's what I meant. Yes, sure you can do the web server and even middleware as async, but there are many services (redis, memcached etc.) and having async modules for them all is the key.
- simias 9y agoCGI scripts used to be fairly popular back in the day, it was not uncommon to have C or C++ code facing the web directly. The main problem is that web apps have a huge surface of attack, a huge part of any web application is handling untrusted user input. For this reason you're taking significant risks by using a memory-unsafe language. Also web services are rarely CPU-bound, so the added performance matters less.
- gnaritas 9y ago> The main problem is that web apps have a huge surface of attack, a huge part of any web application is handling untrusted user input. That's not actually true; not all web frameworks work by having pages that parse query params on every navigation. Callback based frameworks like Seaside let you write apps where form input is the only attack vector and messing with URL's will only make you lose your session. That pretty much removes the largest attack vector in most web apps.
- simias 9y agoWell you're just delegating the issue to the framework then, it doesn't really change what I said. If the framework is written in C++ it'll have to be written very carefully and thoroughly validated. If the framework is written in a safer language then it's not much different than if you write a web application in, say, Django where you call native C and C++ modules. That's very common.
- gnaritas 9y agoSure, but that's why we use frameworks to begin with, to create such abstractions. My point was the main attack vector many web apps suffer from are merely due to frameworks that leave too much to the developer. Abstracting page to page linking, as Seaside does or as this very website's Arc framework does with callbacks kills that vector by changing the very nature of what links do.
- whowouldathunk 9y ago> ALSO: Your example is much slower than the same server written in Node.js, haha. Because the language doesn't support evented programming. Uhh, how do you think Node.js is implemented?
- EGreg 9y agoIt's not about how the bytecods is compiled. It's about the paradigms the language encourages. Look, even PHP has frameworks to do evented programming: Kraken, amphp, ReactPHP. But it lacks an ecosystem of modules that actually do things in an async way and work with the same framework. It's the ecosystem of modules and the ethos that matters.
- whowouldathunk 9y agoC++ has always supported callbacks and has libraries for promises [1] and event loops [2]. Co-routines (i.e., await) are coming soon. [1]: https://msdn.microsoft.com/en-us/library/dd492427.aspx https://msdn.microsoft.com/en-us/library/dd492427.aspx [2]: https://github.com/libuv/libuv https://github.com/libuv/libuv
- gpderetta 9y ago> ALSO: Your example is much slower than the same server written in Node.js, haha. Because the language doesn't support evented programming. The language may be fast, but the paradigm of blocking while waiting for I/O is slow! there are plenty of event libraries for C++, many predating Node.js (which is written in C++ btw) by decades. There is no reason why the op example can't be implemented on top of such a library. Unless your was sarcasm, in which case Poe's law strikes again.
- EGreg 9y agoThat may be true, but his example is not evented programming. Unless the function he is calling is badly named, I am pretty sure the style he was thinking about was about blocking the thread until I/O completed.
- gpderetta 9y agoI can't know what the OP had in mind, but I don't see anything that would prevent the example to be fully asynchronous. http::repsonse could be the moral equivalent of a future or it could be running on top of a coroutine.
- pjmlp 9y agoMicrosoft also used to have one based on ATL, as IIS module.
- Blackthorn 9y agoYou don't need event programming to be fast. A blocking paradigm is perfectly fine in any language or context that supports fibers. And none of that snippet you are replying to even blocks, so... what?
- kllrnohj 9y agohttps://www.youtube.com/watch?v=bzkRVzciAZg https://www.youtube.com/watch?v=bzkRVzciAZg More seriously Node.js has some pretty major performance problems, like the fact that it's single threaded. You'll make excellent use of one core, but that's about it. It looks great on I/O bound request/second benchmarks, but as soon as your code needs to do meaningful work it doesn't scale anymore. The solution to that is to spawn multiple node instances to make use of the actual machine resources, but now you're on your own and why did you bother with node.js then in the first place?