4 ms·
Not to be a curmudgeon but why would one want to use Rust for web development for a non-toy project? I am a big fan of Rust, but as it aims to be a better C++,
by stcredzero 12y ago
Not to be a curmudgeon but why would one want to use Rust for web development for a non-toy project?
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.
- fulafel 12y agoRef counting is murder on caches. Every read will cause a rmw on the ref count cache line. Modern generational GCs are reasonably nice on caches in most cases.
- dllthomas 12y agoEvery handoff, not every read, right? Of which there might be more or fewer.
- AlisdairO 12y agoThis is not so much the case in rust because you only increment the reference count when you're passing around copied refs to an item. If you instead borrow a ref counted item, you don't need to increase the ref count - because rust will statically enforce that the borrow lasts less time than the item it borrows from (which is enough to keep the item alive). With good coding practices, you can eliminate the large majority of ref count increments/decrements.
- yuriks 12y agoSuch cache line will (because it is adjancent to the refcounted data) almost always be in cache anyway. Rust's refcounting is much cheaper than C++'s refcounting because it doesn't need to be atomic. (There is a separate type for atomic RC, and it is enforced by the compiler that you use it when needed.)
- bfrog 12y agoNot everything is reference counted in Rust. Doing stack allocations in rust is actually really easy. Stack allocations in case your not aware are incredibly fast compared to anything else. The only thing faster is static allocations. 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++.
- burntsushi 12y ago> In Rust, from what I've seen, you basically have the option to avoid heap allocations almost entirely which is fantastic for performance. 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.
- pcwalton 12y ago> Not really. Any time you using a `Vec<T>` or a `String` type, you'll be heap allocating. Well, we have the SmallVec type to avoid this if you can bound the size of your vectors. I would also like to introduce scratchpad allocators at some point, which should allow you to avoid heap allocations even for those. Either way, there are many ways you can avoid heap allocations entirely. For example, my NES emulator does not call malloc() during gameplay, at all.
- burntsushi 12y agoOh cool, I didn't know about `SmallVector`! That's clever. > Either way, there are many ways you can avoid heap allocations entirely. For example, my NES emulator does not call malloc() during gameplay, at all. Definitely. Executing searches from `regex!` expressions won't heap allocate either. :-) (Sans the return value, but maybe that can be avoided too.)
- 12y ago
- drbawb 12y agoI managed to get Rust wired up to websockets pretty easily. 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 https://github.com/zedshaw/mongrel2/tree/master/examples/ws