Skip to content

Memory increase when using shapeWithTrace #120

@MattMatic

Description

@MattMatic

The shapeWithTrace method uses a locally scoped function traceFunc that is added to the WASM interface on each call.

This is not a memory leak, but the function is registered every time the call is made, and the wasmExports.__indirect_function_table grows for each call to shapeWithTrace. It can quickly consume megabytes of RAM.

Emscripten Solution
Emscripten exposes an addFunction method in the hb.js file. Oddly, this uses a function getEmptySlot that appears to check first in freeTableIndexes - but this table is never populated.

It appears that there should be a corresponding removeFunction, something like this:

      var removeFunction = (func) => {
        var idx = getFunctionAddress(func);
        if (idx) {
          wasmTable.set(idx, null);
          freeTableIndexes.push(idx);
        }
      };

and then removeFunction(traceFunc) called after shaping, just before the traceFunc goes out of scope.

This works, but I'm unhappy having to mess with the emscripten generated hb.js file.

Alternative solution
For the time being, I've shuffled around hbjs.js to only call the addFunction once (in a similar manner to the drawFuncsPtr of hbjs.js).

The traceFunc I moved outside of the shapeWithTrace method, and used a shapeWithTraceFuncPtr variable to register only once.
However, the closure variables can no longer be accessed, and it involved creating an object wide var temp = [ ]; that is used to pass in the required parameters into the function that has lost the closure scope.

Hacky, but it's not consuming lots of array space now!

Any better solutions would be most welcome!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions