2

First of all, let me make it clear that I'm currently writing a bytecode interpreter.

I've been reading everywhere about the bytecode having to be "compact". However, I don't really understand what this is supposed to mean, or what the advantages would be.

Currently, for example, my "bytecode" is an array of tuples, the first element being a byte - the opcode itself (8 bits) and the second one a uint64 (what one would call an unsigned long long) - the optional parameter for the operation (64 bits).

Tha makes each "instruction" 72 bits. (Admittedly quite innecessary, since many of them don't take any argument, but I thought it was easier - and more performant? - this way since I don't have to check every time if there is a parameter, and just go through the list of instructions).

So, my questions:

  • what are the benefits of a more compact code? (what would I achieve if, for example, each instruction was 32 bits instead of 72?)
  • what could I do to make it better? (how do I handle "optional" arguments in an efficient way? that is: variable-sized instructions)
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 2
    Less size requirements, better caching, easier to parse (no redundancy)... to be continued. – Ctx Dec 18 '19 at 10:05
  • 1
    The size of the instructions heavily affects the performance of the first time execution as it is dominated by loading the code from the main memory while the interpreter loop will be in the instruction cache soon. This is relevant for short running programs or instruction sequences executed a single time only, a domain of interpreted languages, but also for mixed mode engines, like a JVM, which starts with interpreting the bytecode and compiles frequently executed code, so it has first time execution dominated interpretation and input format agnostic compiled code execution. – Holger Dec 19 '19 at 10:03

1 Answers1

1

Benefits I can think of:

  • Less memory usage
  • Less cache misses
  • Less actual size (in case you want to store/deploy the bytecode)
elafalw
  • 26
  • 3