Shared Memory
Explore how WebAssembly's shared memory section allows JavaScript and WebAssembly modules to access and modify the same memory via sharedArrayBuffer. Understand the linear memory model, memory allocation with WebAssembly.Memory(), and see practical examples of exchanging large data efficiently. This lesson guides you through coding a sum function in WebAssembly and integrating it with JavaScript for optimized data sharing.
We'll cover the following...
Transferring data between JavaScript and WebAssembly is an expensive operation. In order to reduce the transfer of data between JavaScript and WebAssembly modules, WebAssembly uses sharedArrayBuffer. With sharedArrayBuffer both the JavaScript and WebAssembly modules can access the same memory and use it to share data with each other.
The memory section of a WebAssembly module is a vector of linear memories. The linear memory model is a memory-addressing technique in which the memory is organized in a single contiguous address space. It’s also known as a flat memory model. The linear memory model makes it easier to understand, program, and represent the memory. But the linear memory model comes with the huge disadvantage of a high execution time for rearranging elements in the memory, and it also wastes memory space. Here, the memory represents a vector of raw bytes of uninterpreted data. They use ...