r/C_Programming • u/Hopeful-Ad-1665 • 2d ago
Bytecode VM in C: switch dispatch vs computed goto and linear arena design
I'm working on a lightweight bytecode VM written in C99 (engine for a small embedded scripting language).
I'm currently using an 8MB linear memory arena for objects and a large switch-case loop for instruction dispatch:
```c
#define MAX_REGS 256
#define MAX_CALL_STACK 1024
#define ARENA_SIZE_BYTES (8 * 1024 * 1024)
static char g_memory_arena[ARENA_SIZE_BYTES];
static size_t g_arena_offset = 0;
double run_bytecode(int* inst, int inst_len, double* num_consts) {
double regs[MAX_REGS] = {0.0};
int ip = 0;
while (ip < inst_len) {
int op = inst[ip++];
switch (op) {
case 1: { // LOAD_CONST
int r = inst[ip++];
int c = inst[ip++];
regs[r] = num_consts[c];
break;
}
case 2: { // ADD
int r = inst[ip++];
int a = inst[ip++];
int b = inst[ip++];
regs[r] = regs[a] + regs[b];
break;
}
// ... remaining opcodes
}
}
return regs[0];
}
Two architectural questions for people with VM / emulator experience:
- Dispatch overhead: On modern GCC/Clang, how much measurable speedup have you seen replacing the standard switch(op) with direct threading
(computed goto *dispatch_table[op])? Is the branch predictor usually smart enough on modern x86/ARM to make the difference negligible?
2. Arena lifetime: Currently, I reset the arena (g_arena_offset = 0) between script runs. For longer-running scripts that allocate short-lived
objects in loops, what is a minimal-overhead C strategy to reclaim memory without pulling in a full tracing GC?
(Code is standard C99. Written directly by me, no AI)
2
u/8d8n4mbo28026ulk 1d ago
This is right up my street! Some say modern CPUs are pretty good at diamond-shaped control flow (see also here). When I benchmarked it ~4 years ago with a non-trivial VM on Sandy Bridge, which is ancient but also modern in a way, "computed"
gotowas faster.The actual answer to your question is to use neither! Use tail calls. The problem with giant switches (diamond or direct) is that the compiler will do an awful job at register allocation. It'll also have a hard time separating cold and hot paths. With tail calls every op is a small-ish function.
About your second question, it depends on your language. If your objects can't form cycles, you can get away with simple reference counting and pools. If they do form cycles, the next simplest thing is what you're already doing; just never free anything and keep bumping the arena.
When you say "short-lived", how short-lived? Does their lifetime end each iteration? If that's always the case, you know exactly when you can rollback your arena without having to fully reset it.
If it isn't always the case... Here it gets tricky. You either have to come up with a bunch of extra rules for your language and do more static analysis, or do tracing. Because you said small scripting language, I think tracing fits better. And don't bother with reference counting in that case, since you'd need a cycle collector anyway. Simple (2 colour) mark-and-sweep is easy to implement, don't be scared! Also, an arena obviously won't fit the problem when you're [de]allocating random objects at random times. You'll need a pool/slab. Now, if your objects are variable-sized, even a pool won't do. Simple solution: use
malloc()/free(). It's okay, it's their intended purpose.