Transcript from the "Memory in Web Assembly" Lesson
>> Okay, I know I lost some of you [LAUGH] a little bit, because that was really tactical, and we're gonna get more tactical now. But [LAUGH] the good news is we're getting to the end, I specifically saved memory for last. I touched on it, we danced around it, but I stayed away from it because this is where, for me as an engineer, when I was learning these things, it got really confusing because I'm just not used to thinking about memory.
[00:00:27] I'm used to everything happening for me. But the best way to visualize memory in web assembly is it's this long, unbroken chain of 0s and 1s. That's all it is. It's 0s and 1s nonstop to, I don't know, 1,000? No, I can't even think of the cases it's way, way larger than that.
[00:00:55] But right now it's just those 1s and 0s are gibberish. The computer doesn't know and our program doesn't know where to start and stop. It doesn't know if this section of code here is a 32 bit number or this is a bunch of eight bit numbers. It doesn't know if it's signed or unsigned.
[00:01:12] It literally means nothing to the computer until we give it a view. And we give it a view using an array, or we convert from array buffer into a typed array buffer. I'll talk about array buffers in second. But dealing with memory, the good thing about web assembly is that it is linear, it doesn't jump around.
[00:01:31] That means it's very efficient. Memory that is co-located next to each other is going to be faster to access that memory that is spread all over the place. Have you ever all, this is a throwback to the Windows days, but do you remember, it was Windows 95, do you remember defragmenting your hard drive?
[00:01:50] Yeah, it's satisfying and you're watching the bits. What that is doing is it's actually taking disparate portions of memory on the hard disk and it's putting them together. So it makes it faster to read. Because technically, when you're reading from memory, you can actually read from wherever, that's why it's called RAM.
[00:02:06] It's called random access memory, memory and can be all over the place. In web assembly, the memory is specific and it's in a specific location and it's all linear, which makes it very, very fast and efficient. But that also means we have to track exactly what we need and what type of data it is, in order to extract it.
[00:02:27] In other languages, like C, for example, we think of memory in terms of pointers. And a pointer is just an address to a memory location, somewhere in memory, it actually doesn't matter. It's technically in the heap. And a heap is a dynamic nonlinear memory that any program used to save all their operations.
[00:03:06] They're not necessarily located next to each other, it's just a space allocated by the operating system to work in. This is different from linear memory, which linear memory is a fixed set of space, and if we go outside of that, we can get a out of band era, we can get a out of memory error because we start reaching out of things that are not accessible to our program.
[00:03:45] It doesn't itself mean anything. We have to give it meaning. Again, when you think of an array buffer I want you to visualize this unbroken strings of 1s and 0s. That's the easiest way. And array buffers tend to give people a little bit of hiccup, that's why I'm spending a bit of time on it.
[00:04:27] I know how to read that and now I can create indexes that point from index 0 is 16 bits, the next index 1 is another 16 bits etc.