text stringlengths 0 1.82k |
|---|
**Bill Kennedy:** From today's perspective, it is the array that is really the most important data structure from the hardware perspective, because it is the array that allows you to create contiguous blocks of memory. |
**Erik St. Martin:** Well, I guess structs are aligned that way too, right? |
**Bill Kennedy:** Say that again, I’m sorry. |
**Erik St. Martin:** Structs are also aligned contiguously. |
**Bill Kennedy:** They are, but if I was gonna create a user struct and I was gonna create a hundred thousand of those and I didn’t lay that out contiguously... You know, I created just a link list of these particular user values and they laid out all over in memory almost randomly, and you started walking down that li... |
**Erik St. Martin:** Right, because over the years processors, even though they haven’t got significantly faster, they've become much better at multi-tasking. So while the processor may be performing a math calculation, in the same cycle it can be making the next stride and pulling the next cache line in, so that the n... |
**Bill Kennedy:** If it can predict what that next cache line is, then it absolutely can do that. But if we’re not being sympathetic in helping it to be able to predict these things, then it can’t pull that cache line until it knows exactly, "Now this is where the data is that I need." But it's... |
**Erik St. Martin:** Yeah, and.. |
**Bill Kennedy:** I’m sorry? |
**Erik St. Martin:** I was just gonna say, so the access patterns, how you talk about it being important thinking about the data and how you’re working with it... I guess that the two main points that I can think of is basically temporal and spatial locality right? Working with things that are located next to each othe... |
**Bill Kennedy:** Yeah, and hopefully... I mean, you’re not gonna avoid cache misses altogether, but if you have a working set of data that you're gonna be doing a lot of processing on, once it gets pulled in now it’s there you can leverage it. If you're bouncing around memory all the time and it’s somewhat random, you... |
**Erik St. Martin:** So let’s give an idea of bouncing around memory. |
**Bill Kennedy:** A linked list to me could be a scenario where you have a node of data, that node of data points to another node of data, and that node of data points to another node of data. Depending on how and when that data was created, that could be almost anywhere in the heap, depending on how that's getting cre... |
**Erik St. Martin:** Right. |
**Bill Kennedy:** So you can’t guarantee in that case that every single node is on the same cache line, or even in cache lines that are next to each other. |
**Erik St. Martin:** \[15:55\] And I guess another example would be like a multidimensional array, right? Iterating over row-based versus column-based. |
**Bill Kennedy:** Yeah, we actually have some examples in the training with that over benchmarking, where you actually see a significant difference in performance. If you go row based you see it’s much faster than if you go in column based... Kind of breaking, you know, going against the grain. |
**Erik St. Martin:** Yes, so it’s interesting though, because we typically think about memory for free, right? You know, we're like, "Yeah, it’s on a RAM. RAM is fast. At least I don’t have to go to disc for it", right? But doing something like column first or row first iteration over an array like that, it really demo... |
And there’s other things, too... Even just because we use... Typical operating systems have the - I always mess up this name - the transaction lookaside buffer, I think that’s what it’s called, where it basically maps the address or memory your program has to the real memory, physical memory address. And that has pages... |
**Bill Kennedy:** You know, my interest in this actually started to develop when I came into Go. Because before that I was in C\# and we had lists, we had keys, we had stacks, we had data structures, right? And even C++ gave us all these data structures. And when I came into Go, I was like, "Where are all my data struc... |
Now when you step back and you look at it from this point of view, the underlying data structure for the slice is an array, right? The slice is the most important data structure in Go. And as I peel this onion every month, more and more about Go, all I keep seeing is how Go is pushing us towards writing sympathetic cod... |
And other areas of the language too, where you see that you're really being sympathetic with the operating system scheduler on the concurrency side, without even realizing it. Just these idioms and these things that we tell people to do all the time, they’re based not on just, “Hey, we want you to do this”, they’re bas... |
**Erik St. Martin:** Yeah, I think there’s a lot of programming idioms that can be followed to help. But I think you’re right... Like I said, I never really considered some of the language functionality, that it’s abstracting the way these things and making our programs more sympathetic by default, right? And channels ... |
**Bill Kennedy:** \[19:54\] So what about the reference type, your slice, your maps, your channel values, right? We’re always told, "Do not share this. Everybody can get a copy of these values." And what that's doing is that it's allowing us to not put pressure on the GC right? Like, we get to leverage the stacks to th... |
The thing that's being shared underneath is what let’s say necessary has to be on the heap, just that. And all these little objects that we need to pass values around across these program boundaries, we get to leverage the stack, right? |
Because there's gonna be two areas where we gonna want to focus around performance. One will be I think around data-oriented design and are we being sympathetic with the hardware and the caching systems, are we working with data the best way we can? And then the other side is gonna be, can we reduce pressure on the gar... |
These are two areas where I think we can focus just day one around performance when we’re not getting enough of it. But I tell everybody in my classes all the time, I go, "Don’t become paralyzed by all this stuff. You have to get whatever it is your working on, working first. And then you can profile and measure what’s... |
**Carlisia Thompson:** How about the data-oriented design? I understand we don’t want to maximize performance ahead of time before you know what you need to optimize and even where you need to optimize. How about the concept of data-oriented design? |
I can totally see you designing your software in a way that is not data-oriented, and it's to make it work, and you might or might not have performance issues. But let’s say you do want to change things around. It seems to me if you didn’t start out thinking about data-oriented - that way of doing it - the changes woul... |
**Bill Kennedy:** So Go is an object oriented programming language, but I don’t want people writing object oriented programs in Go. I think that’s the line, I think if your writing object-oriented software, you’re not thinking about the data first, you’re thinking about all of those relationships, and object-oriented p... |
And I’m a big fan of functions, I love functions. One of the things that was so great when I got into Go was I had my functions back. Not everything had to be on a method on a class. And I think functions can also help reduce a huge amount of your code when you’re using them in a sense where, "Here's my state and here’... |
**Erik St. Martin:** \[24:04\] And now the data-oriented design concept came from the game programming world, I believe. |
**Bill Kennedy:** Yes. |
**Erik St. Martin:** And a lot of their problems were similar, right? They needed things to happen fast because they need high frame rates, so they tried to start organizing their code in a manner so that the data that they were working with was spatially located, so they'd group the data they worked with commonly toge... |
**Bill Kennedy:** Yes, right. They have to do N number of things in X amount of time, and time is not changing, right? So they have to make that happen. So yeah, they started to learn that they had to be even more sympathetic than anyone else. |
But I think the slice, the idea of being able to leverage the slice as much as possible, when it is practical and when it is reasonable, is giving you a lot of this without you even realizing it. |
That’s one of the things I love about Go, is that Go has given us the things that we need and is pushing us towards these things by saying, "Well, I’m not gonna give you any other data structure. I’m giving you maps, I’m giving you slices." And even maps are a leveraging contiguous memory underneath. And then with all ... |
**Brian Ketelsen:** So when we were researching this show I found the term 'false sharing'. How does that fit into this whole picture? |
**Bill Kennedy:** Yeah, so now we’re getting really deep inside the hardware a little bit. But the idea is that because every core is going to be loaded with cache lines, if you have let’s say two threads, each running on a different core, working with the same data that happens to be on the same cache line, you now te... |
Now, the false sharing comes in because of that. From your perspective you're not really sharing data, but from the hardware perspective this data is being shared. The problem with false sharing doesn’t come from reading, because if you're reading data... It’s when that data gets mutated, because as soon as one thread ... |
An example Scott Meyers uses is that somebody has created a global array of counters. So all of these counters, let’s say there's sixteen counters all in the same cache line, and you launched sixteen threads, each thread getting its own index of a counter on this cache line, and all sixteen threads getting their own co... |
**Erik St. Martin:** \[28:22\] So an example of that would be if you had a single backing array holding all of your counters? |
**Bill Kennedy:** Yeah. |
**Erik St. Martin:** We see stuff like this all the time, right? So in your package, if you had a publicly exported array - or slice, for that matter - that just isn’t appended to - well, even when it is appended to... But for example you have an array of 8-byte integers that you’re using as counters, so your example w... |
**Bill Kennedy:** That's right. Because from your perspective you're not caching, but from the hardware perspective you are. Because every core has its own copy of that same exact cache line. |
**Erik St. Martin:** I guess this kind of echoes back to your data-oriented design right? Because if you’re keeping all the data locally that you’re working with, they wouldn’t be grouped together somewhere else, right? Because the counters don’t make sense together, they make sense in their individual... |
**Bill Kennedy:** So the solution to that is, since every Go routine’s stack in the stack frame - in that particular case for any Go team - is going to be on its own unique cache line, the solution to something like that would be to perform your counters on a local variable. That would be on your cache for each thread ... |
**Erik St. Martin:** So if you had to pick just a couple takeaways for everyone, things to be cognizant of when developing, to start at least a journey of being more sympathetic to the hardware, what would those be? |
**Bill Kennedy:** I tell everyone, if you’re not sure how to do something, ask the question around what is the most idiomatic way to do this in Go. Because many of those answers are already tuned to being sympathetic with the operating system of the hardware. |
The next thing I tell people is if you’re working with data, try to work with slices of values as your initial load of data. You can share different elements of those slices, but the core data you’re working with, we try to keep it as contiguous as possible. It’s not going to be perfect because you're going to have str... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.