Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: People with slots

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that switching our fields that we just pre-computed just in case over to property's gave us about a 30% increase,
0:08 both in memory performance and CPU performance, right? Time was shorter, memories was less, by that about.
0:15 Let's see what we can do around this idea of these dictionaries.
0:19 Because remember, when we say "create a crowd", we're creating 100,000 dictionaries and doing a bunch stuff in there. Can we do better?
0:25 So let's go over here and we're gonna have another type that's gonna go over here called "PersonEfficientSlotted". Why use the word "slotted"?
0:33 What is this about? Well, let's go make it, then it'll Be clear. So if we come down and we're gonna use our nice,
0:38 advanced property one. So these techniques combined, in a sense. I'm gonna go, define that, so everything up here is going to
0:47 be the same, right? This, all this code is the same. But remember, what this means is we're gonna have a dictionary that looks like first
0:58 is whatever goes here, right? Last is whatever goes here. There's actually a not super well known way in Python to say "don't do that,
1:10 just always put this value in the first container, this value the second container, that in the third, of where the data for the class
1:17 goes and don't create this dictionary at all". And the way you do that is you go up here on the type and you say
1:24 there's thing called "slots", see that "slots" right there? And then you give it an array, a list, of the names. Notice PyCharm is like "this is
1:32 all going to break". But if I type "first", now it's like "okay, first is cool". You can set a value first, but last, monthly, all this stuff,
1:41 it's, this is the dynamic behavior, it's like "you can't do that", you have to pre-declare there's gonna be all the fields.
1:47 So you just say them here, monthly income, and birthdate. Okay. We've said "these are the field we're going to have,
1:58 and then we're allowed to set them. You saw that we're getting an error. That's actually a runtime error.
2:02 Not just a warning. It will fail. if I have this, and like this, line 55 is a runtime crash. So you've got a pre-declare all these things,
2:14 but if we do, this is not going to create a dictionary and instead it's going to store the data somewhere else. So let's go over here and we'll say,
2:24 "Import this" and let's run this and see what we get. Will it be better in memory?
2:31 Probably, because it doesn't have to create all those dictionaries with the repeated keys 100,000 times, and not doing that might make it faster.
2:38 Let's give it a shot. Here's the first one, 460 milliseconds. The second one is 180. Look at that, 135, and in memory wise,
2:48 20 to 7. That's better than 2X improvement. That's a 3X improvement, almost. All we had to do are, the way we program our code is unchanged.
3:00 We still have properties, we still have the direct fields that we can access and so on. We just cannot dynamically add unexpected fields to this class.
3:10 So we have to say "these are the only things you can ever put there". You saw that PyCharm was already warning us when we tried to break from doing
3:17 that as a convention. This is just making that a runtime requirement. So, like before, in this storage here, if we went over to this class and
3:28 we said, we probably won't make it that far, we have "slots" and its thing string, thing1 thing2, with the right spelling, and we run this,
3:42 we run the right thing, notice it crashes right here, and we try to get dictionary, there is no dictionary. It's gone. That is not a problem anymore.
3:54 Although you can still see that trying to do its print out. It knows about its fields and what it can do.
4:00 So the other thing you would find is like this line right here, if we try to do that right away, let's go put that here as a copy.
4:07 You run it, notice it crashes. When we try to set thing3 that used to work, and it just added to the dictionary, but it doesn't anymore. It crashes,
4:15 okay? So that is pretty interesting. Let's take away the slots up here because this version is demo is not supposed to have
4:23 that, but this one, definitely this one we're gonna add it.
4:27 So we've combined the efficiency of properties and the crazy efficiency in terms of memory of these slots. So that's pretty interesting.
4:36 Remember, we didn't see a huge boost. We did see a boost, but not a huge boost, of performance around time to create it. So
4:45 notice This is 400 and something milliseconds. That's 170 and 150. It's faster. We're not creating those dictionaries, but it's not mega faster.
4:54 We'll see, though, that there's other aspects of performance as well that we're gaining
4:57 also. So there's all kinds of good stuff that we get here, and to me, You know, if I care about memory and I'm working with a lot of
5:04 things, I feel like almost this is the way to go. This is not common, and it takes away the dynamic nature of Python, but if you're not using it,
5:12 you just saw, your paying a 3X performance cost in memory for features you're not using, and they're incredibly easy to just say
5:20 "look, I don't need that feature on just this object, this class". Slots, double thumbs up, these are awesome.

Talk Python's Mastodon Michael Kennedy's Mastodon