{"category":{"__v":2,"_id":"566e55c12c1e760d0030c56d","pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-17T03:34:44.886Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"project":"54408e54309354080070a896","user":"544083bee239230800071bef","version":{"__v":1,"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","categories":["566e55c12c1e760d0030c56d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.3","version":"1.2.3"},"_id":"566e55c12c1e760d0030c577","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-07-08T01:24:58.259Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":9,"body":"# Avoid memory copy motivation\nAn 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.\n\n## Background and Strategy\nBoth 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.\n\n##Availability\nArray 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.\n\n##Illustration\nTo 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.  \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia')\\nvar a = julia.eval('shared = zeros(100)');\\n\\nfor(var i = 0;i < 100;i++) a[i] = i;\\nconsole.log(julia.eval('sum(shared)'))\",\n      \"language\": \"javascript\",\n      \"name\": \"shared_array_example.js\"\n    }\n  ]\n}\n[/block]\nwhen run the following results\n    \n    node shared_array_example.js \n    4950\n    \nIf 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.","excerpt":"","slug":"array-buffer-sharing","type":"basic","title":"Array Buffer Sharing"}

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. ##Availability 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. ##Illustration 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. [block:code] { "codes": [ { "code": "var julia = require('node-julia')\nvar a = julia.eval('shared = zeros(100)');\n\nfor(var i = 0;i < 100;i++) a[i] = i;\nconsole.log(julia.eval('sum(shared)'))", "language": "javascript", "name": "shared_array_example.js" } ] } [/block] when run the following results node shared_array_example.js 4950 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.