Array Buffer Sharing

Avoid memory copy motivation

An unnecessary amount of work and memory usage could be avoided if analogous objects could share the same memory. Single dimension arrays are a good candidate.

Background and Strategy

Both Julia and v8 allow a framework to specify an external buffer of memory to be adopted by an array. This is done in Julia by using the function jl_ptr_to_array while v8 provides the function ArrayBuffer::New, and though not exactly the same, the parameters supplied to these two functions are similar. Also, Julia and V8 provide a mechanism to obtain the underlying memory buffer given an array. And finally both languages employ garbage collectors. So in theory both a v8 and Julia array could be created using the same buffer and so long as reference counts on both sides are maintained, then neither language will garbage collect one another's object.


Array buffer sharing is available in node-julia for single dimension arrays that correspond to JavaScript typed arrays when using node 0.12.x+ and all versions of iojs.


To help convince yourself this actually works, consider the following example. A Julia array is created and referred to in the Julia environment using the variable shared, a reference to that array is returned an as a JavaScript Float64Array typed array. In JavaScript, the array element a[i] is set to the value i for all 0 <= i < 100 and then the expression sum(shared) is evaluated in the Julia environment.

var julia = require('node-julia')
var a = julia.eval('shared = zeros(100)');

for(var i = 0;i < 100;i++) a[i] = i;

when run the following results

node shared_array_example.js 

If the array is actually shared, then the JavaScript loop will set the values of the Julia array directly and the value of the sum will be 0 + 1 + 2 + ... + 99 = 4950. If on the other hand, 2 arrays are maintained the value printed would have been 0 because setting values on a Javascript copy would not affect the Julia array values.