Skip to content

mohitmishra786/low-level-dev-skills / skills/low-level-programming/interpreters

interpreters

Verified

Bytecode interpreter and JIT compiler skill for implementing language runtimes in C/C++. Use when designing bytecode dispatch loops (switch, computed goto, threaded code), implementing stack-based or register-based VMs, adding a simple JIT using mmap/mprotect, or understanding performance trade-offs in interpreter design. Activates on queries about bytecode VMs, dispatch loops, computed goto, JIT compilation basics, tracing JITs, or implementing a scripting language runtime.

FileKindSize
SKILL.mdskill md7.1 KB
references/benchmarks.mdmarkdown2.4 KB

references/benchmarks.md

View on GitHub
# Interpreter Dispatch Benchmark Notes

## Dispatch strategy relative performance

Results vary by workload and CPU, but the general ordering is consistent:

| Strategy | Relative throughput | Branch predictor behaviour |
|----------|--------------------|-----------------------------|
| `switch` dispatch | 1× (baseline) | One shared indirect branch; hard to predict for complex opcode mix |
| Computed goto | 1.5–3× | One indirect branch per opcode; per-opcode training possible |
| Subroutine threading | ~1.5× | Function call overhead; good on CPUs with BTB per call site |
| Direct threading | 2–4× | Same as computed goto but pointer-sized bytecode |
| Native JIT | 10–100× | No dispatch at all for hot traces |

Source: various academic papers and LuaJIT blog posts by Mike Pall.

## Measuring dispatch overhead

```bash
# Profile the interpreter loop
perf record -g -F 9999 ./my_interpreter benchmark.bc
perf report --no-children   # look at self time

# Expected: high self-time on the dispatch instruction itself
# means CPU is mispredicting the indirect branch
```

Symptom of dispatch overhead: `perf report` shows high time at the `jmp [r14]` (or equivalent indirect jump) instruction, not inside the opcode handlers.

Fix: switch to computed goto or add a tracing JIT.

## JIT threshold heuristics

Most production runtimes use multi-tier JIT:

| Tier | Strategy | Activation |
|------|---------|-----------|
| Interpreter | Switch or computed goto | Always |
| Baseline JIT | Simple code gen, no opt | After N=100–1000 calls |
| Optimising JIT | Full SSA, speculative, IC | After M=10000+ calls with stable types |

LuaJIT uses trace recording: compile a hot loop's trace after 56 iterations.
V8 uses function-level tiering: Ignition (bytecode) → Maglev (mid-tier) → Turbofan (optimising).

## Stack frame layout recommendations

```c
// Keep hot fields first in the VM struct (better cache line usage)
typedef struct VM {
    Value  *sp;        // stack pointer — accessed every instruction
    uint8_t *ip;       // instruction pointer — accessed every instruction
    Value  *stack;     // stack base
    Value  *locals;    // current frame locals
    // ... less-hot fields ...
    int     call_depth;
    GC     *gc;
    Table  *globals;
} VM;
```

Pack hot VM state into 1–2 cache lines (64 bytes each). Accessing `sp` and `ip` should never cause cache misses in a tight loop.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall