Skip to content

perf: optimise VM runtime execution loop#7

Merged
corepunch merged 1 commit into
mainfrom
copilot/optimize-vm-runtime
Apr 15, 2026
Merged

perf: optimise VM runtime execution loop#7
corepunch merged 1 commit into
mainfrom
copilot/optimize-vm-runtime

Conversation

Copilot AI commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

The VM interpreter loop contained several avoidable overheads on every instruction fetch, making runtime slower than necessary.

Changes

  • Remove dead division per instructionDWORD line = vm->location / 4 + 1 was computed every loop iteration but never used.

  • OPCOND_AL fast-path_condition() (a function call + switch dispatch) was invoked unconditionally for every instruction. Since the vast majority of ARM instructions use the "always" condition (0b1110), the condition field is now checked inline first; _condition() is only called for genuinely conditional instructions. __builtin_expect hints the branch predictor accordingly.

    // Before: always calls _condition()
    if (!_condition(vm, instr >> 28)) return;
    
    // After: skips the call entirely for OPCOND_AL (the common case)
    DWORD cond = instr >> 28;
    if (__builtin_expect(cond != OPCOND_AL, 0) && !_condition(vm, cond))
        return;
  • Mark internal helpers static_condition, _calcshift, _calcimmediate, all exec_* helpers, and exec_instruction were externally visible despite being purely internal. Without static, the compiler must preserve them as addressable symbols and is conservative about inlining. This lets -O2 inline them freely.

  • Fix UB in _calcimmediate() — when the rotation field is zero, the old code evaluated Imm << 32, which is undefined behaviour for a 32-bit type. Added an early return for the zero-rotation case.

Agent-Logs-Url: https://github.com/corepunch/armvm/sessions/b59e6d5e-873b-4db5-8bb6-00ddcd6e196f

Co-authored-by: corepunch <83646194+corepunch@users.noreply.github.com>
@corepunch corepunch marked this pull request as ready for review April 15, 2026 06:42
@corepunch corepunch merged commit adcac7d into main Apr 15, 2026
3 checks passed
@corepunch corepunch deleted the copilot/optimize-vm-runtime branch April 15, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants