5 ms·
C certainly has this via unions. It's more manual but provides more control, including choice of alignment. It's CPU dependent but in practice the cache benefi
by ridiculous_fish 8y ago
C certainly has this via unions. It's more manual but provides more control, including choice of alignment.
It's CPU dependent but in practice the cache benefits of packed instructions will probably dominate any misalignment penalty. It's not like x86 instructions are aligned after all.
What's the best way to implement a variable-width bytecode interpreter in Rust?
- RReverser 8y agoMore control is, as usual, less ability for automatic optimisations. For example, Rust uses knowledge of tag + data combinations for niche-filling enums where possible (and more similar optimisations to come), while LLVM can use this information to more easily deduce which branches are unreachable. As for alignment - you can control it in Rust enums too, if you wish, by putting custom aligned structures as variant payloads. In general, I'm sure it would be possible to achieve same (or, who knows, maybe even slightly better) performance than AST interpretation with manual tweaking, but the closure-based approach proved to give significant wins out of the box, while being more elegant to maintain and extend over time.
- ridiculous_fish 8y agoIn a bytecode interpreter's loop, you want your language to be little more than a fancy assembler. Exotic techniques become viable and even critical for performance: computed goto, misaligned reads, pointer arithmetic, unchecked type conversions, etc. This is one place where you want to tightly control your machine code.
- RReverser 8y agoOh, sure, if you're willing to go to unsafe techniques, you can go straight to a template JIT as well. But, for us, the balance is important, and safety is probably even more important than further wins in performance (which we can still easily get with algorithm-level optimisations before doing anything unsafe).
- deleted 8y ago[deleted]
- pcwalton 8y agoRust has C unions now, but without safety. I would probably write manual parsing code via the byteorder crate.
- cqtoq 8y agoThis is a bit off-topic, but it has been on my mind for a while. Has the Rust team considered that choosing cute names like "crate" might turn away some of the target audience? The same applies to other ecosystems like homebrew, but in the case of Rust some of the target audience certainly includes C/C++ programmers.
- steveklabnik 8y agoC++ is itself a “cute” name. It seems to be doing just fine. Beyond that, “crate” specifically has utility. The closest existing name is “compilation unit,” but with things like incremental compilation, that’s a bit of a misnomer. We need some kind of name.
- fulafel 8y agoC too (from B, which was a stripped down BCPL).