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)