I32 enum, match via jump?


#1

Context: I am implementing VM instructions.

  1. I want to create a enum,
pub enum OpCode {
  ....

but I want the enum to be stored as a i16. I can guarantee that none of the enum choices take arguments, and that I have less than 2^15-1 upcodes.

  1. At part of the core of the interpreter, there is going to be a line of the form
match op_code {
  .... tens of options ...
```

Is there a way to tell this match to compile itself as a "jump" rather than a chain of if's ?

#2

To specify how your enum is stored (you could also use a i16, but probably an u16 is better):

#[repr(u16)]
pub enum OpCode {

LLVM could compile your match into something different from a chain of ifs. Take a look at the asm. I think there’s a crate to force the usage of jumps, but I think it’s a kind of hack, so better to use it only as last resort, IMHO.


#3

Thanks for the repr(u16) insight.

Match statement efficiency? seems to suggest it DOES by default get compiled to jump table.