7 ms·
Node.native - Node.js in C++0x
- paramaggarwal 15y agoI would like to understand the implications. Does this improve performance? Or does it just allow coding in C++. Please help. (Sorry, noob here.)
- rmgraham 15y agoIt allows coding in C++ in a fashion similar to Node by taking advantage of the lambda syntax added in C++11. It also makes use of libuv and http-parser, which are used in Node, to offer some of the same out-of-the-box functionality.
- deleted 15y ago[deleted]
- exDM69 15y ago> I would like to understand the implications. Does this improve performance? Or does it just allow coding in C++. This is practically a C++ wrapper for libuv, the backend of node.js. It allows you to write code similar to node.js in C++ without using JavaScript and the V8 JavaScript environment. Does it improve performance? In theory, yes. In practice, maybe. The biggest potential win here is that you could run many threads that can run I/O in parallel. In Node.js/V8 you only have one native OS thread while in C++ you can put many native threads to serve the I/O sockets. It's quite a lot cheaper to change from one thread to another than it is to change from one process to another (if you have many node.js processes running) so there is potential for performance increases if you have some CPU-heavy stuff in your code. Or you have more sockets/requests to serve than a single CPU is capable of. This is assuming that libuv and node.native don't have anything that would inhibit running in multiple threads. The system calls running underneath (read, write, epoll/kqueue) are thread safe, but libuv/node.native may have something that isn't. I didn't double-check.
- johnx123-up 15y ago(I submitted the link, but...) Do you think a C port will improve performance over C++? Google only returns wishlist something like this http://rajeshanbiah.blogspot.in/2012/01/nodec-la-nodejs.html http://rajeshanbiah.blogspot.in/2012/01/nodec-la-nodejs.html
- alexchamberlain 15y agoA properly coded C++ app should run as fast as C; it can be faster...
- lmm 15y agoNot while C++ doesn't have restrict pointers
- alexchamberlain 15y agoYeah, all performance benefits of C come from a single under used feature. </sarcasm>
- padenot 15y agoWhile the standard does not specify a restrict keyword, it is practically available in quite a lot of compiler.
- timdot 15y agoWhilst C++ can be as fast, or faster, than C - it's usually not because it's "properly coded". Many of the abstractions that C++ provides are what hit performance and it's usually by avoiding these that you can match performance with C. http://unthought.net/c++/c_vs_c++.html http://unthought.net/c++/c_vs_c++.html
- cube13 15y agoIn all honesty, probably not. Node.js is too thin to really make a C port all that worthwhile. Unless the dispatcher is extraordinary slow, the major performance issues are going to be in the userland code more than the actual Node code.
- deleted 15y ago[deleted]
- simfoo 15y agoGreat to see libuv being used in the wild (outside of Node.js). It's a nice little library that does one thing and does it good.
- noj 15y agoFWIW it's also used in the Rust runtime (https://github.com/mozilla/rust/tree/master/src https://github.com/mozilla/rust/tree/master/src)
- exDM69 15y agoThis is quite similar to boost.asio library, which has got sockets and timers. I know for a fact that boost.asio is thread safe and you can use several threads running the same I/O service allowing you to multiplex n sockets to m threads. Do you guys know whether libuv (and node.native) are thread safe or not?
- shaka881 15y agoNo kidding, asio is fast and battle-tested. I only see risks associated with being an early adopter this. The Node hype is a deafening roar.
- drivebyacct2 15y agoI'd kill to have this in Go instead. Looks like it's a wrapper around libuv, shouldn't be hard with cgo. Hm, maybe another thing to add to the list. edit: it appears that I'm confused as to what this does exactly, my bad. Egg, all over my face.
- exDM69 15y agoDoesn't Go have it's own asynchronous I/O system built into the language runtime? I was under the impression that Go has I/O multiplexing w/green threads similar to Haskell. So when you write socket.read(), it will be put into an epoll/kqueue behind the scenes and a co-operative green thread switch occurs. This way you don't have to write dirty I/O callbacks like in Node.js or Boost.Asio but you get the same effect with neater code. So if Go has this feature, using libuv would not only be useless, it would be harmful because it doesn't play nice with the language runtime w.r.t. parallelism. I think that writing asynchronous parallel code should be done by the compiler and the runtime, not by the coder. Node.js code is a mess of callbacks that have to be written in continuation passing style by the user. Haskell (or Go?) code that has the same effect is written like regular imperative code, which is transformed into Node-style callback code by the compiler and multiplexed by the runtime. Compilers are excellent in doing this kind of transformations.
- jbarham 15y agoAlmost, but not quite. From http://golang.org/doc/effective_go.html#goroutines http://golang.org/doc/effective_go.html#goroutines: "Goroutines are multiplexed onto multiple OS threads so if one should block, such as while waiting for I/O, others continue to run." So the Go runtime doesn't automagically transform blocking system calls into non-blocking calls, but if one goroutine is waiting for I/O, other goroutines can run in the meantime. But you're right in that with Go you can write simple, easy to debug imperative code, but at the same time confidently spawn 10's of thousands of goroutines because they're so lightweight compared to conventional OS threads. The Go runtime handles the bookkeeping of multiplexing goroutines over real OS threads.
- 15y ago
- ronreiter 15y agoWow, great work. Really good for both CPU and IO critical missions (a.k.a people who are cheap on servers)
- gritzko 15y agoWhat is the dramatic advantage of Node.native over using libev/libevent directly?
- johnx123-up 15y agoOther projects around libuv: 1. Mozilla's Rust http://www.rust-lang.org/ http://www.rust-lang.org/ 2. Tim Caswell's LuaNode https://github.com/ignacio/LuaNode https://github.com/ignacio/LuaNode 3. Ben Noordhuis and Bert Belder’s Phode async PHP project https://github.com/bnoordhuis/phode https://github.com/bnoordhuis/phode 4. Kerry Snyder’s libuv-csharp https://github.com/kersny/libuv-csharp https://github.com/kersny/libuv-csharp 5. Andrea Lattuada's web server https://gist.github.com/1195428 https://gist.github.com/1195428 6. Steve Yen's Lua wrapper https://github.com/steveyen/uv https://github.com/steveyen/uv Source http://blog.nodejs.org/2011/09/23/libuv-status-report/ http://blog.nodejs.org/2011/09/23/libuv-status-report/
- yesbabyyes 15y agoNice list, though that's not Tim Caswell's project; this is: https://github.com/luvit/luvit https://github.com/luvit/luvit
- tete 15y agoUsing Moonscript (Lua's CoffeeScript) the performance benefit of LuaJIT when compared to all JavaScript implementations makes it a really interesting. Well, actually Lua doesn't look bad, even though it really needs syntactic sugar for OO, but there is enough of it. Not just Moonscript, but also Loose (Perl's Moose ported to Lua).
- saghul 15y agoShameless plug ;-) Python bindings for libuv: https://github.com/saghul/pyuv https://github.com/saghul/pyuv
- tete 15y agoNice list. I hope libuv gets a wiki that lists all of them. Oh and does anyone know a Perl one?
- Animus7 15y agoA cool hack, but I can't help this feeling pointless in practice. It solves the problem of using Node userland code to do heavy computation. But a) serious developers know better than to do heavy computation inside of Node anyway, and b) it comes at an enormous, almost unforgivable cost: working with C++ is a nightmare compared to working with Node when it comes to realtime web projects (I've been in those trenches) What would be exponentially more useful in practice is a concise, well-documented way to hook (multithreaded) C++ code into Node's engine. But that wouldn't be nearly as sexy as a promise of Node, but with native performance.
- _stephan 15y agoWithout commenting on Node.native, I'd just like to note that C++11 isn't your grandparents' C++ anymore. An experienced programmer probably can write code in C++11 that is almost as elegant as, say, the equivalent C# code. And for larger projects many people would prefer something like C# to JavaScript, if only for the static typing.
- baq 15y agothe problem isn't writing code, the problem is build environment and deployment - correct versions of compilers, headers, libraries, makefiles, cmakelists, wafs, scons', bjams, whatevers...
- alexchamberlain 15y agoIs it really an issue? Are you proud of the fact that you can't sudo apt-get a few packages? I'm not a fan of moving away from Makefiles, however. Makefiles are sooo simple, why complicate it?
- baq 15y agosudo apt-get a few packages is great when it works, but if you want/need a newer boost version, you're on your own. Makefiles are tedious to write and maintain in anything but a toy application. That's not to say everything else isn't, but most of the time you can save yourself some hassle.
- lperrin 15y agoIt might be useful to use node-like patterns in platforms where node is not available (iOS comes to mind).
- ramanujan 15y agoThis is a little off topic, but thought I'd ask the C++11 aficionados who're viewing this thread. For those who've taken the hit and really explored C++11: 1) What are the best resources to learn how to code C++11 in a Pythonic style? Looking for a book or production source code example that uses the new language features idiomatically: lambdas, vector and dictionary literals, and the like. 2) What does your toolchain look like for C++11? Just the latest gcc and latest emacs, plus maybe a few tools like makeheaders[1] and gtags[2], or something more? [1] http://www.hwaci.com/sw/mkhdr/ http://www.hwaci.com/sw/mkhdr/ [2] http://www.gnu.org/software/global/ http://www.gnu.org/software/global/
- exDM69 15y ago1) What are the best resources to learn how to code C++11 in a Pythonic style? Looking for a book or production source code example that uses the new language features idiomatically: lambdas, vector and dictionary literals, and the like. I don't think you can find good books or comprehensive resources at this time. There are various articles and blog posts detailing the C++11 additions to the language. Of course there's the spec but it makes very boring reading. Not a whole lot has changed so you can pretty much jump right in and use the new features as you need them. std::thread and std::chrono are pretty sweet. 2) What does your toolchain look like for C++11? Just the latest gcc and latest emacs, plus maybe a few tools like makeheaders[1] and gtags[2], or something more? I used to build gcc from git sources, but these days my operating system package manager ships with a gcc version that has most of the C++11 stuff I use. Just add -std=gnu++0x to your CFLAGS. FWIW: I use vim + ctags + cscope.