3 ms·
In Rust we use the system libc with segmented stacks, and we call C code by switching stacks. (This has been observed to get us into trouble with the indirect b
by pcwalton 15y ago
In Rust we use the system libc with segmented stacks, and we call C code by switching stacks. (This has been observed to get us into trouble with the indirect branch predictor, unfortunately, but we plan on addressing that by duplicating the stack switching stub for each C function.) Go is similar (I looked at its code when implementing some of this stuff). So it's not true that having segmented stacks undermines Go's ability to interact with C code.
Note that another cool thing we can do is to statically link clang-compiled C code with Rust code and LLVM can inline your small C functions directly into your Rust functions and save you the stack switch -- this is one of the coolest things we can do with LLVM, IMHO.
- soamv 15y agoThat's interesting. What is the cost of switching the stack when calling C code? Do you allocate a new stack every time a C function is called from Rust? If you inline a compiled C function, don't you have to do the stack switching for whatever that inlined function might call? Or do you only inline code that doesn't have any function calls?
- 0xABADC0DA 15y agoOk I understand that you can switch to a larger stack to call C code, although as I said you can't "just call normal C function directly". You can even discount the cost to acquire it by having it stick with the thread using it (no locking, alloc, etc after the first use). That might be ok for Rust where many 'tasks' might be computational only and never call a C function. But with Google Go, I think they said "we want to have a million threads each reading a socket" or some other arbitrary limit which isn't possible in 32-bit using system threads (even with segmented stack). Does Rust do it's own threading, and if so what are the advantages you perceive to doing so?