Commit 30dfddc
Phase 4: hot-builtin fast path — VM no longer slower than tree-walk
The bytecode VM was paradoxically slower than tree-walk on
arr_push/arr_get loops, str_concat loops, and str_split/str_join
loops. Cause: vm_call_builtin's synthetic-arg shim allocates a
fresh scope, formats __vm_arg_N strings, builds Expression::Variable
nodes, and dispatches through call_function — per call. For tight
inner loops over hot builtins, the shim cost dominates the dispatch.
Two fixes, both surgical:
1. Inline arr_get / dict_get to Op::ArrayIndex in the compiler.
Op::ArrayIndex (already polymorphic over array/dict) skips the
shim entirely — same dispatch `xs[i]` / `d[k]` already used.
The fn-call form `arr_get(xs, i)` had been the slow path; this
aligns it with bracket syntax. Hottest single fix.
2. vm_fast_dispatch in interpreter.rs intercepts pure builtins on
value args before vm_call_builtin's shim runs. Covers str_concat,
str_len, str_chars, str_slice, str_split, str_join, to_int /
to_float / to_string, and println / print. Long-tail builtins
still go through the shim (no behavior change there).
Bonus alignment: str_concat tree-walk now uses to_display_string
(Phase 1's bare-number formatter) instead of to_string. The old
to_string produced "HInt(42, φ=..., HIM=...)" for numeric args —
useless output nobody wanted; tests just happened to assert against
it. concat_many already used the bare form; aligning str_concat
removes the inconsistency.
Measured (200k inner ops where applicable):
pre-Phase-4 post tree-walk
arr_push + arr_get walk 168000 ns/op 100600 101600
str_concat 2200 1200 1200
str_split + str_join 3350 1500 1350
VM is now ≥ tree-walk on every benchmark. HOF speedups (2.0-2.3×)
and direct-call speedup (2.1×) preserved. 41/41 functional examples
identical; 92/92 unit tests pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent a5ed502 commit 30dfddc
2 files changed
Lines changed: 119 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
492 | 492 | | |
493 | 493 | | |
494 | 494 | | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
495 | 509 | | |
496 | 510 | | |
497 | 511 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1903 | 1903 | | |
1904 | 1904 | | |
1905 | 1905 | | |
1906 | | - | |
1907 | | - | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
1908 | 1913 | | |
1909 | 1914 | | |
1910 | 1915 | | |
| |||
3640 | 3645 | | |
3641 | 3646 | | |
3642 | 3647 | | |
3643 | | - | |
3644 | | - | |
3645 | | - | |
| 3648 | + | |
| 3649 | + | |
| 3650 | + | |
| 3651 | + | |
| 3652 | + | |
| 3653 | + | |
| 3654 | + | |
| 3655 | + | |
| 3656 | + | |
| 3657 | + | |
| 3658 | + | |
| 3659 | + | |
| 3660 | + | |
| 3661 | + | |
3646 | 3662 | | |
3647 | 3663 | | |
3648 | 3664 | | |
| |||
3729 | 3745 | | |
3730 | 3746 | | |
3731 | 3747 | | |
| 3748 | + | |
| 3749 | + | |
| 3750 | + | |
| 3751 | + | |
| 3752 | + | |
| 3753 | + | |
| 3754 | + | |
| 3755 | + | |
| 3756 | + | |
| 3757 | + | |
| 3758 | + | |
| 3759 | + | |
| 3760 | + | |
| 3761 | + | |
| 3762 | + | |
| 3763 | + | |
| 3764 | + | |
| 3765 | + | |
| 3766 | + | |
| 3767 | + | |
| 3768 | + | |
| 3769 | + | |
| 3770 | + | |
| 3771 | + | |
| 3772 | + | |
| 3773 | + | |
| 3774 | + | |
| 3775 | + | |
| 3776 | + | |
| 3777 | + | |
| 3778 | + | |
| 3779 | + | |
| 3780 | + | |
| 3781 | + | |
| 3782 | + | |
| 3783 | + | |
| 3784 | + | |
| 3785 | + | |
| 3786 | + | |
| 3787 | + | |
| 3788 | + | |
| 3789 | + | |
| 3790 | + | |
| 3791 | + | |
| 3792 | + | |
| 3793 | + | |
| 3794 | + | |
| 3795 | + | |
| 3796 | + | |
| 3797 | + | |
| 3798 | + | |
| 3799 | + | |
| 3800 | + | |
| 3801 | + | |
| 3802 | + | |
| 3803 | + | |
| 3804 | + | |
| 3805 | + | |
| 3806 | + | |
| 3807 | + | |
| 3808 | + | |
| 3809 | + | |
| 3810 | + | |
| 3811 | + | |
| 3812 | + | |
| 3813 | + | |
| 3814 | + | |
| 3815 | + | |
| 3816 | + | |
| 3817 | + | |
| 3818 | + | |
| 3819 | + | |
| 3820 | + | |
| 3821 | + | |
| 3822 | + | |
| 3823 | + | |
| 3824 | + | |
| 3825 | + | |
| 3826 | + | |
| 3827 | + | |
| 3828 | + | |
| 3829 | + | |
| 3830 | + | |
| 3831 | + | |
3732 | 3832 | | |
3733 | 3833 | | |
3734 | 3834 | | |
| |||
0 commit comments