7 ms·
Are there any blogs about how Rust is integrated into Firefox (a mostly C++ program) - i.e. how the Rust runtime is invoked, garbage collection etc?
by smegel 9y ago
Are there any blogs about how Rust is integrated into Firefox (a mostly C++ program) - i.e. how the Rust runtime is invoked, garbage collection etc?
- steveklabnik 9y agoRust doesn't have any more runtime than C, and also doesn't have a GC. Servo has bindings into SpiderMonkey's GC so that the JavaScript stuff works properly, but that's only that part. That said, I don't think there are any direct blog posts about it; Firefox's other code just sees Rust as C code, as far as I know. (I don't work on Firefox though so I could be wrong about some details.)
- smegel 9y agoI swear I had read Rust had optional GC at one point. And similarly Rust wanting to own main(). Maybe not then.
- burntsushi 9y agoI don't know the precise history, but at one point, it did have a "gc" type, which I believe was demarcated by the `@` sigil. So, for example, `@T` was a garbage collected pointer to `T`. IIRC, the actual garbage collector was simplistic, and was mostly just reference counting under the hood. At some point (in 2014, I think), the GC type went away. Since then, there has never been any serious talk of adding an optional GC to Rust proper, although many folks have worked/written about writing their own.
- steveklabnik 9y agoThis is pretty accurate; however, a very long time ago there was a tracing GC!
- iopq 9y agoAre you sure? I thought @ was reference counted in 0.6 and then removed after. Was it before that?
- steveklabnik 9y agoI don't have any citation beyond "Graydon told me one time". It was like, very very long ago, possibly before @T even existed.
- Rusky 9y agoIIRC it was a mark and sweep GC in the OCaml implementation.
- steveklabnik 9y agoThat sounds about right.
- jacobush 9y agoWow, I can't believe I have been reading up lately on official Rust documentation and still I was under the impression that Rust had optional GC. Thanks for sorting me out...
- kbenson 9y agoWell, I think is sort of does when you use the Rc types, right? It's just much more explicit than a type modifier, it's a type wrapper that requires requires some level of additional code ornamentation in some instances when using later as well. It's just not really any different than using a library in C++ that offers the same, except that in Rust's case the library is part of the stdlib.
- steveklabnik 9y agoRust had almost everything at some point :) Rust binaries do define a "main", and setting that up is the runtime's job, like C. But you can also make a library that needs no main at all.
- oscargrouch 9y ago> That said, I don't think there are any direct blog posts about it; Firefox's other code just sees Rust as C code, as far as I know. So it means the compiled binary ends up in a simple compatible C ABI? thats pretty nice, if thats the case. But giving the project is using LLVM, even C++ ABI would be achievable without too much effort (i guess).
- burntsushi 9y agoTo clarify, Rust has its own ABI, just like C++ has its own ABI. And just like C++, you can expose a C ABI if you want by defining special functions. In C++, it's an `extern "C" { ... }` block, and in Rust, it's a `extern "C" fn foo() ...` function declaration. You can see an example here: https://github.com/rust-lang/regex/tree/master/regex-capi https://github.com/rust-lang/regex/tree/master/regex-capi Adding C++ ABI support is a significant effort.
- oscargrouch 9y ago> To clarify, Rust has its own ABI, just like C++ has its own ABI. And just like C++, you can expose a C ABI if you want by defining special functions. In C++, it's an `extern "C" { ... }` block, and in Rust, it's a `extern "C" fn foo() ...` function declaration. Ohh, Now i got it. But its pretty good thing to have anyway. C ABI is the lingua franca anyway to communicate to any language, even C++. > Adding C++ ABI support is a significant effort. Ok. So if has its own ABI, im sure it would be a pretty hard undertaking to be compatible with C++. I was guessing if maybe Rust had managed to squeeze and reuse the C++ ABI.. but sure, giving Rust is not that much alike C++ this would probably be a bad decision for small gains. Thanks for clarifying.
- Manishearth 9y agobindgen actually has the option to generate wrappers for C++ methods and other stuff for Rust to call. We don't use this, but we do use its ability to generate templates and stuff. The other way around -- getting Rust to use the C++ abi -- needs compiler changes and also has questionable benefits.
- shepmaster 9y agoIt's not a blog, but Rust Belt Rust 2017 [1] (a conference I help organize) will have a talk "The Story of Stylo: Replacing Firefox's CSS engine with Rust" [2]. The conference is in Columbus, Ohio, and is reasonably priced. [1]: http://rust-belt-rust.com/ http://rust-belt-rust.com/ [2]: http://rust-belt-rust.com/sessions.html http://rust-belt-rust.com/sessions.html
- firebones 9y agoAre there any stats yet on improvements in memory safety within Firefox attributable to Rust. In theory, it could be as much as 50% fewer based on the original premise of Rust removing whole classes of programmer errors, but are there stats from Rust being in the wild? The CSS replacement reminded me that there should be something to compare.
- burntsushi 9y ago> are there stats from Rust being in the wild? I don't know, but if I recall correctly, nobody has ever reported a memory safety bug to ripgrep, or even the underlying regex engine. I don't really know how many people use ripgrep, but it's not zero.
- lordnaikon 9y agoI can assure the lower bound is at least one. Thanks for boosting my productivity!
- brazzledazzle 9y agoI use it everyday, several times per day. Thank you for you such a useful utility.
- ameliaquining 9y agoWell, everyone who's using VS Code is using ripgrep, and a year ago Microsoft said it had half a million active users, and the number has presumably grown since then.
- 9y ago
- mbrubeck 9y agoHi, I'm a Servo developer who worked on some of the Rust code that's in Firefox. Calls between C++ and Rust code in Firefox all go through "extern C" functions. Some of the code involves reference-counted smart pointers. We use RAII wrapper types in both Rust and C++ to ensure that refcounts are incremented and decremented correctly on either side of the FFI boundary. P.S. This old blog post is not about Rust-in-Firefox, but it does cover a related topic: How the Servo browser engine (written in Rust) interacts with the Spidermonkey JavaScript engine (written in C++ and embedded in both Gecko and Servo), including garbage-collected JavaScript objects: https://research.mozilla.org/2014/08/26/javascript-servos-only-garbage-collector/ https://research.mozilla.org/2014/08/26/javascript-servos-on...
- smegel 9y agoCool thanks.
- senatorobama 9y agoAre there any engineering openings at Mozilla?
- Insanity 9y agoYou can check out this page: https://careers.mozilla.org/ https://careers.mozilla.org/