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