9 ms·
Writing high-performance servers in modern C++
- signa11 11y agono benchmarks. what gives ?
- ansgri 11y agoYeah, and no mention about boost::async, which is in Boost, ie almost standard.
- gamegod 11y agoYou mean std::async (c++11)? http://en.cppreference.com/w/cpp/thread/async http://en.cppreference.com/w/cpp/thread/async
- ansgri 11y agooh, I mean ASIO.
- kinofcain 11y agoFolly, the library underneath wangle, is running on top of boost::asio, so this is basically an abstraction on asio two layers up.
- hellofunk 11y agoASIO header-only implementation is also available without Boost, fyi, as a standalone library; the same one that's in boost, but used without boost.
- neomantra 11y agoFolly does not use boost::asio, but rather libevent: libevent is an excellent cross-platform eventing library. Folly's async provides C++ object wrappers for fd callbacks and event_base, as well as providing implementations for many common types of fd uses. https://github.com/facebook/folly/blob/master/folly/io/async/README.md https://github.com/facebook/folly/blob/master/folly/io/async... grepping the codebase for `asio` brings up nothing.
- kinofcain 11y agoInteresting. Thanks for the correction. I had seen the boost dependency and assumed it was using asio under the hood I guess.
- gaius 11y agoYou mean LOC vs another framework? That would be the only relevant metric in this context.
- ma2rten 11y agoIt says "high-performance servers" in headline...
- jhartmann 11y agoA lot of people state that Python/PHP/Ruby/Javascript on the dynamic language side and Java and other JVM languages on the statically typed side and much quicker to develop than C++, but being a very experienced Java programmer and a decent Javascript programmer I find that I can program just as fast with C++ as long as you use the right choices of libraries and use C++11 or greater. With libraries like Boost, fbfolly, wangle, etc. I find it can be just as quick to develop C++ code as with other languages. I think too many people think C++ is a huge investment above other languages due to past experience, but I feel that it is just not true anymore. Thanks for this post, I think it shows just how quick it is to write performant modern C++, when you stand on the shoulders of other libraries. Just like the other languages.
- Aleman360 11y agoModern C++ is indeed pretty productive. It's the tooling and compile times that hold it back.
- oilywater 11y agoTooling is getting better, CMake is awesome. Modules would reduce compile times significantly but it seems C++17 won't include them, so we'd have to wait a bit longer.
- deleted 11y ago[deleted]
- matthewaveryusa 11y agoPerhaps you mean link time? If you're spending a lot of time compiling you are probably suffering from other problems. Most of C++'s issues with compilation come from needing to re-compile entire .cpp files because of a change in a depending .h file. If you properly forward your class declarations instead of including a header in a .h file, don't do template meta meta magic, and think about physical design a bit, your compilation time should be under control. What I mean by physical design is, if a particular function is prone to be edited constantly, keep that function definition in it's own .cpp file. of course I'll agree that in this day and age, we shouldn't worry about those sorts of things.
- deleted 11y ago[deleted]
- geocar 11y agoThat's a lot of code.
- hellofunk 11y ago>I must warn that it unfortunately doesn’t build on Max OS X as of yet so I recommend to virtualize Ubuntu 14.04 to install it. Important info for some folks (i.e. myself).
- SeanDav 11y agoI don't understand the point of saying something like: > "I show how to build a modern C++ high-performance, asynchronous echo server that can be written with just 48 lines of code." The fact that it is 48 lines of code is almost meaningless. If the framework was a different design it could be done in 1 line of C++ code or 1 line of COBOL. Given the right library/framework, any application can be done with 1 line of code (taking a bit of poetic licence here, but hopefully you see my point). As a discussion about using Wangle in C++, the article has far more merit. Perhaps I am just being overly pedantic.
- ape4 11y agoI agree. Especially because this sort of code seems to be the whole purpose of this framework. I would expect it to take fewer LOC.
- BinaryIdiot 11y ago> The fact that it is 48 lines of code is almost meaningless. If the framework was a different design it could be done in 1 line of C++ code or 1 line of COBOL. Given the right library/framework, any application can be done with 1 line of code (taking a bit of poetic licence here, but hopefully you see my point). I think it's fine to state that; are you ever going to write C++ code with zero included libraries? Probably not. If you write some string manipulation code and you boast that you did it in 10 lines are you going to count it against them that they included std::string? I think saying how many lines of code it took, using the Wrangle framework, shows that it can be powerful and easy to use versus many of the libraries you end up seeing in C++.
- mentos 11y agoIf those frameworks/libraries are battle tested and not one off solutions for the project then I do think it has merit. Imagine building an overpass out of 3 prefabricated concrete pieces; two columns that serve as the base pedestals and 1 beam that spans them. The 48 lines of glue code to me are the nuts and bolts used to tie the pieces together. If the nuts and bolts are structurally sound then I would say our overpass has merit..?
- deleted 11y ago[deleted]
- thepumpkin1979 11y agoI love to read stuff like this, "with a bit of effort I was able to build the whole back-end for my startup using C++", then I start to think: what would it take for me to migrate off Golang to C++? I love to develop in Mac and deploy in Linux/Docker, so whatever stack I pick has to be compatible with both OS's, with this in mind I made a list of libraries I'd need to move my API to C++: * Web Server library * Web Framework-ish library * JSON library for REST interface * Database client library for my database * Integration testing After reading these useful blog posts I guess I'll pick: * Web Server => Facebook's Proxygen * Web Framework-ish library => Facebook's Wangle, but it doesn't seem to work in Mac, that's already a problem, I don't like having to have two dev environments. * JSON library => looks like Facebook's Folly seems to have something for it, let's hope it uses modern c++ constructs and not some obscure templating magic to make it faster than other implementations. * Database client library for RethinkDB: RethinkDB has no official driver and the only community driver public repository has only 22 starts and no CI setup on Github, I'm reluctant to trust this code, this is already a deal breaker to me. * Integration testing => Wangle's documentation is scarce, there wiki is empty and the repo doesn't have a single code sample to take a look at. I can't go to production without testing, another deal breaker. Yes, I agree, I don't have to pick RethinkDB. MySQL and PostgreSQL c++ drivers are very mature to use in production apps. Of course they are, but: * Do these drivers use modern C++ constructs? * Would these drivers block proxygen/wangle IO loop(in case they have one?) * Would I have to use callbacks to make achieve full IO speed? I hate callbacks, there is a reason I abandoned Node.js a long ago. * How do I put all these dependencies together? As with Golang, as much as I hate it's lack of generics and proper inheritance: * Web server: Standard library `net/http` * Web framework-ish: I don't really need a framework/pipeline for it, there are some neat web routers out there that just help me do the job. * JSON library: Standard library `encoding/json` * Database client library: gorethink and almost all the Golang database clients out there have significant adoption with over 700 stars on Github. * Integration testing: Standard library `net/http/httptest` Unlike D, C++ and Rust; the Golang ecosystem is unified, I know I'm able to pick a client library and it'd work with whatever stack I have in place because there's only one scheduler and therefore only one way to do IO, plus it's non-blocking without callbacks. I'm not a fan of vendoring dependencies, but it simply works. In conclusion, I love C++ and even with all the significant progress made in C++11 and C++14, I'm still stucked with Go because it just works.
- thrownaway2424 11y agoI enjoy seeing Facebook libraries and Google libraries in the same program.
- ersoft 11y agoIt's nice that google are using snake case for library filenames and facebook are using camelcase. :) Example: #include <proxygen/httpserver/HTTPServer.h> #include <grpc++/server_context.h>
- MichaelTerry 11y agoAny classical logic is possible, because C++ is a Turing-complete language. That said, of course, how much you can actually do and how fast you can do it depends a lot on the hardware you have available. Not every computer can run fast, high-quality graphics, no matter what language you coded in. THAT said, C++ is considered a relatively high-performance language and is often used for things like advanced game graphics.
- osullivj 11y agoReminds me of Doug Schmidt's ACE Reactor...
- eva1984 11y agoThe API looks a lot like Netty
- Galanwe 11y agoI stopped reading after reading the first line of code containing a virtual function and smart pointers. Nothing is performance oriented in this post. It's just a plain old dummy TCP server like anyone would write.
- dxhdr 11y agoI half agree; it's easy to be cynical. Virtual functions can be a terrible idea... if they are in a hot path. Is EchoHandler::read such a path? Maybe, you really can't say without profiling. If anything using std::string is the red flag for me.
- nodivbyzero 11y agoDid you test your server with 1.000.000 connections?
- dxhdr 11y agoHow about input fuzzing? Did you try crashing it and see how quickly it can recover? Unit tests? What about zero downtime upgrades? DDoS resiliency? Can it utilize all cores on a machine? Scalable? What are the median and max response times under load? Oh it's just a quick demo? Nevermind.
- yelnatz 11y agoTitle needs to be edited to: How to quickly write a basic, modern C++ using Wangle.
- markpapadakis 11y agoFor what it's worth, the network I/O model design+implementation is rarely what makes the difference between high performance, or not, 'servers' -- and that is a mostly solved problem, at least in terms of patterns and practices that are adopted by 'modern' servers (few threads, no more than actual hardware CPU cores, async.multiplexing, etc). High performance is about everything else, including processing actual incoming requests efficiently, reducing block/busy time in threads, caring a lot about memory access/caches. They key IMO is concurrency and migrating blocking operations to other dedicated threads, and this is where an efficient coroutines implementation would help both with accomplishing that task, but, perhaps more importantly, keeping the programming model simple. C++17 will introduce support for reusmable functions ( http://blogs.msdn.com/b/vcblog/archive/2014/11/12/resumable-functions-in-c.aspx http://blogs.msdn.com/b/vcblog/archive/2014/11/12/resumable-... ). This will be a game changer. Now, you can approximate this by e.g creating task abstractions and use lower-level(but slow) setjmp/longjmp functions, or just design tasks/functors that hold state that can schedule other tasks in turn, and so on. It works, but the overhead, mental, but also n terms of processing/executing/scheduling can be too great.