Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Commented Feature: C# direct access to TypedArray data [83]

0
0
I have a need to pass large amounts of data back and forth between JavaScript and C#. Ideally I would like to create a Float32Array in JavaScript and then access it as a native array or byte array in C#. The JavaScript code would calculate values while the C# code would consume them. I can currently brute force this by accessing elements one at a time, but it is way too slow for arrays of 100k or 1M elements.

V8 has a facility for this via ArrayBuffer::Externalize(). Are there any plans to support this capability in ClearScript? What would be awesome is if I could do something like the following:

V8ScriptEngine engine = new V8ScriptEngine();
engine.Execute("var a = new Float32Array(100000);");

... do something in JavaScript to set array values

// access the array and compute the sum of the values
dynamic a = engine.Script.a;

// this is how I have to get values today and it is too slow for large arrays
double total = 0;
for (int i = 0; i < 100000; i++)
{
total += a[i]; // each call dives into V8 to get an indexed value - slow
}

// this is what I want to do
float[] array = (float[]) engine.Script.a.externalize(); // one call to V8
for (int i = 0; i < 100000; i++)
{
total += array[i]; // fast!
}

This is also basically what browsers do to process data in WebGL. The JavaScript code calls bufferData() with a Typed Array. See https://msdn.microsoft.com/en-us/library/dn302373(v=vs.85).aspx

Comments: We'll take a look. Thanks!

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images