Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve vm perf #3

Merged
merged 3 commits into from Dec 23, 2022
Merged

Improve vm perf #3

merged 3 commits into from Dec 23, 2022

Conversation

jgaskins
Copy link
Contributor

Made a couple small tweaks to the VM to improve performance. Numbers running it on an M1 MacBook Pro:

main

➜  monyet git:(main) ./build.sh && ./benchmarks.sh --vm-fast
-----8<------
"engine=vm, 9227465, duration=00:00:03.700458291"

This PR

➜  monyet git:(improve-vm-perf) ./build.sh && ./benchmarks.sh --vm-fast
-----8<------
"engine=vm, 9227465, duration=00:00:03.042388958"

… which makes it faster than the Go implementation on the same machine:

➜  monkey git:(main) ./fibonacci
engine=vm, algo=fast, result=9227465, duration=3.101420333s

How did we get here?

  • Memoized current_frame rather than computing it each time, avoiding many bounds checks on both integer addition and array indexing. This was the single largest contributor to performance improvement.
  • For other integer addition, bypass bounds checking on things like instruction pointers — the stack would have to exceed 2GB for this to be a problem and I don't think that's supported by many programming languages

One other change that didn't seem to do much for performance but that I left in anyway was to replace a few case self with plain-old type inheritance.

Storing a reference to the current frame is more efficient than
computing it each time. This could probably be made even more efficient
by actively setting it in `push_frame` and `pop_frame` instead of
forcing it to be computed for every frame, but I couldn't get that to
work. It's probably something silly I was doing wrong.
This didn't improve performance much, but it was worth a shot.
These indices should never exceed Int32::MAX
@jgaskins
Copy link
Contributor Author

Add the numbers from my Intel Mac:

main

➜  monyet git:(main) ./build.sh && ./benchmarks.sh --vm-fast

"engine=vm, 9227465, duration=00:00:04.346747996"

This PR

➜  monyet git:(improve-vm-perf) ./build.sh && ./benchmarks.sh --vm-fast

"engine=vm, 9227465, duration=00:00:03.586341877"

Go implementation

➜  monkey git:(main) ./fibonacci
engine=vm, algo=fast, result=9227465, duration=3.716671473s

@MarioAriasC
Copy link
Owner

Thanks I'll have a look

@MarioAriasC MarioAriasC self-assigned this Dec 23, 2022
@MarioAriasC MarioAriasC merged commit 09d9211 into MarioAriasC:main Dec 23, 2022
@jgaskins jgaskins deleted the improve-vm-perf branch December 23, 2022 05:53
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.

None yet

2 participants