Skip to content

Commit

Permalink
Remove unnecessary heap allocations and calls to C.
Browse files Browse the repository at this point in the history
Instead of allocating the function name and function parameter/return
value holders for each function invocation, pre-allocate them. This is
possible because invocations occur sequentially (in presence of no
threading).

This both reduces GC pressure and removes two cgo calls.
  • Loading branch information
koponen-styra committed Feb 19, 2020
1 parent 351f772 commit b297eb6
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ func getExportsFromInstance(

var numberOfExpectedArguments = int(wasmFunctionInputsArity)

var wasmInputs = make([]cWasmerValueT, wasmFunctionInputsArity)
var wasmOutputs = make([]cWasmerValueT, wasmFunctionOutputsArity)

type wasmFunctionNameHolder struct {
CPointer *cChar
}

wasmFunctionName := &wasmFunctionNameHolder{
CPointer: cCString(exportedFunctionName),
}

runtime.SetFinalizer(wasmFunctionName, func(h *wasmFunctionNameHolder) {
cFree(unsafe.Pointer(h.CPointer))
})

exports[exportedFunctionName] = func(arguments ...interface{}) (Value, error) {
var numberOfGivenArguments = len(arguments)
var diff = numberOfExpectedArguments - numberOfGivenArguments
Expand All @@ -220,8 +235,6 @@ func getExportsFromInstance(
return I32(0), NewExportedFunctionError(exportedFunctionName, fmt.Sprintf("Given %d extra argument(s) when calling the `%%s` exported function; Expect %d argument(s), given %d.", -diff, numberOfExpectedArguments, numberOfGivenArguments))
}

var wasmInputs = make([]cWasmerValueT, wasmFunctionInputsArity)

for nth, value := range arguments {
var wasmInputType = wasmFunctionInputSignatures[nth]

Expand Down Expand Up @@ -333,11 +346,6 @@ func getExportsFromInstance(
}
}

var wasmOutputs = make([]cWasmerValueT, wasmFunctionOutputsArity)

var wasmFunctionName = cCString(exportedFunctionName)
defer cFree(unsafe.Pointer(wasmFunctionName))

var wasmInputsCPointer *cWasmerValueT

if wasmFunctionInputsArity > 0 {
Expand All @@ -356,7 +364,7 @@ func getExportsFromInstance(

var callResult = cWasmerInstanceCall(
instance,
wasmFunctionName,
wasmFunctionName.CPointer,
wasmInputsCPointer,
wasmFunctionInputsArity,
wasmOutputsCPointer,
Expand Down

0 comments on commit b297eb6

Please sign in to comment.