Skip to content

Array passing back and forth between JS and AS #263

@jbousquie

Description

@jbousquie

Hello,

I'm one the core team member of the BabylonJS 3D framework (https://github.com/BabylonJS/Babylon.js) and I'm evaluating the opportunity to port some parts of BabylonJS to AS.
A 3D engine needs to compute fast many maths between two frames (so ideally in less than 16 milliseconds to get a framerate 60 fps) and to lower the GC impact for performance reasons.

In my test, I would like to :

  • allocate once some big size-fixed float32 typed array. Say, more than 1 million floats. No matter where it's allocated, JS or WASM side, as long as it's shared and re-usable along the render loop
  • populate this array with float32 values JS side once
    then each frame :
  • update the array JS side with new values (new mesh coordinates along time)
  • call an AS function that compute some trigonometry on this array values, store the results (million float32 also) in the array in a free reserved zode (note that we could also use 2 arrays, one for the input, the other for the output, as long as they remain allocated for ever, shared, accessible and uptadable/readable either from JS as from AS).
  • read the results, so the updated shared array, from JS and copy these results to the WebGL buffer.

What I found so far in the Wiki or in the github issues is how to allocate an array AS side and return the pointer to JS, then to read its values with the right typed view or the right offset.
We are really concerned by allocating once only the memory and by sharing it between JS and AS. I guess that I unfortunately copied or reallocated the memory each call either JS side, either AS side as I didn't understand how to access the same initial array both sides in my AS test so far.

Would you please mind to provide a snippet JS and AS side to achieve this ? mainly, the creation of the persistent array among JS and TS and how to access it again and again along the computation calls then.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions