101 points · by chrismorgan · 12y ago · 157 comments
I am a big fan of Rust, but as it aims to be a better C++, it is probably a better fit for OS development, game engines, embedded systems, databases, complex desktop applications (Photoshop/Word/Chrome), etc. While Rust is quite expressive for a systems programming language, its banner features are the borrow checker (+ lifetimes, etc) and powerful static type system. Rust emphasizes zero-cost abstractions with compiler-enforced memory & thread safety. The popular web development languages are dynamically typed and interpreted, with an emphasis on rapid development, which is a very different niche than Rust claims to fill.
Perhaps Rust could be used to build a highly optimized server for a large site, but at that point it would probably better to write the application to run on the JVM. Although the JVM may be slower (within a constant factor, and assuming Rust is eventually as fast as C++) and more memory-intensive than the optimized native code produced by Rust's LLVM based compiler, the JVM's extensive tooling and run-time profiling tools make it a natural choice for high performance web servers.
Web development is horrifically, unbelievably bent around two facts:
1. There has been no convenient, highly popular and supported language for development that also has best-of-breed performance. You can have one or the other. (Yes, I've probably heard of your obscure fast-as-C language that has a great web stack... to a first approximation, nobody else has.)
2. Web development languages have been profoundly, deeply single-threaded, to the point that the web world began to take it as an unmitigated virtue that a page started with a request, generated a response, and dropped everything in the world that it knew at the end of the request. State is something that must be managed carefully, but especially in this era of people writing web applications that may literally have gigabytes available per concurrent user (obviously, not everybody, but this is a non-trivial use case!), the statelessness dogma has seriously broken people's mental models vs. what the hardware is actually capable of.
With Rust, you should be able to write fast, concurrent web applications that actually do something concurrently and aren't constantly marshaling across requests for the client to re-initiate AJAX calls or other such silliness, it'll be fast, and with its emphasis on memory safety, statically safe as well (something that you're not going to get very easily out of Java, and certainly not from a programmer just sitting down and pounding out some Java). It isn't going to be the solution to every problem, but it's going to be the solution to more problems than people realize right now, because we've so deeply internalized our essentially-PHP models of how the web works that we can't hardly even see how things ought to actually be working.
Rust isn't the only language biting at this apple. Go is obviously another one. But Rust has a pretty decent shot at taking a big chunk, if it plays its cards right.
You say the popular web development platforms are all dynamically typed... I'm increasingly coming around to the position that was an accident of history, rather than an immutable fact.
According to http://www.techempower.com/benchmarks/, the 2nd position is held by a Java framework, losing only 13,5% to the C++ overall winner. Granted, you need a 3rd party library to have something similar like goroutines / fibers / actor concurrency / whatever, but its all there if you want it.
And neither are as safe as Rust is/will be with multithreading. Doing really pervasive concurrency is possible, but not particularly easy with those languages.
Some of the top contenders:
* cpollcppsp (C++)
* go (Google)
* hhvm (PHP JIT by Facebook)
* openresty (LuaJIT on Nginx)
* undertow (Java)
* gemini (Java)
This is courtesy of JVM not having value types, and that's just one obvious performance dead end if you write code that deals with that sort of thing (both in memory usage and in access time/runtime). There's plenty of other stuff that makes JVM not suitable for performance sensitive work, as long as we aren't defining performance sensitive as "faster than Python and Ruby".
Web apps seem to be IO bound so runtime performance generally doesn't matter.
It's not "highly popular" compared to something like Python, but has growing usage and attention. The implementations are converging on a standard set of modern features that are cross-platform. I routinely write code in one implementation/OS and run it on different implementations/OSes without a hitch.
Let's not kid ourselves. CL will never get anywhere regarding adoption for web use (at least for values of "never" = in the next 20 years, and "anywhere" = several high profile companies use it, and there are at least 3 dedicated books about its web frameworks).
Almost all programming languages can claim "growing usage and attention", but it rarely amounts to much. Go has managed to have more usage and attention from high profile companies in 3 years of existence that CL or Smalltalk have had in the last 25.
Haskell and Scala?
People typically associate web servers with dynamic languages. I am ready to challenge that notion; there are various things that people don’t think of that a language like Rust makes possible, and those sorts of things are the focus of my talk at Strange Loop this year, in September: https://thestrangeloop.com/sessions/fast-secure-safe-the-web...
The idea of a server that starts up fast, is fast from the start, and has minimal memory usage is appealing to me, but I suspect that it's irrational to optimize for these things except in niche applications like web interfaces in routers.
I am a big fan of Rust, but as it aims to be a better C++, it is probably a better fit for OS development, game engines,
If there was an easy way of using Rust as a websocket server, I would be using that for my project instead of Go. The option to use reference counting is a Big Win when it comes to high degrees of parallelism on multicore processors. When you have GC, the GC thread comes along and causes cache misses for all of the other threads running on different processor sockets. With reference counting, garbage is "collected" locally.
There are working zeromq bindings: so you can use mongrel2 to handle websockets (with or without TLS) and then service those requests with a handler written in Rust!
You have to write the handshake & protocol framing yourself; but there's a reference implementation in Python[1] that's only ~200 lines long and supposedly passed the Autobahn test suite.
Rust's model of concurrency also jives really well with mongrel2's architecture.
---
There's one gotcha: receiving on a zeromq socket through the C bindings blocks the OS thread. This will cause your program to deadlock if you're using Rust's green threading. (Native "1:1" threading is the default however.)
[1]: https://github.com/zedshaw/mongrel2/tree/master/examples/ws
In Rust, from what I've seen, you basically have the option to avoid heap allocations almost entirely which is fantastic for performance. The only other languages that come close to giving you that ability in a very explicit and deterministic way that I know of are C and C++.
Not really. Any time you using a `Vec<T>` or a `String` type, you'll be heap allocating.
I suspect what you meant to say is that Rust does not force programmers to use some sort of garbage collection (whether it be reference counting or otherwise). That is indeed the case. I've written a fair amount of Rust code and haven't needed to use a GC at all.
This is also the key to programming performant Go. However, you can't really entirely avoid heap allocations, just minimize them.
Modern generational GCs are reasonably nice on caches in most cases.
We can also end up with things like HTML templates compiled into your binary and the HTML validated at compile time. Even things as precise as type checking on something like <time datetime="{{ time }}"> can be managed with Rust. And I intend to.
I’ll be talking about how some of these sorts of things can work out at Strange Loop this year: https://thestrangeloop.com/sessions/fast-secure-safe-the-web....
These things are already done in several of the Haskell frameworks. There's even Blaze, which is a Haskell DSL that allows you to write HTML entirely in Haskell. Personally, static verification of HTML doesn't seem that necessary, but I suppose it has its merits (possibly for security?).
I'm not ready to say that Rust's type system lends itself perfectly well to ORM's, as I haven't really seen an ORM as convenient as ActiveRecord in any statically typed language. (This is one of the reasons I like both statically and dynamically typed languages.) But I'm happy to see people try, of course, and I'd love to be surprised.
http://ocharles.org.uk/blog/posts/2013-12-06-24-days-of-hack...
More in-depth interactive tutorial:
https://www.fpcomplete.com/school/starting-with-haskell/libr...
That talk looks really nice too, a shame it's in several months.
Continue the good work !
I’m definitely in with Rust and HTTP for the long haul. Hang round, things will get better.
I don't understand what the type system has to do with building a fancy ORM. Do you mind elaborating?
However, due to the JVM nature, one could use some of the well-tested and well-defined Java Web Framework from Clojure (with a lightweight wrapper, of course). I'm not really sure if people are actually doing that in production, though. [6] [7]
[1] http://caribou.github.io/caribou/docs/outline.html
[3] http://www.luminusweb.net/
[4] https://github.com/pedestal/pedestal
[5] https://news.ycombinator.com/item?id=7472841
[6] https://github.com/apache/tapestry-5/tree/master/tapestry-cl...
Clojure has Korma[0], Common Lisp has Crane[1] and Integral[2].
[0] https://github.com/korma/Korma
It's fast, expressive, and the modern Mono+ASP.Net web-stack is open-source (older stuff is still proprietary).
EDIT: I said not Go, because I don't like its type system, and whenever I have to cast something to interface I have a strong flashback from the old Java 1.3 days. On the other hand. It's purely personal opinion.
(I also love have fallen in love w/ how mongrel2 handles websockets. I find it fits very well w/ Rust's message passing between tasks.)
So it's not like writing a webservice in Rust is completely impossible. I will admit that "we aren't web yet," but I'm glad to see the community working on it.
Mongrel2 is a really good way to have all the nitty-gritty of http handled while you worry exclusively about two ZeroMQ sockets (from and to Mongrel2). It makes it a whole lot easier and safer.
Discovery of Rust projects is super hard right now. I'm quite into both REST and Rust and didn't know about your framework :/
Good point! But I'm not sure of any other way to get projects noticed besides word of mouth and posting about them on sites like Hacker news and such. In fact that is where I first heard about rustful.
> didn't know about your framework
My framework is actually at https://github.com/jroweboy/oxidize/tree/incoming (master branch is sorely out of date) and I feel I still have a long way to go before I get that finished :)
As Go and Rust share a lot of similarities, what is it about Go that's so unappealing for this use case?
It seems to me that Go and Rust are much more different than alike. Go has GC, Rust has lifetimes. Go has a simplistic type system, Rust has a sophisticated one. Go allows shared mutable state, Rust does not. These are pretty deep differences.
Absolutely, but it's not a reason to not use Go. The web is probably the platform with the least possible damage caused by GC.
It's OK if people like the language and want to use it for web development, though. I'm personally pretty excited about it, because I'm not a huge fan of Go and Rust looks pretty neat.
My experience has been that Go has very lightweight GC. The compiler intelligently turns a significant number of allocations into stack allocations (the same allocations would be heap allocations in .NET and Java, by comparison). It, by design, minimizes the amount of work that the GC needs to do, and in most real projects it just becomes a complete non-issue.
And
"The current implementation is a parallel mark-and-sweep collector but a future version might take a different approach." (which I'm certain it will)
Have you done a back of the envelope calculation to justify this? I have a Javascript/browser MMO engine backend that's written in Go. I simply invoke the garbage collector every world-tick (8 times a second) and that works great for me. I can manage a huge world with GC pauses as large as 20ms.
That said, I can't wait for rust teepee server to land, it shapes up to be a rather interesting project.
(BTW, I’m curious: where did the spelling TeePee come from?)
(Link currently down)