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

vm: Add basic support for VM instances to be reused (via Reset()) #59

Closed
wants to merge 1 commit into from

Conversation

deanm
Copy link
Contributor

@deanm deanm commented Aug 19, 2019

Adds a bench mark that shows this something around a 30% difference.

Benchmark_NewVMs-4 3000000 431 ns/op
Benchmark_ReuseVMs-4 5000000 302 ns/op

Adds a bench mark that shows this something around a 30% difference.

Benchmark_NewVMs-4       3000000         431 ns/op
Benchmark_ReuseVMs-4     5000000         302 ns/op
@ghost
Copy link

ghost commented Aug 19, 2019

I would suggest removing the Reset method and simply place it at the beginning of the Run call:

diff --git a/bench_test.go b/bench_test.go
index 61c6e55..82dd247 100644
--- a/bench_test.go
+++ b/bench_test.go
@@ -202,7 +202,6 @@ func Benchmark_ReuseVMs(b *testing.B) {
 	v := vm.NewVM(false)
 
 	for n := 0; n < b.N; n++ {
-		v.Reset()
 		_, err = v.RunSafe(program, &env)
 	}
 
diff --git a/vm/vm.go b/vm/vm.go
index 704c42c..90b6257 100644
--- a/vm/vm.go
+++ b/vm/vm.go
@@ -53,18 +53,16 @@ func (vm *VM) RunSafe(program *Program, env interface{}) (out interface{}, err e
 	return
 }
 
-func (vm *VM) Reset() {
+func (vm *VM) Run(program *Program, env interface{}) interface{} {
+
 	vm.stack = vm.stack[0:0] // keep the existing memory/capacity
+	vm.bytecode = program.Bytecode
+	vm.constants = program.Constants
 	vm.ip = 0
 	vm.pp = 0
 	if vm.scopes != nil {
 		vm.scopes = vm.scopes[0:0] // keep the existing memory/capacity
 	}
-}
-
-func (vm *VM) Run(program *Program, env interface{}) interface{} {
-	vm.bytecode = program.Bytecode
-	vm.constants = program.Constants
 
 	for vm.ip < len(vm.bytecode) {

@ghost ghost mentioned this pull request Aug 19, 2019
@antonmedv
Copy link
Member

Closed as of #107

@antonmedv antonmedv closed this Apr 13, 2020
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