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

fix(vm): Release VM properly #1116

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ type MachineOptions struct {
var machinePool = sync.Pool{
New: func() interface{} {
return &Machine{
Ops: make([]Op, OpSize),
Values: make([]TypedValue, ValueSize),
Ops: make([]Op, VMSliceSize),
Values: make([]TypedValue, VMSliceSize),
}
},
}
Expand Down Expand Up @@ -117,20 +117,14 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {
}
context := opts.Context
mm := machinePool.Get().(*Machine)
*mm = Machine{
Ops: mm.Ops,
NumOps: 0,
Values: mm.Values,
NumValues: 0,
Package: pv,
Alloc: alloc,
CheckTypes: checkTypes,
ReadOnly: readOnly,
MaxCycles: maxCycles,
Output: output,
Store: store,
Context: context,
}
mm.Package = pv
mm.Alloc = alloc
mm.CheckTypes = checkTypes
mm.ReadOnly = readOnly
mm.MaxCycles = maxCycles
mm.Output = output
mm.Store = store
mm.Context = context

if pv != nil {
mm.SetActivePackage(pv)
Expand All @@ -139,13 +133,12 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {
}

const (
OpSize = 1024
ValueSize = 1024
VMSliceSize = 1024
)

var (
opZeroed [OpSize]Op
valueZeroed [ValueSize]TypedValue
opZeroed [VMSliceSize]Op
valueZeroed [VMSliceSize]TypedValue
)

// m should not be used after this call
Expand All @@ -154,13 +147,14 @@ var (
// and prevent objects that were not taken from
// the pool, to call Release
func (m *Machine) Release() {
// copy()
// here we zero in the values for the next user
m.NumOps = 0
m.NumValues = 0
// this is the fastest way to zero-in a slice in Go
copy(m.Ops, opZeroed[:])
copy(m.Values, valueZeroed[:])

ops, values := m.Ops[:VMSliceSize:VMSliceSize], m.Values[:VMSliceSize:VMSliceSize]
copy(ops, opZeroed[:])
copy(values, valueZeroed[:])
*m = Machine{Ops: ops, Values: values}

machinePool.Put(m)
}
Expand Down
10 changes: 10 additions & 0 deletions gnovm/pkg/gnolang/machine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gnolang

import "testing"

func BenchmarkCreateNewMachine(b *testing.B) {
for i := 0; i < b.N; i++ {
m := NewMachineWithOptions(MachineOptions{})
m.Release()
}
}
Loading