Skip to content

VM: Speed up chaining assign. - #1163

Merged
schungx merged 1 commit into
mainfrom
vm-speed-up-chaining-assign
Sep 13, 2026
Merged

VM: Speed up chaining assign.#1163
schungx merged 1 commit into
mainfrom
vm-speed-up-chaining-assign

Conversation

@schungx

@schungx schungx commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

This PR speeds up chaining assignments such as a[x] = something.

Currently, the VM runs an assignment on a chain around 50% slower than the AST walker.

That is because it first calculates the resultant value, then pushes it into the Scope under a dummy name, then it processes the operands, and finally it reads the value back from the Scope and puts it back onto the stack.

This PR eliminates this entire process by changing the order of the stack pushes -- with the resultant value pushed first. The whole process now operates purely on the stack without touching the Scope.

Chain assignments now run at par with the AST walker, with the primes bench reaching 1.0x speed.

Another minor enhancement is the use of Scope::push_entry which takes an ImmutableString which can be cloned from the strings interner. This avoids additional allocations of the same strings over and over again.

@schungx schungx added regression vm Issues related to the Rhai Grain bytecodes compiler and VM. labels Sep 8, 2026
@schungx
schungx merged commit d8bff10 into main Sep 13, 2026
54 checks passed
@schungx
schungx deleted the vm-speed-up-chaining-assign branch September 13, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

regression vm Issues related to the Rhai Grain bytecodes compiler and VM.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants