Latest / Talk Python To Me / #274: Profiling data science code with FIL
Transcript
- 0:00Do you write data science code? Do you struggle loading large amounts of data or wonder what parts
- 0:04of your code use the maximum amount of memory? Maybe you just want to require smaller compute
- 0:08resources, servers, RAM, and so on. If so, this episode is for you. We have Itamar Turner-Trowing,
- 0:15creator of the Python data science memory profiler Phil, here to talk about memory usage
- 0:20and data science. This is Talk Python To Me, episode 274, recorded July 8th, 2020.
- 0:26Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:44and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
- 0:49Keep up with the show and listen to past episodes at talkpython.fm, and follow the show on Twitter
- 0:55via at Talk Python. This episode is brought to you by Linode and us. Do you want to learn Python,
- 1:02but you can't bear to subscribe to yet another service? At Talk Python Training, we hate subscriptions
- 1:08too. That's why our course bundle gives you full access to the entire library of courses
- 1:12for one fair price. That's right. With the course bundle, you save 70% off the full price of our
- 1:18courses, and you own them all forever. That includes courses published at the time of the purchase,
- 1:24as well as courses released within about a year of the bundle. So stop subscribing and start learning
- 1:30at talkpython.fm/everything.
- 1:32Hey, Tamar. Welcome to Talk Python To Me.
- 1:34Hi, great to be here.
- 1:35Yeah, it's great to have you here. I'm excited to talk about Python and memory.
- 1:39Yeah, me too.
- 1:40Yeah, and I think it's something that doesn't really get as much coverage as I think it deserves in the
- 1:46Python space. You know, if you're a Java developer or a .NET developer, people go on and on and on about
- 1:53optimizing the GC and tweaking this thing or that thing or your code or algorithms for memory management.
- 2:00If you're a C developer, you're constantly in fear of memory leaks and memory management. And in Python,
- 2:07we get it just kind of coast.
- 2:08Or not. And so my motivation for getting into this was doing some scientific computing with basically a giant pile of images,
- 2:18and we'd have to extract information from them. And I initially just focused on getting it working.
- 2:24And then one day, I said, Okay, we're running this on these cloud computers. And it's taking, you know, 18 hours to process the data. Like most of the CPUs are idle, because you're using so much memory. I wonder if this is a problem.
- 2:37And so I did some math, and I talked to management about our expected revenue. And it turns out we were going to spend like 70% of our expected revenue just on cloud computing, given my current implementation, which wouldn't have let any, there wouldn't be any left over for it.
- 2:53Were they excited about that? Or were they not so excited?
- 2:55I didn't mention this. I went and optimized it. And then I just, like, then I sent emails to my manager saying, Look, look, look, the great work I did.
- 3:04Exactly.
- 3:05But I hadn't done any optimization.
- 3:07Yeah, yeah, yeah, yeah. That's very, very cool.
- 3:09And so and reducing the memory, like meant that you could use a lot more CPUs, because that was the bottleneck initially. Like we had this cloud VM that was like mostly just sitting idle, because you just need so
- 3:22much RAM for each of the threads or processes.
- 3:24Right. You can get a high memory version of a cloud computer, but it still, there is that tradeoff, right? You want to take full advantage of the CPUs there. And obviously, less memory is better. And also just it might mean fewer cloud computers to manage.
- 3:41Yeah. And if you think about your computer, if you look at like the usage of your computer, much of the time, your computer usage is going to be like you're using 1% of the CPU is just sitting there. And your RAM, if you're like, a lot of people say like a gigabytes of RAM, their computer, your RAM is going to be like three quarters percent full, 75% full.
- 3:59And basically, it's just that proportionally RAM is much more expensive than computing. And so you don't have as much of a just look at all the CPU guy, like memory tends to be resource constrained, and then the failure modes are you run out of memory and like your computer's wedged or you lost your data.
- 4:14Right, right. You run out of CPU, it goes slower.
- 4:17Yeah. So the failure modes are much worse.
- 4:20Interesting. Yeah, well, it's going to be really fun to dig into it. And I think it's an interesting angle of just the Python ecosystem that people don't spend that much time obsessing about the memory, but it's important. And it's interesting. And we're going to spend some time obsessing about it for the next hour. So for sure. Before we do, let's get into your story, though. How'd you get into programming a Python?
- 4:42I got into programming back in the mid 90s, and my parents were this business creating multimedia CD-ROMs, which was exciting new technology in the mid 1990s. And so I ended up just doing coding for them. I got into Python a few years later, when I discovered SOAP, the Z-O-P framework, which at the time was like really huge in the Python world. Like, you go to Python conferences, there'd be like a whole track on SOAP.
- 5:09And then I just stuck around and ended up using Python for lots of things like distributed computing, worked on Twisted for many years, and scientific computing, and just a variety of different things.
- 5:20Yeah, very cool. And what do you do day to day?
- 5:22I've been doing training, stuff with Docker and packaging for Python. I'm hoping to eventually teach some stuff about Python memory, and then have some products, do a little consulting on the side and start thing.
- 5:35Yeah, very cool. Is this training in person? Is it online? What is it like?
- 5:38Originally, this was in person training. I was supposed to have like, open enrollment class right after Taicon in Pittsburgh, for example. And nowadays, it's over Zoom. Because what do you do?
- 5:54Yeah, because the world is crazy. It's absolutely crazy. Yeah. Okay. Well, cool. That's a lot of fun. I did that for like 10 years and really enjoyed my time doing in-person training. Luckily, there were no pandemics.
- 6:06Zoom actually works.
- 6:08It definitely got disrupted with other things, but not too much. Yeah, we did some stuff over, I think, GoToMeeting, GoToWebinar, which there was no Zoom. So that's what we were using. It was pretty good, actually. Yeah, it's not a bad story.
- 6:18All right. So speaking of obsessing with Python memory, let's just get started off with a little bit of an overview of how Python memory works. So I feel like Python memory lives a little bit in between the C++ world, where it's very explicit, and the Java.net GC world, where it's not even deterministic. What's the story?
- 6:40As a payload, this actually depends on which Python interpreter you're using. If you're using PyPy, P-Y-P-Y, it's actually basically like Java or .NET. If you're using CPython, which most people do, it's a little bit different.
- 6:54And the basic idea is that every Python object has a reference counter. And so when you get a reference to an object, it gets incremented by one. You remove a reference, it gets decremented. So when you append your object to a list, that's an extra reference. If you destroy the list, that reference goes down to...
- 7:14If the reference goes down to zero, the object is not being used by anyone. There's no references to it. So it can immediately be freed up and deallocated.
- 7:22The problem with reference counting is that it's not... It doesn't cover all cases. If you have a circular... Set of circular references, the objects will never hit reference count to zero.
- 7:31So if you take a list and then you append it to itself, it's going... Because it has a reference to itself, its reference count is never going to hit zero, even if you don't have any other references to it.
- 7:42So in addition to the reference counting, Python also has a garbage collection system, which every... I think it's based on how many bytecodes have run.
- 7:49It will go and look for objects that are in this little loop by themselves, but not being used in any actual code and get rid of them too.
- 7:57Right. And I think the GC is also generational, like the other main ones, say Java and .NET as well.
- 8:04Yeah. And I don't quite remember how this works.
- 8:09So, you know, a totally, maybe more maniacal example might be if you're studying some kind of graph theory type object, like a network of things or a network of relationships among people or something like that, where it doesn't even have to be one thing pointing back at itself.
- 8:27It could be thing A points at B, B points at C and D, D points back at F, but F points at A.
- 8:34If you can make a circle following that chain, reference counting breaks.
- 8:38Yeah. And so then you fall back to GC, the garbage collection and...
- 8:42Right. But I would say for the most part that just knowing the GC is there to kind of catch that edge case is really all most people need to know, right?
- 8:48Because the primary story is this reference counting story. What do you think?
- 8:53Unless you're using PyPy, because then there's no reference counting. It's only garbage collection.
- 8:58Yeah. But I'm thinking most people are running CPython. Maybe they're using some data science libraries, especially in the context of using your tool that we're going to talk about.
- 9:06It feels like it's definitely in the data science side of things. In that world, in the CPython world, then it's probably reference counting that you care the most about.
- 9:16Yeah. And I mean, just a fairly high level understanding that as long as something's referring to your object, it will exist. If the references go away, it will either immediately or eventually disappear and get deallocated.
- 9:26That's pretty much all you need to know the vast majority of the time.
- 9:30Yep.
- 9:30And the vast majority of time, that's enough, but not always.
- 9:33Not always. So we're going to talk about a project that you started called PhilFIL that is about profiling memory allocations for data pipeline type of scenarios in particular is optimized for that.
- 9:48Although I suspect you could use it for a lot of different things.
- 9:51I think so, yeah.
- 9:52But let's start the story by just talking about some memory challenges, I guess we could call them.
- 9:57So you wrote a cool blog post article called Clinging to Memory, How Python Function Calls Can Increase Your Memory Usage.
- 10:06Yeah.
- 10:07That's a pretty interesting one. Tell us the general idea here.
- 10:09And so this is something I encountered in the real world, so it can impact you.
- 10:14And this is more of an issue in the kind of applications where you're processing large amounts of data.
- 10:20So like one object might be like four gigabytes of RAM.
- 10:23Like if it's like, if objects live slightly longer and they're like, you know, a dictionary of three entries and there's only one dictionary, you don't really care how long it lives because it's not using.
- 10:33Are you using 2.7 or 2.701 megabytes for this working memory?
- 10:39Nobody cares.
- 10:39Yeah.
- 10:40Yeah.
- 10:40When you have like an array that's like four gigabytes or 20 gigabytes, like this can have very significant impacts.
- 10:46If an array lives even slightly longer than it needs to.
- 10:49And so the idea is if you have a function and you create something in it and then you pass that object to another function that you're calling, you have function f and you're creating this.
- 11:00You have this large array, you pass it to g.
- 11:02If you have a local variable inside of f that the parent function still refers to that array.
- 11:08Like the parameter that accepted the data, for example.
- 11:11Yeah.
- 11:12Then that reference within that function call is a reference.
- 11:18It means reference count's not going to hit zero.
- 11:20Even if g like uses that array and then throws it away and doesn't care about it anymore.
- 11:24The parent function still has reference to that array.
- 11:28And so you can end up with these situations where if you read the code, you know that you are never going to use this data again.
- 11:34There is no way you can use it.
- 11:36But from Python's perspective, because there's a local variable, then the function frame that's referring to it, that object is going to persist until that function returns or if there was an exception that exits.
- 11:47Right.
- 11:47Because everything that was loaded up in that function got defined.
- 11:50And so here's all the variables of the function.
- 11:52And reference counting, they're still pointing at things until those variables go away, right?
- 11:58And they go away when the function returns.
- 12:00Yeah.
- 12:00And you can imagine like if you go into PDB, like you can actually travel up and down the stack and like you can go up to like the parent function and see the local variables.
- 12:09They're still there.
- 12:09Like you can still go in the debugger prompt, just go up two frames to the parent caller and you'll still see the local variable pointing to your large object.
- 12:17And so you can restructure your code in various ways to deal with this.
- 12:22And the way I ended up actually doing it was basically copying this idiom from C++ where you have this object whose only job is to own another object.
- 12:32It has that.
- 12:33You end up with only one reference to the large area that you care about, which is from inside the owner object.
- 12:38Then you pass the owner object around.
- 12:40And when you know that you don't need that data anymore, you tell the owner object, clear your contents.
- 12:46And then that one reference goes away and memory is freed.
- 12:49So you sort of just interesting situation where every once in a while you actually have to fall back to the manual memory management techniques that you have to use all the time in languages like C or C++.
- 13:00Right.
- 13:01You know what's interesting is I see examples of code like this.
- 13:05And then you'll see other people lamenting the fact that code is written this way.
- 13:10And they'll say, you should never write code this way.
- 13:12It's not necessary in Python because it has automatic memory management.
- 13:16Or you should never do this like halfway through randomly set a variable to none and then keep going.
- 13:22Why would you ever do that?
- 13:23That's like you don't need to do that.
- 13:25Right.
- 13:25If you're not going to use it again.
- 13:26Oh, except when that was costing you an extra gig of memory.
- 13:32All of a sudden, this kind of nonstandard pattern, it turns out to be really valuable.
- 13:37Right.
- 13:37It's the difference between it works or it doesn't work or it's a thousand versus two hundred dollars a cloud compute or whatever.
- 13:43Right.
- 13:45This portion of Talk Python To Me is brought to you by Linode.
- 13:48Whether you're working on a personal project or managing your enterprise's infrastructure, Linode has the pricing, support, and scale that you need to take your project to the next level.
- 13:57With 11 data centers worldwide, including their newest data center in Sydney, Australia, enterprise-grade hardware, S3-compatible storage, and the next-generation network, Linode delivers the performance that you expect at a price that you don't.
- 14:12Get started on Linode today with a $20 credit and you get access to native SSD storage, a 40 gigabit network, industry-leading processors, their revamped cloud manager at cloud.linode.com, root access to your server, along with their newest API and a Python CLI.
- 14:28Just visit Talk Python.com.com when creating a new Linode account and you'll automatically get $20 credit for your next project.
- 14:36Oh, and one last thing.
- 14:37They're hiring.
- 14:38Go to linode.com slash careers to find out more.
- 14:41Let them know that we sent you.
- 14:45Yeah.
- 14:45Having never done scientific computing before this job I was at a couple years ago, it was an interesting experience learning a different – because the domain is different, like you have different constraints and different goals, and some of the ways you write software end up being different.
- 15:01Unless you're doing large-scale data processing most of the time in Python, you just don't think of any of these things.
- 15:08Like, you might have to worry about memory leaks, but that's a different sort of – much of the time, that's a different set of problems, where, like, you don't think about the fact that an object being alive for five more milliseconds might cost you, like, another $100,000 if you're scaling up.
- 15:25Yeah, for sure.
- 15:26It's interesting.
- 15:27Another solution that you proposed – well, you proposed three solutions.
- 15:31One is this ownership story.
- 15:34One was maybe only applicable for very limited small functions, but you could just have no local variables and just basically chain one function call into another.
- 15:44Yeah.
- 15:44The intermediate one, though, seems possible, possibly reasonable as well, which is to reuse the local variable.
- 15:51So you're going to load up some data, and then you're going to maybe make some changes, which will copy the data.
- 15:55Instead of having data one, data two, data three, you just say data equals load it, data equals modify the data, data equals modify the data again.
- 16:03And that way, at least as you go through these steps, after each one, it's, you know, released the memory from the prior, potentially.
- 16:10Yeah.
- 16:10And one of the things about sort of data processing applications, they often have these sort of idioms where you're, like, doing a series of steps.
- 16:18And this is where, like, keeping old copies of the data around tends to end up cumulatively being very expensive in terms of memory because it's a series of steps.
- 16:26Once you've done step one, you don't really care about the initial input.
- 16:29Once you've done step two, you don't care about that one.
- 16:31So just explicitly overriding the previous step is another way to do this.
- 16:36I can see somebody looking at this in a code review and going, why are you doing this?
- 16:40These data mean different things.
- 16:41One should be initial data.
- 16:43The other should be, you know, grouped by state.
- 16:46And the third should be some other thing.
- 16:48Like, you're naming these wrong.
- 16:49You know what I mean?
- 16:50That's what I was kind of hinting at is, like, sometimes you need to break the rules to break through to, like, a better outcome.
- 16:57Yeah.
- 16:58And in general, pretty much every best practice is very situation-specific.
- 17:03And sometimes it's the vast majority.
- 17:05But...
- 17:06Yeah, that's a really good point.
- 17:08A lot of times when you hear advice like that, it's spoken as if it was absolute.
- 17:13But there's an implicit context, right?
- 17:15Like you said, when you don't really care about memory and that kind of stuff, you just said you just go and write the code.
- 17:20But, you know, it probably means implicitly what I care about is readability.
- 17:25And what I care about is maintainability.
- 17:27And I just want to optimize it to be as clean and pure as possible, which is fine.
- 17:32But if pure doesn't work and not clean totally works, like, forget the clean.
- 17:37We don't care anymore.
- 17:37I want it to work.
- 17:38That's more important.
- 17:39Like, functioning is primary here.
- 17:41Yeah.
- 17:41And then there's, like, places like MicroPython where you're running on little embedded devices with very little RAM.
- 17:46And then some of the problems that you have in large data processing are translated down to very small programs.
- 17:53That's an interesting example for sure because, again, if you didn't care about that extra meg of RAM, but all of a sudden you only have half a meg, now you really care about it.
- 18:01Yeah.
- 18:01I do want to throw out something from Philip Guo over at pythontutor.com.
- 18:06So if you want to understand, like, a lot of these relationships and how objects refer back to each other, he's got a really cool visualization.
- 18:12I think when you're over there, you have to check.
- 18:15There's, like, a checkbox at the bottom.
- 18:18Let me pull it up.
- 18:19Under the way it renders objects, I think you have to flip it.
- 18:23From inline primitives to, say, render all objects on the heap like Java and Python do.
- 18:28Anyway, if you want to, like, show that off or visualize that, that's a really cool quick one.
- 18:33Also, if you want to observe reference counting without changing reference counting.
- 18:39Because, like, you might want to say, how do I know if there's a reference to this?
- 18:42You can't store a variable and point at it and say, now we're going to ask because you've now changed it, right?
- 18:48Have you done anything with weak references?
- 18:51Weak ref?
- 18:52I'm not sure I've ended up using them in scientific computing.
- 18:55I've definitely done them and used them in some places with, like, asynchronous programming servers.
- 19:02Yeah.
- 19:03Yeah, you could use them for, like, caches that can kind of, like, auto expire and stuff as well.
- 19:08But they're really good for, I could create a weak reference to an object.
- 19:11Then you can ask, how many things point at this?
- 19:13And even if you know something points at it, knowing whether that's one or two might help you get a different understanding, right?
- 19:20You're like, oh, I thought there was only one pointer.
- 19:21Why are there two pointers to this thing?
- 19:23Where did that second one come from?
- 19:25And so you can ask interesting questions without changing the reference counting with weak references.
- 19:30It's really easy.
- 19:31Yeah.
- 19:31Yeah.
- 19:31And there's an API, sys.get refers.
- 19:33It gives you the objects that refers to an object.
- 19:36But then, yeah, you inevitably add the current function frame as an additional reference, and you have to discount it.
- 19:43Right, right.
- 19:43You threw also get size of in here as well.
- 19:47What's the story of the get size of?
- 19:49The function call thing is sort of just an example of places where sort of automatic memory management gets in your way.
- 19:54But there are more fundamental limits or problems you end up with when using Python in memory intensive situations, which you need to understand.
- 20:06And one of them is just that Python objects use a surprising amount of memory for what information that they store.
- 20:14So pretty much every, if you look at the implementation of the CPython interpreter, every object has an addition to whatever data you need to actually store the object itself.
- 20:22It has, on a 64-bit machine, which is most of them these days, it has a pointer to the class or the C type for the class.
- 20:30So that's another eight bytes.
- 20:31And then it has the reference count.
- 20:33So that's another eight bytes.
- 20:35And I think if you have, if it's object sports garbage collection, it's even more.
- 20:40And so if you check the system get size of as a nice utility that lets you use, tell me, tell you how many bytes object uses.
- 20:48I don't think that actually traverses the object tree, right?
- 20:53Like if this thing, if it's a list and the list points at things and those points at those.
- 20:57Yeah.
- 20:58I think it's just how much is like that, the immediate thing that that variable value points at, right?
- 21:04Yeah.
- 21:04Yeah.
- 21:05Okay.
- 21:05That's what I thought.
- 21:06Yeah.
- 21:07I can check what you're talking.
- 21:08And if you check the, how much memory, like an integer uses, like the number one, it takes 28 bytes.
- 21:16Yeah.
- 21:16So if you think about like how you'd represent numbers in memory, like unless you have really large numbers where you obviously need more, 64 bits is sort of, will get you some really big numbers.
- 21:27So if you're using a list with a million integers, that's way like I did the math and I think it was 35, a list with a million integers in it is 35 megabytes of RAM.
- 21:49If you allocated that in a CRA, it would be eight megabytes of RAM.
- 21:52So you're using four and a half times as much memory just because you're using Python objects.
- 21:57In another example, while we're talking about it is the character A. So in C, the character A would be what, four bytes or something like that?
- 22:06If you're using UTF-8, you can probably get it down to one byte.
- 22:09Yeah. Yeah. You could definitely make it smaller if you do it right.
- 22:11Yeah.
- 22:12In Python, it's 50.
- 22:12Yeah.
- 22:14So also get size of, it does some interesting stuff. So if I give it a list of like a million items, it'll say the size is 800,000. It's not quite a million. Maybe it's 100,000. I think it's 100,000.
- 22:25But if I give it a list, which has the number one and also contains within that list, that list of 100,000 items, the size is 72. So yeah, you got to be real careful. It's, it's, it doesn't tell you the whole story, but it does.
- 22:38Yeah, exactly. But it gives you a sense of, oh, like the letter A is 50 and the number one is 28. The memory that we use per representation and data in Python is fairly expensive, I think is the takeaway, right?
- 22:50Yeah. So if you have like a common, one place where people hit this is like, you're reading in some data and then like you're creating like a list per thing for like a reading in some like rows of data from the CSV or something.
- 23:05And you're turning into like, here's a list and then like there's a dictionary with like a bunch of keys for each one or an object for each entry.
- 23:12And you end up with like a massive amount of, considering the information you're storing, you end up with a huge amount of just overhead from creating all those different Python objects.
- 23:22And so like one situation you end up in Python running out of memory is if you're doing like data processing and it's just like, you just have 10 gigabytes of data you loaded.
- 23:31It's going to be a lot of memory, but sometimes it's not actually that much data if you store it on disk or if you store it in the appropriate CXC object.
- 23:40And it's just a lot of data because you create a lot of Python objects.
- 23:43And so it's using like five times as much memory as the actual information.
- 23:47Right, right. So maybe load it into NumPy or Pandas or something like that instead of into a native Python dictionary or something.
- 23:55Yeah. So if you think about Python lists, which has a bunch of Python integers in it.
- 24:00And so each of those Python integers has like 28 bytes of RAM.
- 24:04A NumPy array, it's basically has the same Python overhead, but only once at the beginning where it says, I'm an array.
- 24:12And I store 64-bit integers.
- 24:14And then the storage is just, it's not a generic pointer to generic Python.
- 24:19Right, right. It's eight bytes per entry.
- 24:21Yeah.
- 24:22Yeah. And so the information that would take 35 megabytes in a Python list will be eight megabytes in a NumPy array.
- 24:27Yeah. Another one that, I mean, moving to some of these libraries that support it more efficiently, certainly in the data science world make a lot of sense.
- 24:34But also something that makes a lot of sense that I think people may be overlooking is using different algorithms or different ways of processing.
- 24:43Like one really simple way is like I need to load, compute a bunch of stuff and give it back as a collection.
- 24:49So I'm going to create a list, fill it up and return it. Right.
- 24:52That loads, you know, maybe a million items in a memory and all the cost and overhead of that.
- 24:57And then you give it over to be processed and off it goes.
- 24:59Alternatively, add a yield instead of a list and just do a generator and you process them one at a time.
- 25:06Because probably what you're going to do when you get that list back is go through the list one at a time. Right.
- 25:10And that uses one one millionth of the memory or something to that effect. Right.
- 25:14It doesn't, it only loads one in memory at a time and not all of them.
- 25:18There's things like that as well that you can do.
- 25:20Yeah.
- 25:21If processing them one at a time in order makes sense.
- 25:23If you need to seek around and say, well, what the third one compared to the first one is, then forget it.
- 25:28Yeah.
- 25:28Like the three basic techniques usually are batching and streaming with that generator is like a sort of batches of one.
- 25:36A size of one.
- 25:37Yeah.
- 25:38Yeah.
- 25:38And then there's compression where you have the same memory and same data semantically, but with less overhead.
- 25:46So like switching from Python lists to NumPy arrays in some sense compression.
- 25:50If you know your numbers are only going to go up to 100, you can use a 8-bit NumPy array.
- 25:56And then like you've cut your memory by like 80% at no cost because you have exact same information.
- 26:02And then the final technique is indexing where you need to load only some of the data.
- 26:08Then you can sort of arrange your data.
- 26:10So you can only, you only need to load that part.
- 26:12So like if you're doing accounting, like if you have one file for every month of the year, like you can just load July's file and then you don't have to worry about the data in the other months.
- 26:20Yeah.
- 26:21Yeah.
- 26:21Very cool summary.
- 26:22So that's the picture.
- 26:24That's the memory story.
- 26:25That's some of the challenges you might hit and some of the potential solutions that you might come up against.
- 26:31But at some point you might just need to know like, okay, this is about as good as it's going to get, but I still need to understand the memory better.
- 26:38Or I'm running out of memory.
- 26:39Why?
- 26:40Where?
- 26:41Or maybe you want to take the lazy approach.
- 26:44Maybe you want to start from, well, I know I have this problem of using too much memory.
- 26:48I know one of these things that these guys talked about will possibly solve it, but where should I focus my attention?
- 26:55Right.
- 26:55I've got a thousand lines of Python.
- 26:57Maybe only three need to be changed.
- 26:59Which are the three?
- 27:00Right.
- 27:01So you probably want to profile it somehow.
- 27:03Answer the question, like where is the memory coming from?
- 27:06What's the problem?
- 27:07It's very difficult to optimize something if you can't measure it.
- 27:10Like the example we gave with functions keeping local variables, keeping things alive.
- 27:15Like I would never, now that I know it's a problem I've encountered, I might be able to look for it.
- 27:21But at time, like it was, I believe it was something like extra 10 gigabytes of RAM or something.
- 27:27And I don't think I ever would have spotted it just by reading the code.
- 27:31Yeah.
- 27:31Because it looks perfect.
- 27:32It's clean.
- 27:33It's readable.
- 27:34It's optimized exactly for the scenario that you most of the time optimize it for.
- 27:38So it doesn't look broken.
- 27:39Yeah.
- 27:40If you want to understand why something is using too much resources, like you need to measure it.
- 27:44I built a profiler for a memory profiler called Phil, Phil, F-I-L, which is designed to solve this problem because I hadn't tried other tools available.
- 27:55I decided they weren't sufficient.
- 27:56Yeah.
- 27:56So Phil, I think is really interesting.
- 27:59And the thing that made it connect for me at first, I was like, well, we already have some pretty interesting ones.
- 28:06I mean, you've got built-in C profile.
- 28:07I think that only does CPU profiling, not memory.
- 28:10We have memory underscore profiler, which will do memory profiling.
- 28:14Yeah.
- 28:15We have Austin.
- 28:16Yeah.
- 28:17Yeah.
- 28:17Yeah.
- 28:17We have Austin.
- 28:18Are you familiar with Austin?
- 28:19I've not used it.
- 28:20I've used a Pi instrument and I know about Pi top, Pi spy, and they're all sampling profilers.
- 28:27Right.
- 28:27Right.
- 28:28And Austin's pretty interesting as well.
- 28:30But Phil, the way that you laid it out is really a lot of these profilers are either general purpose or they're built around the idea of working on servers.
- 28:40And long living processes that do short amounts of work many, many times.
- 28:45Right.
- 28:45Like a web server or something like that.
- 28:47And that's a pretty different scenario of I have a script.
- 28:50I need to run it once in order and then look at where the memory comes from.
- 28:56Right.
- 28:57Yeah.
- 28:57So memory profiler is the tool I ended up using when I was trying to use memory usage.
- 29:02And memory profiler will do this thing where it gives you, like you write on a function that says this function added 10K in memory or 100 megabytes in memory or whatever.
- 29:11And if you're trying to find a memory leak, this is actually pretty useful.
- 29:14Like you can say, like I call this function, now my memory usage higher.
- 29:18And so what happened?
- 29:19So you can just figure out this function is where your memory is leaking.
- 29:24But the thing that I was trying to do and what data processing applications, as you mentioned, are trying to do is reduce your peak memory.
- 29:31The idea is that you're running this process.
- 29:35It's going to load in some data.
- 29:36It's going to process it.
- 29:37And then it's going to write it out.
- 29:38And it's going to exit.
- 29:39And the peak memory usage is what determines how much hardware you need or virtual hardware.
- 29:45Like it doesn't matter if like 99% of the time it's only using 100 megabytes, if 1% of the time it needs 60 gigabytes of RAM.
- 29:53Like it's that peak moment in time that you need to.
- 29:56That's what you have to put it in for.
- 29:58Yeah.
- 29:58Yeah.
- 29:58The high watermark.
- 29:59It's like you're building a dam.
- 30:00Like you figure out what the highest flood you get is.
- 30:04And the thing about memory profiler, like you can run it on your function and it'll say this line of code added zero megabytes of RAM.
- 30:12Like it measured before, measured after.
- 30:13They're the same.
- 30:14So no memory was added.
- 30:16But that doesn't tell you.
- 30:18It's fine, right?
- 30:19Right.
- 30:20But it may be that it allocated 20 gigabytes of RAM, did something, and then deallocated.
- 30:25And so you have to, the memory profiler, like recursively go through your whole code base function by function until you find that one line of code that's spiking things.
- 30:38And so you can use it to figure out the peak memory, but it is a very manual, tedious process.
- 30:45Yeah.
- 30:46And once your code base is hard enough, it can become quite difficult.
- 30:51And another big distinction between servers and data pipelines is how much you care about memory leaks.
- 30:56As long as it's a small memory leak, like if you're doing like a process that runs for an hour and it leaked 100k, like after an hour, it'll just exit.
- 31:05If you have, if you're leaking 100k an hour, but your process, you have like 10 processes and they're running for a year, 100k may still not be a problem.
- 31:14But like there are some thresholds where for a server, like it accumulates and your server crashes.
- 31:18And for a batch process, so long it's not impacting the peak, you don't care.
- 31:22Right.
- 31:22Well, imagine you leak only one kilobyte of memory, but it's in the context of a web request and you're getting 100,000 web requests an hour.
- 31:31All of a sudden, your server is toast, right?
- 31:34Whereas if you call the function once and you leak a kilobyte and you're doing like a top to bottom run at once data pipeline, who cares?
- 31:41Doesn't matter, right?
- 31:43It's lost in the void there.
- 31:45So I think also just the focus of what you care about is really different.
- 31:50You don't generally have these huge spikes in server type applications.
- 31:54You can if you're doing like reporting or other weird stuff, but like standard data driven stuff, it's pretty flat line.
- 32:00Yeah.
- 32:01And it turns out that if you think about it, a memory leak, a tool that can find peak memory can also find memory leaks.
- 32:08Because if you have a memory leak, peak memory is always like right now.
- 32:13Yeah, exactly.
- 32:14If you just run for a while and peak memory, eventually like your memory is overwhelmed by the leak.
- 32:18And then you dump the memory then.
- 32:20And so that moment is peak memory.
- 32:22So a tool that can find peak memory can deal with leaks, but it deals with leaks can't necessarily help you with peak memory.
- 32:29So it's actually a more general concept.
- 32:31Talk Python To Me is partially supported by our training courses.
- 32:36How does your team keep their Python skills sharp?
- 32:39How do you make sure new hires get started fast and learn the Pythonic way?
- 32:43If the answer is a series of boring videos that don't inspire or a subscription service you pay way too much for and use way too little, listen up.
- 32:52At Talk Python Training, we have enterprise tiers for all of our courses.
- 32:56Get just the one course you need for your team with full reporting and monitoring.
- 33:00Or ditch that unused subscription for our course bundles, which include all the courses and you pay about the same price as a subscription once.
- 33:08For details, visit training. talkpython.fm/business or just email sales at talkpython.fm.
- 33:15Another thing I like to do is relate quantum mechanics back to programming ideas.
- 33:22And I think they're really relevant in both profiling and debugging.
- 33:27And that is the idea I'm thinking of is the observer effect, that by observing some phenomenon, you might actually change it, right?
- 33:35Maybe the tool you're using to measure it actually makes some difference.
- 33:39Or in quantum mechanics, like just insane, bizarre observer effect.
- 33:43Things happen that, again, shouldn't, but it does.
- 33:47One of the challenges I see around profiling is, especially instrumenting style profilers,
- 33:55is you run it because it's too slow, you won't understand the performance.
- 33:59So you apply the profiler to it.
- 34:02Now it's 10 times slower or 20 times slower, but not evenly, right?
- 34:06Like if it's in a really tight loop, that part slows down more than if you're calling like a C function that you're not technically profiling that part,
- 34:13but it's still slow.
- 34:14That might not really slow down at all.
- 34:15So you might exaggerate different parts of it as well.
- 34:19And it sounds to me like Phil doesn't have much of this observer problem.
- 34:23Yeah.
- 34:23So the observer problems tend to be worse in CPU profiling, because as you said, like the act of profiling can change how fast the process runs or which parts of the code run faster.
- 34:34So C profile suffers from this, because it's adding overhead per Python function.
- 34:39And so code that has a lot of Python functions will be slower than code that has less Python functions, even if the actual runtime is the same.
- 34:46So the ads are already unevenly.
- 34:48And the solution on the hand CPU profile is actually sampling.
- 34:51We only like every thousand times a second, you see what's running right now.
- 34:55And tools, I believe Austin works that way and PySpy and PyInstrument.
- 34:59All right.
- 35:00It's more like a helicopter parent.
- 35:02Like, what are you doing?
- 35:03What are you doing?
- 35:03What are you doing?
- 35:04Instead of actually walking along every step, just constantly asking.
- 35:08Yeah.
- 35:09And so then it gets a chance to run faster or whatever when it's not asking.
- 35:12Yeah.
- 35:12The impact is quite minimal.
- 35:14And because slower CPU functions will show up more when you're just peaking every once in a while, like statistically it'll converge.
- 35:22You'll get an overview of where performance is being spent that isn't exactly right, but it's close enough that it doesn't matter that it's not exact.
- 35:31So that's CPU.
- 35:32In memory, sampling might work well for something like a memory leak.
- 35:38Because with a memory leak, like eventually all your memory usage is this one function being called over and over.
- 35:43So if you only check some of the time, it's like eventually you'll catch it.
- 35:48But if you care about the peak, you have to maybe not have to capture all the allocations, but like you may have like one specific allocation that's like 20 gigabytes.
- 35:59That's what causes your peak.
- 36:00And if your sampling doesn't catch it, then the sampling, the profiling is useless.
- 36:06And so effectively, one way or another, you have to track every memory allocation if you actually want to find peak memory.
- 36:13So the implementation approach, whereas sampling is like the superior approach for CPU, if you will care about a high watermark or peak memory, instrumentation is often the only way to go.
- 36:26If you have uneven allocation patterns, which is the case in data processing applications.
- 36:31Right.
- 36:31Yeah.
- 36:32And it sounds like maybe a 50% speed hit is what the docs say.
- 36:36That doesn't sound too bad.
- 36:38Yeah.
- 36:38I mean, it's probably slower in some cases and faster than others.
- 36:43This is what you like if you run PyStone.
- 36:44Yeah.
- 36:45It's not like a thousand percent or something like that.
- 36:47Right.
- 36:47Yeah.
- 36:47And I spent basically, once your profile is slow enough, people just don't use it because they don't have the patience.
- 36:54Yeah.
- 36:54So a lot of the effort I put, like, the basic idea of what it does is not that sophisticated.
- 37:02It's basically, like, you intercept all memory allocations and keep track.
- 37:06And then whenever you hit a new peak, you store a copy of that so that you know that's the peak.
- 37:12It's just if you want to do that with low overhead, that takes work.
- 37:16Right.
- 37:16Absolutely.
- 37:18So one of the challenges is the reason you're using the profiler probably is because you have a lot of data and you built it in some small scenario and then you run it in the real scenario, then it actually is not doing as well as you'd hoped.
- 37:28That's exactly when you need to be able to run it with the profiler.
- 37:32And you need it to work fast, I guess is what I'm saying, to really use it in real scenarios.
- 37:36Yeah.
- 37:36And another thing I've done to handle that, which, and this is a new project, so this is all, like, work in progress.
- 37:43But I know at least, like, I've gotten at least one success story of someone saying they found a, like, in within minutes, they found a memory issue they wouldn't have found otherwise.
- 37:52So I know it's useful for some people and other people have bugs.
- 37:56But another feature that I've added is when the worst case scenario for running out of memory is your program just crashes.
- 38:04And this can be as bad as, like, your computer just wedges altogether, which is not uncommon.
- 38:09Like, just everything's become so utterly slow that, like, yes, if you left it alone for a day, it'd come back, but you put files to restart it.
- 38:19Or you get, like, or it just crashes.
- 38:20And you can do a core dump, but, like, the core dump doesn't tell you.
- 38:23In theory, it has information you want.
- 38:25Yeah, in practice, that's a whole other level right there.
- 38:28Yeah.
- 38:28Or it actually does not have the information you want, to come to think of it.
- 38:32Another thing, a feature I've added is Phil makes some attempts to handle out-of-memory crashes.
- 38:39Like, if you run out of memory, it'll say, like, okay, you just got a failed allocation, so I'm going to try to deallocate all the large allocations that I know about just to free up some memory.
- 38:49And it has, like, this emergency stash of, like, 16 megabytes that, like, just allocates up front and select it.
- 38:55It breaks the glass.
- 38:56It allocates that memory, so it's a bit more.
- 38:58It lets it go and then starts tearing stuff down as hard as it can.
- 39:00Yeah.
- 39:01And then it tries to dump a report of, like, this is what your memory usage was.
- 39:04And it won't always work, and I suspect it needs a bunch more work.
- 39:08Like, it needs a bunch of optimization, because I feel it dumping the report from field text memory.
- 39:13But the idea, like, my goal, at least, is that when you run out of memory, instead of just a crash, you'll actually get some feedback that will help you diagnose the problem.
- 39:24Yeah, that's really, really cool.
- 39:25I don't know how CPython, a Cprofile, excuse me, I don't know exactly how deep its reach is.
- 39:32But in Cprofile, if I'm trying to look at, say, data science stuff, and I'm calling a library, and it's using its internal malloc and its internal C stuff to manage the memory down in the C layer, I don't know if Cprofile will check that.
- 39:48You know, if it's doing, like, crazy Fortran stuff or other allocations, who knows?
- 39:53So CPprofile, I mean, it's giving you CPU, but it's...
- 39:57Yeah, I'm sorry, Memory Profiler, the one that does memory one.
- 40:00Yeah, yeah.
- 40:00So Python actually has a memory profiler thing, TraceMalloc, but it only knows about Python memory APIs.
- 40:09Like, if you're using an arbitrary C++ library, you won't know about it.
- 40:12Which is common in the data science world, right?
- 40:14I mean, that's exactly where a lot of the action is.
- 40:16Yeah.
- 40:17Yeah, Memory Profiler has a bunch of different ways it can work, but it can actually...
- 40:23The most general way it works is, like, at the beginning of the line of code, the end of the line of code, it checks just how much memory that process is using.
- 40:29And so it'll work with any allocation, but it has the other downsides that we talked about earlier.
- 40:35So Memory Profiler can actually...
- 40:38The reason I was using it was because it can actually catch any allocation from any C library.
- 40:43I see.
- 40:44Painfully, for purposes of reducing memory usage.
- 40:47Yeah, for sure.
- 40:48And so my goal with Phil was to not just be tied to Python code allocations and be able to just generically support anything that any third-party library is using.
- 41:00Yeah.
- 41:03Yeah.
- 41:04I think that's a lot of things that I've done.
- 41:05I think that's a lot of things that I've done.
- 41:05I think that's a lot of things that I've done.
- 41:06I think that's a lot of things that I've done.
- 41:07I think that's a lot of things that I've done.
- 41:08I think that's a lot of things that I've done.
- 41:10I think that's a lot of things that I've done.
- 41:11I think that's a lot of things that I've done.
- 41:12I think that's a lot of things that I've done.
- 41:13I think that's a lot of things that I've done.
- 41:14I think that's a lot of things that I've done.
- 41:15I think that's a lot of things that I've done.
- 41:16I think that's a lot of things that I've done.
- 41:17I think that's a lot of things that I've done.
- 41:18I think that's a lot of things that I've done.
- 41:19I think that's a lot of things that I've done.
- 41:20I think that's a lot of things that I've done.
- 41:21I think that's a lot of things that I've done.
- 41:22I think that's a lot of things that I've done.
- 41:23I think that's a lot of things that I've done.
- 41:24I think that's a lot of things that I've done.
- 41:25I think that's a lot of things that I've done.
- 41:26I think that's a lot of things that I've done.
- 41:27I think that's a lot of things that I've done.
- 41:28I think that's a lot of things that I've done.
- 41:29I think that's a lot of things that I've done.
- 41:30I think that I've done.
- 41:31because the operating system will cleverly load and unload stuff from disk on demand.
- 41:36And so it is affecting how much memory you use, but the OS will sort of optimize it for you.
- 41:41So it's not clear how to measure it.
- 41:43So there's a lot of ways that if you want to track everything, like there's a lot of them,
- 41:48and I don't do all of them quite yet.
- 41:50But I've been sort of adding them one by one and hope to cover the vast majority of cases pretty soon.
- 41:57Yeah, but you covered some of these at least already, huh?
- 42:00Yeah.
- 42:01I cover basic MF usage, malloc, calloc, realloc, which I said, the standard APIs, added aligned alloc, which is C++.
- 42:10Apparently, at least in some cases, Fortran, I've never done anything with Fortran.
- 42:16I just know that it's a thing that scientific computing uses.
- 42:19And so like I said, okay, I'm going to figure out if Fortran is covered by this.
- 42:23And it turns out that traditionally Fortran never actually had memory allocation.
- 42:28You would just like write some code and you would say, I'm going to have this array and that's all you ever got.
- 42:34But modern Fortran from 1990 onwards has explicit allocation.
- 42:38And Phil can at least capture that if you use at least GCC's Fortran compiler.
- 42:43And so the idea is you should be able to just take arbitrary data processing or scientific computing code and it will figure out those allocations.
- 42:51It won't tell you like which line of Fortran and which line of C was responsible because that's like there are tools that do that.
- 42:59But the performance overhead is immense.
- 43:02But it will tell you at least which line of Python was responsible and much of the time that's sufficient.
- 43:07Right. And as a Python developer, really, that's kind of the answer you want.
- 43:10You don't want to know that like this internal part of NumPy did it.
- 43:13You just want to know I called, you know, load CSV on Pandas or something.
- 43:18And that's where the memory is or something.
- 43:20Yeah, exactly.
- 43:20You want to see the kind of boundary into that library because that's where you control.
- 43:25You're not going to go rewrite Pandas or NumPy.
- 43:26Yeah.
- 43:27And yeah, much of it.
- 43:28So yeah, you will like the goal field is to tell you where in your Python code the memory usage was and not only tell you that in a very easy to understand way, which was another one of my goals.
- 43:41Yeah. So you want to tell people maybe describe the flame graphs that they can see and explore.
- 43:45Yeah.
- 43:46And maybe we can link to one of the show notes.
- 43:48So flame graph, I think Brendan Gregg came up with the idea.
- 43:52And the idea is it's sort of showing you, you know, your programs that you can think of as a, any point you have like a call stack, like you have function F calls function Z calls function H.
- 44:02That's sort of a stack.
- 44:04And so you can put these bars that where the wider they are, the more resource they're using.
- 44:10Brendan Gregg originally did this for CPU.
- 44:12I'm using it for memory.
- 44:13And the idea is so if you have a really wide, like if you have a bar that's like 100% of the screen, that's like it's things using all this or the functions of call.
- 44:21They're using all your memory.
- 44:22If it's like narrower, it's using less memory.
- 44:25And then I've arranged it in a way that it actually includes the source code.
- 44:28So what you're reading looks like a stack trace.
- 44:30It looks like something through an exception and you're just reading it.
- 44:34But the width of the bar shows you which lines of code were responsible for how much memory cumulatively.
- 44:40I also added some stuff where there's a building on a Rust library called Inferno, which is great, which didn't much of the heavy lifting.
- 44:47But I added a feature to Inferno where the wider the bar, the more memory it's using, the redder it is.
- 44:53And so the idea is you just look at the graph and you can just see like where it's red is where.
- 44:58Where is it red?
- 44:59That's the problem.
- 45:00That's the thing you got to focus on, right?
- 45:01Yeah.
- 45:02It really focuses on the expensive parts of the code.
- 45:05And then what you're reading is a stetress.
- 45:07Yeah.
- 45:08And these are cool.
- 45:09You can embed these into the web pages and then you can hover over them and click and like zoom into the functions and really explore it quick and easy, right?
- 45:17Yeah.
- 45:18I originally rolled this sort of Perl script that converted data into these SVGs and then Inferno library ported that to Rust and so I'm using it.
- 45:26So they didn't much of the work and I'm just building on top of it mostly.
- 45:30So they did a few small features.
- 45:32Yeah.
- 45:33It's like this whole UI for exploring there.
- 45:35To use it is super simple.
- 45:36Like if you were going to run Python space your app.py with its arguments, you just would replace Python with fill dash profile space run and that's it, right?
- 45:47And you get this output.
- 45:48Yeah.
- 45:48My goal was also no options.
- 45:51Like this isn't a people don't run memory profiling like every day.
- 45:56Like it's not like a tool you want to tweak and customize your own personal needs or that you want to spend a lot of time learning.
- 46:02So another of my goals is just it should just work.
- 46:05So I've at the moment it has one command line option, like where it dumped the data, you know, you need to set that or think about it.
- 46:11And then the output is like a HTML page that has the graphs embedded and has some explanations.
- 46:17And so the goal is as much as possible to make it as sort of transparent and easy to use.
- 46:23And I have some further ideas of how to improve the UX, which I haven't gotten to yet.
- 46:28Nice.
- 46:29So if I'm like a data scientist or a computing person who is not necessarily a programmer, I could just drop in here, pip install, fill, fill dash profile, run my thing that normally I would just say Python run.
- 46:39And that's, that's all I really got to know.
- 46:41And then I just look at a web page.
- 46:43Yeah.
- 46:44It'll open the web page automatically if you're, it can.
- 46:46So you don't even have to.
- 46:47Yeah.
- 46:48If you're, the goal is you run it and it, yeah.
- 46:50Yeah.
- 46:51If you're, the goal is you run it, it pops up a web page, read the web page and you have the answer.
- 46:55Yeah.
- 46:56What's using where memory is going.
- 46:57You spoke about one of the cool features being the out of memory catch and analysis, and you've got to do a slightly different thing on the command line to make that work.
- 47:06Right?
- 47:06Yeah.
- 47:07The issue is, and this is a thing I can probably fix eventually.
- 47:11It's just, this is sort of a limit in my implementation.
- 47:13The code that generates the report right now is in Python.
- 47:16And if you just run out of memory, you can't go back into Python at that point.
- 47:20Yeah.
- 47:21So if you run out of memory, like it's not, the experience isn't quite as nice.
- 47:25Eventually I might end up like, if it reaches the point where I'm not like iterating it as quickly, I might rewrite that in Rust.
- 47:33And then at that point, it might be feasible to actually like have the fully nice UI and the crash.
- 47:39Yeah.
- 47:39Right.
- 47:40Okay, cool.
- 47:41Now also currently it runs on POSIX, Linux and macOS only, right?
- 47:46Yeah.
- 47:47I would expect that.
- 47:48I'm not sure it would run on anything other than like, if you run this in FreeBSD, my guess is it will work.
- 47:52Yeah.
- 47:53But I don't think-
- 47:54Linux and macOS, yeah.
- 47:55Yeah.
- 47:56Yeah.
- 47:56I don't think data scientists or scientists are using much FreeBSD.
- 47:58Right.
- 47:59Yeah.
- 48:00And macOS was added fairly recently.
- 48:04And someday I would like to add Windows, but it's, there's a lot of like dealing with like linkers and like fairly low level details that I don't know as much about on Windows.
- 48:18So it should be possible.
- 48:19I've seen things that make that make me think that it is possible.
- 48:23I just, it's a chunk of work I haven't done too, because they're hard.
- 48:27Sure.
- 48:28Yeah.
- 48:29You've either got to get it working or-
- 48:31Yeah.
- 48:32You're just supporting macOS, because that's a lot of work, so.
- 48:34Yeah.
- 48:35Yeah.
- 48:36I'm sure it was.
- 48:37So I actually think that maybe you don't have to worry too much about Windows.
- 48:41And that's not to say that people don't use Windows.
- 48:43Windows is used by like half the Python developers, and it's probably pretty heavy in the data science world as well.
- 48:49But, you know, Windows 10 now has Windows subsystem for Linux, and V2 is quite nice.
- 48:55So it's very possible you can just point people at, you know, you have to use Windows subsystem for Linux.
- 49:00It would probably work, because it's all, it's all APIs that I would expect are emulated fairly faithfully.
- 49:07Yeah.
- 49:07I think it's just a BlinkView virtual machine, so I don't think you have to do anything.
- 49:12My impression is that it, well, at least the original one was rather more sophisticated.
- 49:15Like, there was something about like, translating syscalls.
- 49:18I don't know about version two.
- 49:19But yeah, there's a decent chance it'll work just fine on WSL.
- 49:23Yeah, I'll put a link to Chris Moffitt's article on creating a, using Windows SL to build a Python development environment on Windows.
- 49:32And maybe that'll help people in general.
- 49:34Maybe this will work.
- 49:35I don't know.
- 49:35We can give it a try.
- 49:36Cool.
- 49:37And then you also, you know, it's one thing to just say, well, too bad that didn't work.
- 49:43It's a lot better to say.
- 49:44And here are some ideas for making it better.
- 49:46So you have a couple of recommendations for data scientists on how to be more efficient with their code and their memory.
- 49:53So I talked earlier about batching, indexing, and compression.
- 49:57And I actually gave a, supposed to give a talk at PyCon about that this year.
- 50:02It was, I mean, there's a recorded recording of it, but I never gave it live.
- 50:05And there's a series of articles here that sort of talk about those ideas and then show how to apply them in NumPy, show how to apply them in Pandas.
- 50:12And I started writing some articles about like how to just Python level issues, like how do you, like we talked about with like function calls and just ways to structure a code to reduce memory usage.
- 50:23So there's a bunch of articles that are already adding more over time, just with sort of the techniques you need to, once you figure out where the problem is to reduce the memory usage.
- 50:34Right, right.
- 50:35Yeah.
- 50:35I just saw your video.
- 50:37I didn't realize, I didn't watch it yet.
- 50:39So I'll put a link to it in the show notes so people can watch your virtual PyCon talk.
- 50:43Yeah.
- 50:44I've been going to PyCon for a very long time.
- 50:47And so it's just really sad not being able to see like friends that leave me once a year.
- 50:51And I know, PyCon is like my geek holiday, you know, just get out of there and hang out with a lot of my friends that I only see otherwise interact with online.
- 51:01And it's really special.
- 51:02It's too bad it didn't happen this year.
- 51:03Yeah.
- 51:04Someday.
- 51:05Yeah.
- 51:05Someday it'll be back someday, like everything.
- 51:08All right.
- 51:09Well, these are really interesting ideas.
- 51:12I think covering them in general was good.
- 51:14And Phil is a cool project.
- 51:15So I think it'll help some people out there who are having challenges.
- 51:19Maybe their code is using too much memory and swaps out and becomes insanely slow, or they just couldn't process the data they wanted because it didn't work.
- 51:27So they can hit it with this, use some of your recommendations and maybe unlock some answers.
- 51:32Yeah.
- 51:32I should add, this is a very new project.
- 51:34And so like, I know one person for whom it worked right, but I also know one person for whom it just wildly misreported the memory usage.
- 51:43Okay.
- 51:44He's hoping to send me a reproducer later this week.
- 51:46We can fix it.
- 51:47So if it doesn't work, I very much encourage you to file a bug report.
- 51:52Let me know.
- 51:53I'm happy to do a screen sharing session.
- 51:55So some people will bug it just because I want this to be a tool that works for people.
- 52:01And so if it's not working, I want to help.
- 52:03And it's an early enough stage that I expect that there are still a bunch of major issues, even if it does actually work in some cases.
- 52:11So please try it.
- 52:12It might just work.
- 52:13And if it doesn't, please let me know.
- 52:15I'll do my best to help.
- 52:16Yeah.
- 52:16Very cool.
- 52:17And speaking of which, you know, people are asking me recently, hey, I'm looking for an open source project to contribute to.
- 52:22Do you have any recommendations on ones I might look at or consider contributing to?
- 52:27What's the story there?
- 52:28Are you looking for people who might participate?
- 52:30I would be happy to accept contributions.
- 52:33It's some parts of it are, there's a lot of fun stuff in there.
- 52:37Like in terms of low level systems programming, there's like, there's a bunch of rust and like a bunch of C code and like poking into the internals of CPython.
- 52:47If that is a thing that interests you, there's a bunch of work there.
- 52:50There's also a bunch of UI things that could be done.
- 52:53Like, if you think about profiling, the real usage pattern should really be profile this program, try to fix it, and then say, profile this again and show me the difference.
- 53:04Like, and then you can have a visualization of the differences.
- 53:07That is my eventual goal is like, to have a user experience that's not just what you use now, but actually shows you if things are better or worse and where.
- 53:15So if people are interested in sort of that sort of UX kind of work, there's a room there.
- 53:21What about building like tutorials and stuff like that?
- 53:25Yeah.
- 53:26I mean, like in general, it'd be exciting to see people pick it on, but it's also the same time.
- 53:33Yeah.
- 53:34Some low level stuff, right?
- 53:35You will hit these places where it's like, I'm poking into the, like I'm causing slight memory leaks internally in CPython for optimization purposes.
- 53:45Yeah.
- 53:46Things like that.
- 53:47Sure.
- 53:48Because you want to be able to refer to pointers being like, there's a bunch of work in order to not have a lot of overhead when you report a new allocation.
- 53:56And so you want to be able to like keep like a pointer address in the Python interpreter as a persistent key, which means you have to make sure things don't think garbage.
- 54:04Yeah.
- 54:05Makes sense.
- 54:05Yeah.
- 54:06I can imagine low level.
- 54:07This is a beast.
- 54:08Yeah.
- 54:09The debugging can be tricky, but it's a lot of fun.
- 54:12And it's a very, I find it sort of a therapeutic project because like, it's like, it's tricky and difficult, but it's also like a very, it's like a closed universe.
- 54:22You know, you're doing web development or distributed systems.
- 54:25It's like, you're talking to remote services and like, you have to spin up five processes and like, you're dependent on the whole external world to make anything work these days.
- 54:35Otherwise, this is sort of like, it's a program that runs in your computer, read some data, write some data.
- 54:40Like there's no, there's no outside world.
- 54:43Yeah, that's cool.
- 54:44So it's just like, you can stay focused on the problem on hand and not the fact that like GitHub is down or whatever.
- 54:51Yeah, I've been there.
- 54:52All right.
- 54:53Before I let you out here though, let me ask you the final two questions.
- 54:55If you're going to write some Python code, what editor do you use?
- 54:58I use space max, which is a configuration of Emacs that makes Emacs a lot more like a modern ID.
- 55:04Nice.
- 55:05Okay.
- 55:06It makes Emacs like experience jump 20 years forward.
- 55:10That's awesome.
- 55:11Just by installing and configuring the right packages.
- 55:12Cool.
- 55:13And a notable PI PI package.
- 55:16Pip install.
- 55:17Is it Phil or Phil dash profile?
- 55:18I got a pip install.
- 55:19It's Phil profiler, no dash or hyphen.
- 55:22So like F I L E R F I L E R.
- 55:26That's an obvious one.
- 55:27What's another one that maybe you've come across recently and you're like, oh, this is really cool.
- 55:30People should know about.
- 55:31Nothing is, I guess, to mention Austin.
- 55:36I don't know quite as much about it, but Pyspy is another, another sampling profiler.
- 55:42And it's another kind of a system programming package where like it's doing these interesting things in Rust where it's like, it looks at like the memory layout.
- 55:52It doesn't, it looks at the memory layout of your Python program, like parses out the data structures and reads things out.
- 55:58So it's another sort of very intense system programming, which ideally is all hidden behind the scenes.
- 56:05It just gives you really useful results.
- 56:06Cool.
- 56:07All right.
- 56:08That's a good one.
- 56:09Yeah.
- 56:10I have to check it out.
- 56:11I haven't tried that one.
- 56:12All right.
- 56:13Final call to action.
- 56:14People want to get started with Phil.
- 56:15What do they do?
- 56:16Go to pythonspeed.com/products/philprofiler.
- 56:18Maybe get a URL wrong.
- 56:19I should probably get a shorter URL.
- 56:22Excuse me.
- 56:23Google F-I-L space profiler.
- 56:24That should do it.
- 56:25Or you can put a link in the show notes.
- 56:27Yeah.
- 56:28I'll definitely have a link in the show notes.
- 56:29No doubt.
- 56:30Yeah.
- 56:31Google in Phil space profiler works for me.
- 56:32Works for me.
- 56:33Or you can go to pythonspeed.com and then I'll do some links to that and other stuff I've
- 56:37written.
- 56:38All right.
- 56:39Very cool.
- 56:40Also include the link to your virtual PyCon talk as well so people can check that out.
- 56:43Cool.
- 56:44All right.
- 56:45Thanks for having me.
- 56:46This has been another episode of Talk Python To Me.
- 56:48Our guest on this episode was Itamar Turing-Trowing.
- 56:51And it's been brought to you by Linode and us over at Talk Python Training.
- 56:55Start your next Python project on Linode's state of the art cloud service.
- 56:59Just visit talkpython.fm/linode.
- 57:03You'll automatically get a $20 credit when you create a new account.
- 57:07Want to level up your Python?
- 57:09If you're just getting started, try my Python jumpstart by building 10 apps course.
- 57:14Or if you're looking for something more advanced, check out our new async course that digs into
- 57:19all the different types of async programming you can do in Python.
- 57:22And of course, if you're interested in more than one of these, be sure to check out our
- 57:26everything bundle.
- 57:27It's like a subscription that never expires.
- 57:29Be sure to subscribe to the show.
- 57:31Open your favorite podcatcher and search for Python.
- 57:33We should be right at the top.
- 57:34You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct
- 57:40RSS feed at /rss on talkpython.fm.
- 57:44This is your host, Michael Kennedy.
- 57:46Thanks so much for listening.
- 57:47I really appreciate it.
- 57:48Now get out there and write some Python code.
- 57:50I'll see you next time.
- 57:51Bye.
- 57:51Bye.
- 57:52Bye.
- 57:53Bye.
- 57:54Bye.
- 57:55Bye.
- 57:56Bye.
- 57:57Bye.
- 57:58Bye.
- 57:59Bye.
- 58:00Bye.
- 58:01Bye.
- 58:02Bye.
- 58:03Bye.
- 58:04Bye.
- 58:04Bye.
- 58:05Bye.
- 58:05Bye.
- 58:06Bye.
- 58:07you
- 58:08you
- 58:08Thank you.
- 58:10Thank you.