Latest / Talk Python To Me / #265: Why is Python slow?
Transcript
- 0:00The debate about whether Python is fast or slow is never-ending.
- 0:03It depends on what you're optimizing for.
- 0:05CPU server consumption?
- 0:07Developer time?
- 0:08Maintainability?
- 0:09There are many factors.
- 0:11But if we keep our eye on the pure computational speed in the Python layer,
- 0:15then yes, Python is slow.
- 0:17In this episode, we invite Anthony Shaw back on the show.
- 0:21He's here to dig into the reasons that Python is computationally slower
- 0:24than many of its pure languages and technologies, such as C++ and JavaScript.
- 0:29This is Talk Python To Me, episode 265, recorded May 19, 2020.
- 0:34Welcome to Talk Python To Me, a weekly podcast on Python,
- 0:51the language, the libraries, the ecosystem, and the personalities.
- 0:54This is your host, Michael Kennedy.
- 0:56Follow me on Twitter, where I'm @mkennedy.
- 0:58Keep up with the show and listen to past episodes at talkpython.fm,
- 1:02and follow the show on Twitter via at Talk Python.
- 1:04This episode is sponsored by Brilliant.org and Sentry.
- 1:08Please check out their offerings during their segments.
- 1:10It really helps support the show.
- 1:12Anthony, welcome back to Talk Python.
- 1:15Hey, Mike.
- 1:15It's great to be back.
- 1:16Yeah, it's great to have you back.
- 1:17You've been on the show a bunch of times.
- 1:19You've been over on Python Bytes when you're not featured there.
- 1:22But, you know, people may know you were on episode 168, 10 Python security holes and how to plug them.
- 1:29That was super fun with one of your colleagues.
- 1:31And then 214, dive into the CPython 3.8 source code.
- 1:36Or just what was new in 3.8.
- 1:38And then a guided tour of the CPython source code, which I think at the time was also 3.8.
- 1:42And now we're going to look at the internals of Python again.
- 1:45I feel like you're becoming the Python internals guy.
- 1:48Yeah.
- 1:48Well, I don't know.
- 1:50There's lots of people who know a lot more about it than I do.
- 1:53But I've been working on this book over the last year on CPython internals, which has been focused on 3.9.
- 2:00So, yeah, we've got some stuff to talk about.
- 2:03Yeah, that's awesome.
- 2:04And your book started out as a realpython.com article, which I'm trying to define a term that describes what some of these look like.
- 2:13When I think of article, I think of a three to four page thing.
- 2:17Maybe it's in depth and it's 10 pages.
- 2:19This is like 109 pages or something as an article, right?
- 2:22It was like insane.
- 2:23But it was really awesome and really in depth.
- 2:24And so you were partway towards a book and you figured like, well, what the heck?
- 2:28I'll just finish up this walk.
- 2:29Yeah, I figured I'd pretty much written a book.
- 2:32So I might as well put it between two covers.
- 2:34It was actually a lot.
- 2:36It was actually a lot of work to get it from that stage to where it is now.
- 2:41So I think the whole thing's pretty much been rewritten.
- 2:43There's a way that you explain things in an article that people expect, which is very different to the style of a book.
- 2:49And also there's stuff that I kind of skimmed over in the article.
- 2:53I think it's actually about three times longer than the original article.
- 2:56And it's a lot more practical.
- 2:59So rather than being like a tourist guide to the source code, it's more about like CPython internals and optimizations and practical tools you can learn as more sort of like advanced techniques.
- 3:12If you use CPython a lot for your day job to either make it more performant or to optimize things or to make it more stable and stuff like that.
- 3:21Yeah.
- 3:21It's really interesting because if you want to understand how Python works and you're, say, the world's best Python developer, your Python knowledge is going to help you a little bit.
- 3:31But not a ton for understanding CPython because that's mostly, well, C code, right?
- 3:36And so I think this having this guided tour, this book that talks about that is really helpful, especially for taking people who know and love Python, but actually want to get a little deeper and understand the internals or maybe even become a core developer.
- 3:49Yeah, definitely.
- 3:49And if you look at some of the stuff we'll talk about this episode, hopefully, like Cython and mypyc and stuff like that, then knowing C or knowing how C and Python work together is also really important.
- 4:02Yeah, absolutely.
- 4:02All right.
- 4:03So looking forward to talking about that.
- 4:05But just really quickly, you know, give people a sense of what you work on day to day when you're not building extensions for IDEs, writing books and otherwise doing more writing.
- 4:15Yeah, so I work at NTT and run sort of learning and development and training for the organization.
- 4:22So I'm involved in, I guess, like what skills we teach our technical people and our sales people and all of our employees, really.
- 4:30Yeah, that's really cool.
- 4:31That sounds like a fun place to be.
- 4:32Yeah, that's a great job.
- 4:33Yeah, awesome.
- 4:33All right.
- 4:34Well, the reason I reached out to you about having you on the show for this specific topic, I always like to have you on the show.
- 4:42We always have fun conversations, but I saw that you were doing, were you doing multiple or just this PyCon talk?
- 4:50Just one.
- 4:51I was accepted for two, but I was supposed to pick one.
- 4:55I see.
- 4:55That's right.
- 4:55That's right.
- 4:56And then PyCon got canceled.
- 4:57Yeah.
- 4:59So I was like, well, let's, you know, talk.
- 5:00We can talk after PyCon after you give your talk.
- 5:02It'll be really fun to cover this.
- 5:04And then, you know, we were supposed to share a beer in Pittsburgh and we're like half a world away.
- 5:12Didn't happen, did it?
- 5:13Yeah.
- 5:13Maybe next year.
- 5:14Yeah.
- 5:15Hopefully next year.
- 5:15Hopefully things are back to up and running because I don't know.
- 5:18To me, PyCon is kind of like my geek holiday that I get to go on.
- 5:22I love it.
- 5:22Yeah.
- 5:23All right.
- 5:23Well, so just, I guess, for people listening, you did end up doing that talk in an altered sense,
- 5:30right?
- 5:30And they can technically go watch it soon, at least maybe by the time this is out.
- 5:34Yeah, definitely.
- 5:35It'll be out tonight.
- 5:36It's going to be on the YouTube channel, the PyCon 2020 YouTube channel.
- 5:41The organizers reached out to all the speakers and said, if you want to record your talk and
- 5:46submit it from home, then you can still do that and put them all up on YouTube.
- 5:50I think that's great.
- 5:51You know, and there's also a little bit more over PyCon online.
- 5:54One thing I think is really valuable for people right now is they have the job fair, kind of,
- 6:00right?
- 6:01There's a lot of job listings for folks who are looking to get in jobs.
- 6:05Have you seen the PSF JetBrains survey that came out?
- 6:08Yes.
- 6:09In the 2019, it came out just like a few days ago.
- 6:11Really interesting stuff, right?
- 6:13Like a lot of cool things in there.
- 6:14Yeah, definitely.
- 6:15Yeah.
- 6:15I love that.
- 6:16That and the Stack Overflow developer survey.
- 6:18Those are the two that really, I think, have the pulse correctly taken.
- 6:22One of the things that was in there I thought was interesting is more than any other category
- 6:27of people, they said, how long have you been coding?
- 6:30I don't know if it was in Python or just how long have you been coding, but it was different,
- 6:36you know, one to three years, three to five, five to 10, 10 to 15.
- 6:41And then people like me forever, long time, you know, like 20 plus or something.
- 6:45The biggest bar of all those categories, the biggest group was the one to three years.
- 6:51Yeah.
- 6:52Right.
- 6:52Like by 29% of the people said, I've only been coding three years or fewer.
- 6:56And I think that that's really interesting.
- 6:58So I think things like that job board and stuff are probably super valuable for folks just getting
- 7:02into things.
- 7:03Definitely.
- 7:03Yeah.
- 7:03So really good that they're putting that up and people will be able to check out your
- 7:07talk.
- 7:07I'll put a link to it in the show notes, of course, but they can just go to the PyCon 2020
- 7:11YouTube channel and check it out there.
- 7:13Yeah.
- 7:13And check out the other talks as well.
- 7:15There's some really good ones up already.
- 7:16The nice thing about this year's virtual PyCon is you can watch talks from your couch.
- 7:20That's right.
- 7:22You don't even have to get dressed to go to PyCon.
- 7:25Just do it in your PJs.
- 7:26That's right.
- 7:27It's so much more comfortable than the conference chairs.
- 7:31That's true.
- 7:31That's for sure.
- 7:32Yeah.
- 7:33Very cool.
- 7:33I'm definitely looking forward to checking out more of the talks as well.
- 7:35I've already watched a few.
- 7:36I wanted to set the stage for our conversation here by defining slow because I think slow is
- 7:44in the eye of the beholder, just like beauty, right?
- 7:46Like sometimes slow doesn't matter.
- 7:50Sometimes computational speed might be slow, but some other factor might be quick.
- 7:57So I'll let you take a shot at it, then I'll throw in my two cents as well.
- 8:00Like let's like, what do you mean when you say, why is Python slow?
- 8:04So when I say, why is Python slow?
- 8:06The question is, why is it slower than other languages doing exactly the same thing and have
- 8:14picked on an error?
- 8:15Right.
- 8:15So if I had an algorithm that I implemented, say in C, a JavaScript on top of Node and Python,
- 8:20it might be much slower in Python.
- 8:23Wall time, like execution time.
- 8:25Yeah.
- 8:26Execution time might be much slower in Python than it is in other languages.
- 8:29And that matters sometimes.
- 8:31And sometimes it doesn't matter as much.
- 8:34It depends what you're doing, right?
- 8:35If you're doing like a DevOps-y thing and you're trying to orchestrate calling into Linux, well,
- 8:40who cares how fast Python goes?
- 8:42Probably like the startup time is the most important of all of them.
- 8:45If you're modeling stuff and you're trying to do the mathematical bits, anything computational,
- 8:51and you're doing that in Python, then it really might matter to you.
- 8:54Yeah.
- 8:55So it was kind of like a question, if we can find out the answer, maybe there's a solution
- 9:00to it.
- 9:00Yeah.
- 9:01Because, you know, you hear this thrown around.
- 9:02People say Python's too slow and I use this other language because it's faster.
- 9:06And so I just wanted to understand, like, what is the actual reason why Python is slower
- 9:12at doing certain things than other languages?
- 9:14And is there a reason that can be resolved?
- 9:18Or is it just that's just how it is as part of the design?
- 9:22Fundamentally, it's going to be that way.
- 9:23Yeah.
- 9:24I don't think it is.
- 9:25I think...
- 9:26You don't think it's slow?
- 9:27No, I don't think it's fundamentally has to be that way.
- 9:30I agree with you.
- 9:31I think in the research as well, it uncovered it doesn't fundamentally have to be that way.
- 9:36And in lots of cases, it isn't that way either.
- 9:38Like there's ways to get around the slowdown, like the causes of slowdown.
- 9:44And if you understand in what situations Python can be slow, then you can kind of like bypass
- 9:51those.
- 9:51Right.
- 9:52So let me tell a really interesting story that comes from Michael Driscoll's book, Python
- 9:57Interviews.
- 9:58So over there, he interviewed, I think it was Alex.
- 10:02Yeah, Alex Martelli.
- 10:03And they talked about the history of YouTube, right?
- 10:07YouTube's built on Python.
- 10:09And why is that the case?
- 10:11Originally, there was Google Video, which had hundreds of engineers writing, implementing
- 10:18Google Video, which is going to be basically YouTube.
- 10:21But YouTube was also a startup around the same time, right?
- 10:24And they were kind of competing for features and users and whatnot.
- 10:26And YouTube only had like 20 employees at the time or something like that, whereas Google
- 10:31had hundreds of super smart engineers.
- 10:34And Google kept falling behind farther and farther and not be able to implement the features that
- 10:39people wanted nearly as quick as YouTube.
- 10:41YouTube.
- 10:41And the reason was they were all doing it in C++ and it took a long time to get that written.
- 10:47And YouTube just ran circles around them with a, you know, more less than a fifth of the
- 10:52number of people working on it.
- 10:53So in some sense, like that's a testament of Python speed, right?
- 10:58But it's not its execution speed.
- 11:00It's like the larger view of speed, which is why I really wanted to find like what computational
- 11:04speed is.
- 11:05Another sense where it may or may not matter is like where you're doing stuff that waits,
- 11:10right?
- 11:10Somewhere where asyncio would be a really good option, right?
- 11:13I'm talking to Redis.
- 11:14I'm talking to this database.
- 11:15I'm calling this API.
- 11:16Like if 95% of your time is waiting on a network response, it probably doesn't matter, right?
- 11:21As long as you're using some sort of async or something.
- 11:24But then there's that other part where it's like I have on my computer, I've got six hyperthreaded
- 11:30cores.
- 11:30Why can I only use one twelfth of my computational power on my computer if I still write C code,
- 11:36right?
- 11:37So there's these other places where it super matters.
- 11:39Or I just, like you said, there's this great example that we're going to talk about the
- 11:43in-body problem, modeling like planets and how they interact with each other.
- 11:48And I mean, just like to set the stage, what was the number for C versus Python in terms
- 11:53of time, computation time?
- 11:54To give people a sense, like why did we care?
- 11:56Like why is this a big enough deal to worry about?
- 11:58Is it, what is it like 30% slower?
- 12:00It's a little bit slower.
- 12:01Yeah.
- 12:01It's a, for this algorithm, so this is called the end body problem and it's to do with calculating
- 12:07the orbits of some of the planets in the solar system.
- 12:10And you just do a lot, a really simple arithmetic operations.
- 12:15So just adding numbers, but again and again and again.
- 12:17So millions of times.
- 12:18Lots of loops, lots of math.
- 12:20Lots of math, lots of looping.
- 12:22And in C, this implementation is seven seconds to complete.
- 12:27And in Python, it's 14 minutes.
- 12:29That might be a difference that you're needing to optimize away.
- 12:32That could be too much, right?
- 12:34Yeah.
- 12:34I mean, everyone is calculating the orbits of planets as part of their day job.
- 12:38So yeah.
- 12:39You know, I honestly, I haven't really done that for at least two weeks.
- 12:44No, but I mean, it's, it's fundamentally like I'm thinking about like, this is, I think this
- 12:48undercovers one of the real Achilles heels of Python in that doing math in tight loops is really not super great in pure Python.
- 13:00Right.
- 13:01Whether that's planets, whether that's financial calculations or something else.
- 13:05Right.
- 13:05But numbers are very flexible, but that makes them inefficient.
- 13:08Right.
- 13:09Python is interpreted, which has a lot of benefits, but also can make it much slower as well.
- 13:15Right.
- 13:15Yeah.
- 13:16So I think looking at this particular problem, because I thought it would be a good example,
- 13:20it shines a bit of a spotlight on one of CPython's weaknesses when it comes to performance.
- 13:26But in terms of like the loop, the only times you would be doing like a small loop and doing
- 13:31the same thing over and over again is if you're doing like math work, doing like number crunching,
- 13:37or if you're doing benchmarks, that's like one of the other reasons.
- 13:41So like the way that a lot of benchmarks designed to do like computational benchmarks anyway,
- 13:47is to do the same operation again and again.
- 13:49So if there is an overhead or a slowdown, then it's magnified to the point where you can see
- 13:55it a lot bigger.
- 13:55Yeah, for sure.
- 13:56I guess one thing to put out there before people run code, it doesn't go as fast as they'd hoped.
- 14:04So they say that Python is slow, right?
- 14:07Assuming the code they originally ran was Python like that.
- 14:09That would be a requirement, I guess, is you probably should profile it.
- 14:13You should understand what your code is doing and where it's slow.
- 14:17Like, for example, if you're doing lookups, but your data structure is a list instead of
- 14:21a dictionary, right?
- 14:23You could make that a hundred times faster just by switching a date because you're just doing
- 14:26the wrong type of data structure, the wrong algorithm.
- 14:29It could be just that you're doing it wrong, right?
- 14:32So I guess before people worry about like, is it executing too slowly?
- 14:37Maybe you should make sure that it's executing the right thing.
- 14:40Yeah, and it's unlikely that your application is running a very small operation, which is
- 14:47this benchmark again and again, like millions of times in a loop.
- 14:50And if you are doing that, there's probably other tools you could use and there's other
- 14:55implementations you can do in Python.
- 14:59This portion of Talk Python To Me is brought to you by Brilliant.org.
- 15:03Brilliant's mission is to help people achieve their learning goals.
- 15:06So whether you're a student, a professional brushing up or learning cutting edge topics,
- 15:10or someone who just wants to understand the world better, you should check out Brilliant.
- 15:14Set a goal to improve yourself a little bit every day.
- 15:17Brilliant makes it easy with interactive explorations and a mobile app that you can use on the go.
- 15:22If you're naturally curious, want to build your problem solving skills, or need to develop
- 15:26confidence in your analytical abilities, then get Brilliant Premium to learn something new
- 15:31every day.
- 15:32Brilliant's thought-provoking math, science, and computer science content helps guide you
- 15:37to mastery by taking complex concepts and breaking them into bite-sized, understandable chunks.
- 15:42So get started at talkpython.fm/brilliant, or just click the link in your show notes.
- 15:50Another benchmark I covered in the talk was the regular expression benchmark,
- 15:54which Python is actually really good at.
- 15:57So this is like the opposite to this particular benchmark.
- 16:01So just saying that Python is slow isn't really a fair statement,
- 16:04because, and we'll kind of talk about this in a minute,
- 16:07but like for other benchmarks, Python does really well.
- 16:11So its string implementation is really performant.
- 16:14And when you're working with text-based data,
- 16:17Python's actually a great platform to use, a great language to use.
- 16:20The CPython compilers is pretty efficient at dealing with text data.
- 16:25And if you're working on web applications or data processing,
- 16:29chances are you're dealing with text data.
- 16:32Yeah, that's a good example.
- 16:33Like the websites that I have, like the Talk Python training site,
- 16:37and the various podcast sites and stuff,
- 16:39they're all in Python with no special,
- 16:42incredible optimizations,
- 16:43other than like databases with indexes and stuff like that.
- 16:46And, you know, the response times are like 10, 30 milliseconds.
- 16:51There's no problem.
- 16:52Like it's fantastic.
- 16:53It's really, really good.
- 16:54But there are those situations like this in-body problem
- 16:59or other ones where it matters.
- 17:01I don't know if it's fair or not to compare it against C, right?
- 17:04C is really, really low level,
- 17:07at least from today's perspective.
- 17:10It used to be a high level language,
- 17:11but now I see it as a low level language.
- 17:13If you do a malloc and free and, you know,
- 17:15the address of this thing, right,
- 17:17that feels pretty low level to me.
- 17:19So maybe it's unfair.
- 17:21I mean, you could probably get something pretty fast in assembly,
- 17:24but I would never choose to use assembly code these days
- 17:27because it's just like I want to get stuff done and maintain it
- 17:29and be able to have other people understand what I'm doing.
- 17:31But, you know, kind of a reasonable comparison,
- 17:36I think, would be Node.js and JavaScript.
- 17:38And you made some really interesting compare and contrast
- 17:42between those two environments
- 17:43because they seem like, well, like, okay, Python,
- 17:46at least it has some C in their JavaScript.
- 17:48Who knows what's going on with that thing, right?
- 17:50Like, you know, what's the story between those two?
- 17:52Yeah, you make a fair point, which is,
- 17:53I mean, comparing C and Python isn't really fair.
- 17:56One is like a strongly typed compiled language.
- 17:59The other is a dynamically typed interpreted language
- 18:03and they handle memory differently.
- 18:06Like in C, you have to statically or dynamically allocate memory
- 18:10and CPython is done automatically.
- 18:12Like it has a garbage collector.
- 18:14There's so many differences between the two platforms.
- 18:16And so I think Node.js, which is,
- 18:20so Node.js is probably a closer comparison to Python.
- 18:24Node.js isn't a language.
- 18:25It's a kind of like a stack that sits on top of JavaScript
- 18:29that allows you to write JavaScript,
- 18:32which operates with things that run in the operating system.
- 18:36So similar to CPython, like CPython has extensions
- 18:40that are written in C that allow you to do things
- 18:43like connect to the network
- 18:45or, you know, connect to like physical hardware
- 18:49or talk to the operating system in some way.
- 18:51Like if you just wrote pure Python and there was no C,
- 18:53you couldn't do that because the operating system APIs
- 18:56are C headers in most cases.
- 18:59Right.
- 18:59Almost all of them are in C somewhere.
- 19:01Yeah.
- 19:01Yeah.
- 19:02And with JavaScript, it's the same thing.
- 19:03Like if you want to talk to the operating system
- 19:05or do anything other than like working with stuff
- 19:09that's in the browser,
- 19:10you need something that plugs into the OS.
- 19:12And Node.js kind of provides that stack.
- 19:15So when I wanted to compare Python with something,
- 19:17I thought Node was a better comparison
- 19:21because like JavaScript and Python,
- 19:23in terms of the syntax, they're very different.
- 19:25But in terms of their capabilities, they're quite similar.
- 19:29You know, they both have classes and functions
- 19:31and you can use them interchangeably.
- 19:33They're both kind of like dynamically typed.
- 19:35The scoping is different and the language is different.
- 19:37But like in terms of the threading as well,
- 19:41they're quite similar.
- 19:42Right.
- 19:42They do feel much more similar.
- 19:44But there's a huge difference between how they run,
- 19:48at least when run on Google's V8 engine,
- 19:51which basically is the thing behind Node and whatnot,
- 19:54versus CPython is,
- 19:56CPython is interpreted and V8 is JIT compiled,
- 20:01just in time compiled.
- 20:02Yeah, so that's probably one of the biggest differences.
- 20:04And when I was comparing the two,
- 20:06so I wanted to see, okay, which one is faster?
- 20:10Like if you gave it the same task
- 20:12and if you gave it the end body problem,
- 20:13then Node.js is a couple of multiples faster.
- 20:18I think it was two or three times faster
- 20:21to do the same algorithm.
- 20:23And for a dynamically typed language,
- 20:25you know, that means that they must have some optimizations,
- 20:28which make it faster.
- 20:30I mean, if you're running on the same hardware,
- 20:32then, you know, what is the overhead?
- 20:34And kind of digging into it, I guess,
- 20:37in a bit more detail.
- 20:39So JavaScript has this,
- 20:40actually there's multiple JavaScript engines,
- 20:43but kind of the one that Node.js uses
- 20:45is Google's V8 engine.
- 20:47So quite cleverly named,
- 20:49which is all written in...
- 20:52Only would it be better if it were a V12, you know?
- 20:54Or an inline six.
- 20:56I think that's a better option.
- 20:57Yeah, there you go.
- 21:01So Google's V8 JavaScript engine is written in C++,
- 21:05so maybe that's a fair comparison.
- 21:07But the optimizing compiler is called TurboFan,
- 21:11and it's a JIT optimizing compiler.
- 21:14So it's a just-in-time compiler,
- 21:15whereas CPython is an ahead-of-time or an AIT compiler.
- 21:20And it's JIT optimizer has got some really clever,
- 21:24basically sort of algorithms and logic
- 21:27that it uses to optimize the performance
- 21:29of the application when it actually runs.
- 21:31And these can make a significant difference.
- 21:33Like some of the small optimizations alone
- 21:36can make 30%, 40% increase in speed.
- 21:39And if you compare even just V8
- 21:42compared to other JavaScript engines,
- 21:44you can see, like,
- 21:45what all this engineering can do
- 21:47to make the language faster.
- 21:49And that's how it got two, three multiples
- 21:51performance increases,
- 21:53was to optimize the JIT
- 21:54and to understand, like,
- 21:56how people write JavaScript code
- 21:58and the way that it compiles the code
- 22:01down into operations.
- 22:03Then basically, like,
- 22:04it can reassemble those operations
- 22:06that are more performant for the CPU
- 22:08so that when it actually executes them,
- 22:10does it in the most efficient way possible.
- 22:12Right.
- 22:13The difference between a JIT and an AOT
- 22:14is that the JIT compiler
- 22:16kind of makes decisions
- 22:18about the compilation
- 22:19based on the application
- 22:21and based on the environment,
- 22:22whereas an AOT compiler
- 22:24will compile the application the same
- 22:27and it does it all ahead of time.
- 22:29Right.
- 22:29So you probably have a much more
- 22:31coarsely-grained set of optimizations
- 22:33and stuff for an ahead-of-time compiler,
- 22:36like C++ or something, right?
- 22:38Like, I've compiled against x86 Intel CPU
- 22:42with, like, the multimedia extensions
- 22:47or whatever, right?
- 22:48The scientific computing extensions.
- 22:49But other than that,
- 22:50I make no assumptions,
- 22:51whether it's multi-core,
- 22:53highly multi-core,
- 22:54what its L2 cache is,
- 22:56none of that stuff, right?
- 22:57It's just,
- 22:58we're going to kind of target
- 22:59modern Intel on macOS
- 23:03and do it on Windows
- 23:04and compile that.
- 23:05Yeah.
- 23:05So modern CPU architectures
- 23:08and modern OSes
- 23:09can really benefit
- 23:10if you've optimized
- 23:12the instructions
- 23:13that you're giving them
- 23:14to benefit,
- 23:15like,
- 23:16the caches that they have
- 23:17or the cycles
- 23:18that they've set up
- 23:19and the sort of
- 23:20the turbo fan optimizer
- 23:22for the VA engine
- 23:23takes a lot of advantage
- 23:24of those things.
- 23:25Yeah.
- 23:25That seems really powerful.
- 23:27I guess we should step back
- 23:28and talk a little bit
- 23:29about how CPython runs,
- 23:32but being an interpreter,
- 23:34it can only optimize so much.
- 23:37It's got all of its byte codes
- 23:39and it's going to go through
- 23:40its byte codes
- 23:41and execute them,
- 23:42but saying, like,
- 23:43well, these five byte codes,
- 23:44we could actually turn that
- 23:45into an inline thing over here
- 23:47and I see this actually
- 23:48has no effect
- 23:49on what's loaded on the stack,
- 23:51so we're not going to, like,
- 23:52push the item.
- 23:53I mean,
- 23:53it seems like it doesn't operate
- 23:55optimizing,
- 23:56tell me if I'm wrong,
- 23:57if it doesn't optimize,
- 23:58like, across lots of byte codes
- 24:02as it's thinking about it.
- 24:04Yeah, so what CPython will do
- 24:05when it compiles your code,
- 24:07and it's also worth pointing out
- 24:08that when you run your code
- 24:11for the first time,
- 24:12it will compile it,
- 24:13but when you run it again,
- 24:14it will use the cached version,
- 24:15so...
- 24:16Right, if you ever see
- 24:17the dunder pycache
- 24:18with .pyc files,
- 24:20that's, like,
- 24:21three of the four steps
- 24:22of getting your code ready to run
- 24:23saved and done
- 24:25and never done again.
- 24:26Yeah, so that's, like,
- 24:27the compiled version.
- 24:28So it's not...
- 24:29If Python is slow
- 24:30to compile code,
- 24:31it doesn't really matter
- 24:32unless your code
- 24:33is somehow changing
- 24:34every time it gets run,
- 24:36which I'd be worried about.
- 24:37You have bigger problems.
- 24:38Yeah, exactly.
- 24:39So the benefits, I guess,
- 24:41of an AOT compiler
- 24:43is that you compile things
- 24:45ahead of time
- 24:45and then when they execute,
- 24:46they should be efficient.
- 24:47So CPython's compiler
- 24:50will kind of take your code,
- 24:52which is, like,
- 24:52a text file,
- 24:53typically.
- 24:53It'll look at the syntax.
- 24:55It will parse that
- 24:56into an abstract syntax tree,
- 24:59which is a sort of
- 25:00representation of functions
- 25:02and classes
- 25:03and statements
- 25:04and variables
- 25:05and operations
- 25:06and all that kind of stuff.
- 25:08your code,
- 25:08your file,
- 25:09your module,
- 25:10basically,
- 25:10becomes like a tree
- 25:12and then what it does
- 25:13is it then compiles that tree
- 25:15by walking through
- 25:17each of the branches
- 25:18and walking through
- 25:19and understanding
- 25:19what the nodes are
- 25:20and then there is a compilation.
- 25:23Basically,
- 25:24like, in the CPython compiler,
- 25:26there's a function
- 25:26for each type of thing
- 25:28in Python.
- 25:28so there's a compile binary operation
- 25:32or there's a compile class function
- 25:34and a compile class
- 25:35will take a node
- 25:37from the AST,
- 25:38which has got your class in it
- 25:39and it will then go through
- 25:41and say,
- 25:41okay,
- 25:42what properties,
- 25:42what methods does it have
- 25:44and it will then go
- 25:45and compile the methods
- 25:46and then inside a method
- 25:47it will go and compile the statements.
- 25:48So, like,
- 25:49once you break down
- 25:50the compiler
- 25:50into smaller pieces,
- 25:51it's not that complicated
- 25:53and what a compiler will do
- 25:55is it will spit out
- 25:56so compiled basic frame blocks
- 25:59they're called
- 26:00and then they get assembled
- 26:02into bytecode.
- 26:03So, after the compiler stage,
- 26:05there is an assembler stage
- 26:06which basically figures out
- 26:08in which sequence
- 26:10should the code be executed,
- 26:11you know,
- 26:12which basically,
- 26:13like,
- 26:13what will the control flow be
- 26:15between the different parts of code,
- 26:17the different frames.
- 26:18In reality,
- 26:19like,
- 26:19they get executed
- 26:20in different orders
- 26:21because they depend on input
- 26:23whether or not
- 26:23you call this particular function
- 26:25but still,
- 26:26like,
- 26:26if you've got a for loop,
- 26:27then it's still got to go
- 26:29inside the for loop
- 26:30and then back to the top again.
- 26:31Like,
- 26:32that logic is,
- 26:33like,
- 26:33hard-coded into the for loop.
- 26:34Right.
- 26:35You know,
- 26:35as you're talking,
- 26:36I'm wondering if,
- 26:37you know,
- 26:38minor extensions
- 26:39to the language
- 26:40might let you
- 26:41do higher-level optimizations.
- 26:43Like,
- 26:43say,
- 26:44like,
- 26:44having a frozen class
- 26:46that you're saying
- 26:46I'm not going to add any fields to
- 26:49or,
- 26:49like,
- 26:50an inline on a function,
- 26:52like,
- 26:52I only,
- 26:52or make it a
- 26:53function internal
- 26:54to a class
- 26:55in which it could be
- 26:57inlined,
- 26:57potentially,
- 26:58because,
- 26:58you know,
- 26:58no one's going to be able to,
- 26:59like,
- 26:59look at it from the outside
- 27:01of this code and stuff.
- 27:02What do you think?
- 27:03There is an optimizer
- 27:04in the compiler
- 27:05called the peephole optimizer.
- 27:07And when it's compiling,
- 27:10I think it's actually
- 27:10it's after the compilation stage,
- 27:12I think,
- 27:13it goes through
- 27:14and it looks at the
- 27:15code that's been compiled
- 27:17and if it can make some
- 27:18decisions about
- 27:19either,
- 27:20like,
- 27:21dead code
- 27:22that can be removed
- 27:23or branches
- 27:23which can be simplified,
- 27:25then it can basically
- 27:26optimize that.
- 27:27And that will make
- 27:28some improvement,
- 27:29like,
- 27:29it will optimize
- 27:30your code slightly.
- 27:31Right.
- 27:32But then once it's done,
- 27:33basically,
- 27:33your Python application
- 27:34has been compiled down
- 27:36into this,
- 27:36like,
- 27:37assembly language
- 27:38called bytecode,
- 27:39which is the,
- 27:40like,
- 27:40the actual individual operations
- 27:42and then they're
- 27:44executed in sequence.
- 27:45They're split up
- 27:46into small pieces,
- 27:47they're split up
- 27:48into frames,
- 27:48but they're executed
- 27:50in sequence.
- 27:50Right.
- 27:51And if you look
- 27:51at the C source code,
- 27:53dive into there,
- 27:54there's a C eval.c file
- 27:56and it has,
- 27:57like,
- 27:57the world's largest
- 27:59while loop
- 28:00with a switch statement
- 28:01in it,
- 28:02right?
- 28:02Yeah.
- 28:02So this is,
- 28:03like,
- 28:03the kind of the brain
- 28:05of CPython.
- 28:06Oh,
- 28:06maybe it's not the brain,
- 28:07but it's the bit that,
- 28:08like,
- 28:09goes through each
- 28:10of the operations
- 28:10and says,
- 28:11okay,
- 28:11if it's this operation,
- 28:12do this thing,
- 28:13if it's that one,
- 28:14do this thing.
- 28:14This is all compiled in C,
- 28:16so it's fairly fast,
- 28:18but it will basically
- 28:19sit and run the loop.
- 28:20So when you actually
- 28:21run your code,
- 28:22it takes the assembled
- 28:24bytecode
- 28:25and then for each
- 28:26bytecode operation,
- 28:27it will then
- 28:27do something.
- 28:29So,
- 28:29for example,
- 28:30there's a bytecode
- 28:31for add an item
- 28:33to a list.
- 28:33So it knows that
- 28:35it will make a value
- 28:36off the stack
- 28:36and it will put that
- 28:37into the list
- 28:38or this one
- 28:39which calls a function.
- 28:40So,
- 28:40if the bytecode
- 28:41is call function,
- 28:42then it knows to
- 28:43figure out how to
- 28:44call that function
- 28:45in C.
- 28:46Right.
- 28:46Maybe it's loaded
- 28:47a few things
- 28:47on the stack,
- 28:48it's going to call it,
- 28:49do it just get sucked along,
- 28:51something like that.
- 28:51And so I guess
- 28:53one of the interesting things,
- 28:55and you were talking
- 28:56about an interesting
- 28:56analogy about this,
- 28:58sort of when Python
- 28:59can be slow
- 29:00versus a little bit
- 29:01less slow,
- 29:02it's the overhead
- 29:03of like going
- 29:04through that loop,
- 29:05figuring out what to do,
- 29:06like preparing stuff
- 29:06before you call
- 29:07the CPython's
- 29:09thing,
- 29:10right?
- 29:10Like list.sort,
- 29:11it could be super fast
- 29:13even for a huge list
- 29:14because it's just going
- 29:15to this underlying
- 29:16C object
- 29:16and say,
- 29:17in C,
- 29:17go do your sort.
- 29:18But if you're doing
- 29:20a bunch of
- 29:20small steps,
- 29:21like the overhead
- 29:23of the next step
- 29:24can be a lot higher.
- 29:26in the nbody problem,
- 29:27the step that it has
- 29:29to do,
- 29:29the operation it has
- 29:30to do,
- 29:30will be add number
- 29:31A to number B,
- 29:32which on a decent CPU,
- 29:35I mean,
- 29:35this is like nanoseconds
- 29:36in terms of time
- 29:38it takes to execute.
- 29:39So if it's basically,
- 29:41if the operation
- 29:42that it's doing
- 29:43is really tiny,
- 29:44then after doing
- 29:46that operation,
- 29:46it's got to go
- 29:47all the way back
- 29:47up to the top
- 29:48of the loop again,
- 29:49look at the next
- 29:50barcode operation,
- 29:52and then go and run
- 29:53this,
- 29:54you know,
- 29:55call this thing,
- 29:56which runs the operation,
- 29:57which takes again
- 29:58like nanoseconds
- 29:59to finish,
- 29:59and then it goes
- 30:00all the way back
- 30:00around again.
- 30:01So I guess the analogy
- 30:02I was trying to think
- 30:03of with the nbody problem
- 30:04is,
- 30:05you know,
- 30:06if you were a plumber
- 30:07and you got called out
- 30:08to do a load of jobs
- 30:10in a week,
- 30:11but every single job
- 30:12was,
- 30:13can you change
- 30:14this one washer
- 30:15on a tap for me,
- 30:16which takes you like
- 30:17two minutes to finish,
- 30:19but you get a hundred
- 30:21of those jobs
- 30:21in a day,
- 30:22you're going to spend
- 30:23most of your day
- 30:24just driving around
- 30:25and not actually doing
- 30:25any plumbing.
- 30:26You're going to be
- 30:28driving from house
- 30:28to house
- 30:29and then doing
- 30:29these like two
- 30:30minute jobs
- 30:31and then driving
- 30:32on to the next job.
- 30:33So I think the nbody
- 30:35problem,
- 30:35that's kind of an
- 30:36example of that
- 30:37is that the evaluation
- 30:39loop can't make
- 30:40decisions,
- 30:40like it can't say,
- 30:41oh,
- 30:41if I'm going to do
- 30:42the same operation
- 30:43again and again
- 30:43and again,
- 30:44instead of going
- 30:46around the loop
- 30:46each time,
- 30:47maybe I should just
- 30:49call that operation
- 30:50the number of times
- 30:52that I need to.
- 30:53and those are the
- 30:53kind of optimizations
- 30:54that a JIT would do
- 30:55because it kind of
- 30:56changes the compilation
- 30:57order in sequence.
- 30:59So that's,
- 31:00I guess like we could
- 31:01talk about there are
- 31:02JITs available for
- 31:03Python.
- 31:04Yes.
- 31:05CPython doesn't have,
- 31:06CPython doesn't use a JIT,
- 31:08but for things like the
- 31:10nbody problem,
- 31:11instead of the,
- 31:12you know,
- 31:13the plumber driving to
- 31:14every house and doing
- 31:15this two minute job,
- 31:16why can't somebody
- 31:18actually just go and,
- 31:19why can't everyone
- 31:20just send their tap
- 31:21to like the factory
- 31:23and he just sits in
- 31:23the factory all day
- 31:24replacing the washers.
- 31:26Like Netflix of taps
- 31:27or something,
- 31:28yeah.
- 31:28Back when they sent
- 31:30out DVDs.
- 31:31Maybe I was stretching
- 31:32the analogy a bit,
- 31:33but,
- 31:33you know,
- 31:34basically like you can
- 31:35make optimizations
- 31:36if you know you're
- 31:37going to do the same
- 31:38job again and again
- 31:39and again,
- 31:39or maybe like he just
- 31:41brings all the
- 31:42washers with him
- 31:43instead of driving
- 31:44back to the warehouse
- 31:45each time.
- 31:45So,
- 31:46like there's
- 31:46optimizations you can
- 31:47make if you know
- 31:48what's coming.
- 31:49But because the
- 31:50CPython application
- 31:52was compiled ahead of
- 31:53time,
- 31:53it doesn't know
- 31:54what's coming.
- 31:55There are some opcodes
- 31:56that are coupled
- 31:57together,
- 31:58but there's only a few
- 32:00like which ones
- 32:01they are off the top
- 32:01of my head,
- 32:02but there's only a
- 32:03couple and it doesn't
- 32:04really add a huge
- 32:04performance increase.
- 32:05Yeah,
- 32:06there have been some
- 32:06improvements around
- 32:07like bound method
- 32:08execution time and
- 32:10methods without keyword
- 32:11arguments or some
- 32:12something along those
- 32:13lines that got quite a
- 32:14bit faster.
- 32:14But that's still just
- 32:16like how can we make
- 32:17this operation faster?
- 32:18Not how can we say
- 32:19like,
- 32:19you know what,
- 32:19we don't need a
- 32:20function,
- 32:20let's inline that.
- 32:21It's called in one
- 32:21place once,
- 32:22just inline it,
- 32:23right?
- 32:23Things like that.
- 32:24This portion of
- 32:27Talk Python To Me is
- 32:28brought to you by
- 32:28Sentry.
- 32:29How would you like to
- 32:30remove a little
- 32:31stress from your
- 32:31life?
- 32:32Do you worry that
- 32:33users may be having
- 32:34difficulties or are
- 32:35encountering errors
- 32:36with your app right
- 32:37now?
- 32:37Would you even know
- 32:38it until they send
- 32:39that support email?
- 32:40How much better would
- 32:42it be to have the
- 32:42error details immediately
- 32:44sent to you,
- 32:44including the call
- 32:46stack and values of
- 32:47local variables,
- 32:48as well as the
- 32:48active user stored in
- 32:50the report?
- 32:51With Sentry, this is
- 32:52not only possible,
- 32:53it's simple and free.
- 32:54In fact, we use Sentry
- 32:56on all the Talk Python
- 32:57web properties.
- 32:58We've actually fixed a
- 32:59bug triggered by our
- 33:00user and had the
- 33:01upgrade ready to roll
- 33:03out as we got the
- 33:04support email.
- 33:04That was a great email
- 33:06to write back.
- 33:06We saw your error and
- 33:07have already rolled out
- 33:08the fix.
- 33:09Imagine their surprise.
- 33:10Surprise and delight
- 33:11your users today.
- 33:12Create your free account
- 33:14at talkpython.fm
- 33:15slash Sentry and
- 33:17track up to 5,000
- 33:18errors a month across
- 33:19multiple projects for
- 33:20free.
- 33:20So you did say there
- 33:23were some.
- 33:23There was Pigeon,
- 33:25there's PyPy,
- 33:27there's Unladen
- 33:29Swallow, there's
- 33:31some other options as
- 33:33well, but those are the
- 33:34JITs that are coming to
- 33:35mind.
- 33:35Piston, all of those
- 33:36were attempts and I
- 33:37have not heard anything
- 33:38about any of them for a
- 33:39year, so that's
- 33:40probably not a super
- 33:41sign for their
- 33:42adoption.
- 33:43Yeah, so the ones I
- 33:44kind of picked on
- 33:45because I think they've
- 33:45got a lot of promise
- 33:46and kind of show a big
- 33:48performance improvement
- 33:49is PyPy, which
- 33:51shouldn't be new.
- 33:52I mean, it's a popular
- 33:53project, but PyPy uses
- 33:55a...
- 33:55PY, PY, because some
- 33:56people say like Python
- 33:58package inject, they
- 33:59also call it PyPy, but
- 34:00that's a totally different
- 34:00thing.
- 34:01Yeah, so PyPy...
- 34:02Just for listeners who
- 34:03aren't sure.
- 34:03PyPy kind of helped
- 34:06solve the argument for
- 34:07my talk actually, because
- 34:08if Python is slow, then
- 34:10writing a Python
- 34:11compiler in Python should
- 34:12be like really, really
- 34:13slow.
- 34:14But actually, PyPy, which
- 34:15is a Python compiler
- 34:16written in Python, in
- 34:19problems like the n-body
- 34:21problem, where you're
- 34:23doing the same thing
- 34:23again and again, it's
- 34:25actually really good at
- 34:26it.
- 34:26Like, it's significantly...
- 34:28It's 700-something
- 34:29percent faster than C
- 34:32Python at doing the
- 34:33same algorithm.
- 34:33Like, if you copy and
- 34:35paste the same code and
- 34:36run it in PyPy versus
- 34:38CPython, yeah, it will
- 34:40run over seven times
- 34:41faster in PyPy, and
- 34:43PyPy is written in
- 34:44Python.
- 34:44So it's an alternative
- 34:46Python interpreter that's
- 34:48written purely in
- 34:49Python.
- 34:49But it has a JIT
- 34:50compiler.
- 34:51That's probably the big
- 34:52difference.
- 34:52Yeah.
- 34:52As far as I understand it,
- 34:54PyPy is kind of like a
- 34:55half JIT compiler.
- 34:57It's not like a full JIT
- 34:58compiler like, say, C
- 35:00Sharp or Java, in that it
- 35:02will, like, run on C
- 35:04Python and then, like,
- 35:05decide to JIT compile the
- 35:07stuff that's run a lot.
- 35:08I feel like that's the
- 35:09case.
- 35:09PyPy is a pure JIT
- 35:11compiler, and then
- 35:12number is a, you can
- 35:14basically choose to JIT
- 35:16certain parts of your
- 35:17code.
- 35:17So with number, you can
- 35:19use a, actually, a
- 35:21decorator, and you can
- 35:22stick it on.
- 35:22An at JIT.
- 35:23Yeah, it literally is
- 35:24that.
- 35:25You can do an at JIT on a
- 35:27function, and it will JIT
- 35:28compile that function for
- 35:30you.
- 35:30So if there's a piece of
- 35:31your code which would
- 35:33work better if it were
- 35:34JITed, like it would be
- 35:35faster, then you can just
- 35:37stick a JIT decorator on
- 35:39that using the number
- 35:40package.
- 35:41Yeah, that's really cool.
- 35:42Do you have to, how do
- 35:43you run it?
- 35:44I've got some function
- 35:45within a larger Python
- 35:46program, and I put an at
- 35:47JIT on it.
- 35:48Like, how do I make it
- 35:50actually JIT that and,
- 35:51like, execute?
- 35:52Can I still type Python
- 35:53space, I think, or what
- 35:55happens?
- 35:56I don't know.
- 35:56Do you know?
- 35:57Yeah, I'm just wondering,
- 35:58like, it probably is the
- 35:59library that, as it pulls
- 36:02in what it's going to give
- 36:03you back, you know, the
- 36:04wrapper, the decorator, the
- 36:05function, it probably does
- 36:06JIT.
- 36:07So interesting.
- 36:07I think that's a really
- 36:09good option.
- 36:09Of all the options,
- 36:10honestly, I haven't done
- 36:11anything with Numba, but
- 36:12it looks like probably the
- 36:13best option.
- 36:13It sounds a little bit
- 36:15similar to Cython, but
- 36:16Cython's kind of the
- 36:17upfront style, right?
- 36:20Like, we're going to
- 36:20pre-compile this Python
- 36:22code to see, whereas
- 36:23Numba, it sounds more, a
- 36:25little more runtime.
- 36:26Yeah, so Cython is not
- 36:29really a JIT or a JIT
- 36:30optimizer.
- 36:31It's a way of decorating
- 36:34your Python code with
- 36:36type annotations and
- 36:39using, like, a sort of
- 36:40slightly different syntax
- 36:41to say, oh, this
- 36:43variable is this type,
- 36:45and then Cython will
- 36:47actually compile that
- 36:48into a C extension
- 36:49module, and then you run
- 36:51it from CPython.
- 36:51So it basically, like,
- 36:53compiles your Python into
- 36:55C and then loads it as a
- 36:57C extension module, which
- 36:58can make a massive
- 36:59performance improvement.
- 37:01Yeah, so you've got to
- 37:01run a, like, a set of
- 37:03py build command to
- 37:05generate the libraries, the
- 37:07.o files, or whatever the
- 37:10platform generates, and
- 37:11then those get loaded in.
- 37:13Even if you change the
- 37:14Python code that was their
- 37:15source, you've got to
- 37:16recompile them, or it's
- 37:18just still the same old
- 37:19compiled stuff, same old
- 37:20binaries, yeah.
- 37:21You can automate that so
- 37:22you don't have to type it
- 37:23by hand, but I think
- 37:24Cython is a really good
- 37:26solution for speeding it
- 37:27up.
- 37:28But as I kind of
- 37:29pointed out in my talk,
- 37:30it doesn't answer the
- 37:31question of why Python is
- 37:32slow.
- 37:33It says, well, Python can
- 37:35be faster if you do C
- 37:36instead.
- 37:37Yeah.
- 37:37One thing I do like about
- 37:39Cython these days is they've
- 37:40adopted the type hints,
- 37:42type annotation format.
- 37:44So if you have, what is
- 37:47that, Python 3, 4, or
- 37:49later type annotations, you
- 37:51got to be explicit on
- 37:52everything.
- 37:53But if you have those,
- 37:54that's all you have to do
- 37:56to turn it into like
- 37:58official Cython, which is
- 38:00nice because it used to be
- 38:00you'd have to have like a
- 38:02C type or Cython type dot
- 38:04int rather than a, you
- 38:06know, colon int or
- 38:07something funky like
- 38:08that.
- 38:08Yeah.
- 38:08And it's nice that they
- 38:09brought the two things
- 38:10together.
- 38:10Cython like had type
- 38:12annotations before the
- 38:13language did, I think.
- 38:14Right.
- 38:14Yeah.
- 38:15So they had their own
- 38:15special way.
- 38:16They had their own special
- 38:17little sub language that
- 38:18was Python-esque, but not
- 38:20quite.
- 38:20So I was looking at this
- 38:22nbody problem and I
- 38:22thought, all right, well, I
- 38:24probably should have played
- 38:25with Numba, but I have a
- 38:26little more experience with
- 38:27Cython.
- 38:27So let me just see, like
- 38:29the code is not that hard
- 38:30and I'm going in terms of
- 38:32like how much code is there
- 38:33or whatever.
- 38:34Sure.
- 38:34The math is hard, but the
- 38:35actual execution of it
- 38:37isn't.
- 38:37So I'll link to the actual
- 38:39Python source code for the
- 38:40nbody problem.
- 38:41And I ran it.
- 38:42It has some defaults that
- 38:43are much smaller than the
- 38:44one you're talking about.
- 38:45So if you run it, just
- 38:46hit run.
- 38:46It'll run for like two on
- 38:48my machine.
- 38:48It ran for 213
- 38:50milliseconds just in pure
- 38:51CPython.
- 38:52So I said, all right, well,
- 38:53what if I just grab that
- 38:54code and I just plunk it
- 38:56into a PYC file unchanged.
- 38:59I didn't change anything.
- 39:00I just moved it over.
- 39:01I got it to go into 90
- 39:03milliseconds, which is like
- 39:052.34 times faster.
- 39:06And then I did the type
- 39:08hints that I told you
- 39:09about.
- 39:09Because if you don't put
- 39:11the type hints, it'll
- 39:11still run, but it will
- 39:13work at the, the, the
- 39:14pie object level.
- 39:16Like, so your numbers are
- 39:18pie object numbers, not,
- 39:20you know, ints and
- 39:21floats down.
- 39:22So you make it a little
- 39:23bit faster.
- 39:23So, but I was only able
- 39:24to get it four times
- 39:25faster down to 50
- 39:26milliseconds.
- 39:26Either I was doing it
- 39:28wrong or that's just about
- 39:29as that much faster as I
- 39:30can get it.
- 39:31I could have been missing
- 39:32some types and it was
- 39:32still doing a little more
- 39:33CPython interrupt stuff.
- 39:36But yeah, I don't know.
- 39:37It's, it's an interesting
- 39:38challenge.
- 39:39I guess the last thing to
- 39:40talk about, like on this
- 39:41little bit right here is
- 39:42the, is my PYC.
- 39:44Yeah.
- 39:44I didn't know much about
- 39:45my PYC.
- 39:46I don't know a lot
- 39:47about it either.
- 39:47So my PY is a type
- 39:49checking library and
- 39:51verification library for
- 39:53the type annotations.
- 39:54Right.
- 39:54So if you put these type
- 39:55annotations in there, they
- 39:57don't do anything at runtime.
- 39:57They're just like there to
- 39:59tell you stuff.
- 39:59Right.
- 40:00But things like certain
- 40:02editors can partially check
- 40:03them or my PY can like
- 40:05follow the entire chain and
- 40:07say this code looks like
- 40:08it's typewise hanging
- 40:09together.
- 40:10Not like a pure five
- 40:12levels.
- 40:12pass an integer and you
- 40:13expect a string.
- 40:14So it's broken.
- 40:15Right.
- 40:15It can check that.
- 40:16So they added this thing
- 40:17called my PYC, which can
- 40:19take stuff that is
- 40:20annotated in a way that
- 40:22my PY works with, which
- 40:23is basically type
- 40:24annotations, but more.
- 40:25And they can compile that
- 40:27to see as well, which
- 40:28they also interestingly got
- 40:30like a four times speed
- 40:31up with stuff, not in
- 40:32the embody problem, but
- 40:33on my PY.
- 40:34So I don't know.
- 40:34It's, there's a lot of
- 40:36options, but as you point
- 40:38out, they are a little
- 40:39bit dodging Python.
- 40:41The number stuff is cool
- 40:42because I think you
- 40:44don't really write
- 40:45different code.
- 40:45Do you?
- 40:46Yeah, it's been more
- 40:47natural.
- 40:47And I think PYPY, like
- 40:50you're saying you kind of
- 40:51got two to four times
- 40:53improvement by moving
- 40:54things to Cython.
- 40:55And it took a decent
- 40:55amount of work, right?
- 40:56Because every loop
- 40:57variable had to be
- 40:58declared somewhere else
- 40:59because you can't set the
- 41:00type or the type
- 41:01annotation inside the
- 41:02loop declaration, right?
- 41:03Like it wasn't just
- 41:04put a colon in.
- 41:05I had to do like a
- 41:06decent amount of work to
- 41:07drag out the types.
- 41:08Yeah.
- 41:08And whereas PYPY will
- 41:10be a seven times
- 41:11improvement in speed,
- 41:12for that problem.
- 41:13Yeah.
- 41:14And there's no C compilation.
- 41:15Yes.
- 41:16That's really nice.
- 41:17That's really nice.
- 41:18So we talked about JITs
- 41:20and JITs are pretty
- 41:21interesting.
- 41:21To me, I feel like
- 41:23JITs often go together
- 41:24with garbage collection
- 41:26in the entirely
- 41:28unmanaged sort of
- 41:30non-deterministic sense
- 41:31of garbage collection,
- 41:32right?
- 41:33Not reference counting,
- 41:34but sort of the
- 41:35mark and sweep style.
- 41:37So Python,
- 41:38I mean,
- 41:38maybe we could talk
- 41:39about GC
- 41:40at Python first
- 41:41and then
- 41:41if there's any way
- 41:43to like change that
- 41:44or advantages there,
- 41:45disadvantages.
- 41:46From the
- 41:47Instagram story
- 41:48that they saw
- 41:50a performance improvement
- 41:51when they turned off GC.
- 41:52Yeah, like
- 41:52we're going to solve
- 41:53the memory problem
- 41:54by just letting it leak.
- 41:55Like literally,
- 41:56we're going to disable
- 41:57garbage collection.
- 41:58Yeah,
- 41:59I think they got like
- 42:00a 12% improvement
- 42:01or something.
- 42:01It was significant.
- 42:02They turned it off
- 42:03and then they just
- 42:03restarted the worker processes
- 42:05every 12 hours
- 42:06or something like that.
- 42:06And it wasn't that bad.
- 42:07The GC itself,
- 42:08like to your,
- 42:10I said there's another
- 42:11problem that I studied
- 42:13which was the
- 42:14binary tree problem.
- 42:15And this particular
- 42:18problem will show you
- 42:19the impact of the
- 42:21garbage collector
- 42:22performance on,
- 42:23like in this particular
- 42:25algorithm,
- 42:25this benchmark,
- 42:26it will show you
- 42:27how much your GC
- 42:28slows down the program.
- 42:30And again,
- 42:31I wanted to compare
- 42:32Node with Python
- 42:33because they both have
- 42:34both reference
- 42:35counting and
- 42:35garbage collection.
- 42:36So the garbage
- 42:39collector with Node
- 42:40is a bit different
- 42:40in terms of its design,
- 42:42but both of them
- 42:43are a stop everything
- 42:44garbage collector.
- 42:45So,
- 42:45you know,
- 42:46CPython has a
- 42:47main thread,
- 42:49basically,
- 42:49and the garbage
- 42:51collector will run
- 42:52on the main thread
- 42:53and it will run
- 42:54every number of
- 42:55operations.
- 42:55So,
- 42:56I think the,
- 42:57I can't remember what
- 42:57the default is,
- 42:58it's like 3,000 or
- 42:59something.
- 42:59Every 3,000
- 43:00operations in the
- 43:01first generation
- 43:02where an object
- 43:04has been assigned
- 43:05or deassigned,
- 43:05then it will run
- 43:06the garbage collector,
- 43:07which goes and
- 43:08inspects every,
- 43:09every list,
- 43:10every dictionary,
- 43:11every,
- 43:12what other types,
- 43:13like custom objects,
- 43:14and sees if they have
- 43:16any circular references.
- 43:17Right,
- 43:17and the reason we need
- 43:18the GC,
- 43:19which does this,
- 43:20is because it's not
- 43:21even the main
- 43:22memory management system,
- 43:23because if it was,
- 43:24Instagram would not
- 43:26at all be able
- 43:26to get away
- 43:27with that trick.
- 43:27Right,
- 43:28this is like a,
- 43:29a final net
- 43:30to catch the stuff
- 43:32that reference
- 43:33counting doesn't
- 43:33work.
- 43:33Normally,
- 43:34like if there's
- 43:34some references
- 43:35to an object,
- 43:35once things stop
- 43:37pointing at it,
- 43:38the last one that
- 43:39goes,
- 43:40it just poof,
- 43:40it disappears.
- 43:41But the challenge
- 43:42of reference
- 43:42counting garbage
- 43:43collection is if
- 43:45you've got like
- 43:46some kind of
- 43:47relationship where
- 43:48one thing points
- 43:48at the other,
- 43:49but that thing
- 43:49also points back
- 43:50to itself,
- 43:51right,
- 43:51like a couple
- 43:52object,
- 43:53right,
- 43:54a person object
- 43:54with a spouse
- 43:55pointer or something
- 43:56like that,
- 43:57right?
- 43:57If you're married,
- 43:58you're going to leak.
- 43:58Yeah,
- 43:58absolutely.
- 43:59So this is the thing
- 44:00you're talking about,
- 44:00those types of things
- 44:01that's addressing.
- 44:02And it's kind of
- 44:02designed on the assumption
- 44:03that most objects
- 44:05in CPython
- 44:06have very short
- 44:08lifespans.
- 44:09So,
- 44:10you know,
- 44:10they get created
- 44:11and then they get
- 44:11destroyed shortly
- 44:12afterwards.
- 44:13So like local
- 44:13variables inside
- 44:14functions or,
- 44:15you know,
- 44:16like local variables
- 44:17inside list
- 44:18comprehensions,
- 44:19for example,
- 44:19like those can be
- 44:21destroyed pretty much
- 44:22straight away.
- 44:22But the garbage
- 44:23collective will stop
- 44:24everything running
- 44:25on the main thread
- 44:26while it's running
- 44:26because it has to
- 44:28because you can't,
- 44:29you know,
- 44:29if it's deleting stuff
- 44:30and there's something
- 44:31else running at the
- 44:32same time that's
- 44:33expecting that thing
- 44:34to exist,
- 44:34it's going to cause
- 44:35all sorts of problems.
- 44:36So yeah,
- 44:37the GC will kind of
- 44:38slow down your
- 44:39application if it gets
- 44:40hit a lot.
- 44:41And the binary tree
- 44:42problem will basically
- 44:44construct a series of
- 44:46trees and then loop
- 44:47through them and then
- 44:48delete the nodes and
- 44:49the branches,
- 44:49which kind of triggers
- 44:51the GC to run a lot.
- 44:53And then you can
- 44:53compare the performance
- 44:55of the garbage
- 44:56collectors.
- 44:56So one thing I kind of
- 44:58noted in the design is
- 45:00that they stop
- 45:01everything.
- 45:02If the time it takes to
- 45:03run the garbage
- 45:04collector could be as
- 45:05short as possible,
- 45:06then the performance
- 45:07hit of running it is
- 45:08going to be smaller.
- 45:08And something that Node
- 45:09does is it runs a
- 45:11multi-threaded mark
- 45:13process.
- 45:13So when it actually goes
- 45:14and looks for circular
- 45:16references,
- 45:16it actually starts
- 45:18looking before it
- 45:20stops the main thread
- 45:21on different helper
- 45:23threads.
- 45:23So it starts separate
- 45:25threads and starts the
- 45:26mark process.
- 45:26And then it still stops
- 45:28everything on the main
- 45:29process, but it's kind
- 45:30of prepared all its
- 45:31homework ahead of time.
- 45:32It's already figured
- 45:33out what is garbage
- 45:34before it stops stuff.
- 45:36And it's like, now we
- 45:37just have to stop what
- 45:38we throw it away and
- 45:39update the pointers and
- 45:41then you can carry on,
- 45:42right?
- 45:42Because it's got to,
- 45:43you know, balance the
- 45:44memory and stop
- 45:45allocation and whatnot.
- 45:45Yeah.
- 45:46So I think technically
- 45:47that's possible in CPython.
- 45:49I don't think it has
- 45:50anything to do with
- 45:51the GIL either, like
- 45:52why that couldn't be
- 45:53done.
- 45:53You could still do...
- 45:55Right.
- 45:55It seems like it
- 45:55totally could be done.
- 45:56Yeah.
- 45:56Yeah.
- 45:57Because marking and
- 45:58finding circular
- 45:58references could be done
- 46:00outside of the gill
- 46:01because it's a C-level
- 46:01call.
- 46:02It's not an opcode.
- 46:04But like I say in the
- 46:06talk, you know, all this
- 46:08stuff that I've listed
- 46:09so far is a lot of
- 46:11work and it's a lot of
- 46:12engineering work that
- 46:13needs to go into it.
- 46:14And if you actually look
- 46:15at the CPython compiler,
- 46:17like the CEval, and look
- 46:20at the number of
- 46:20people who've worked
- 46:21on or contributed to
- 46:22it, it's less than 10
- 46:24like to the core
- 46:26component.
- 46:27I wouldn't want to
- 46:28touch it.
- 46:28I would not want to
- 46:29get in there and be
- 46:30responsible for that
- 46:31part of it.
- 46:31No way.
- 46:31Yeah.
- 46:33And at this stage,
- 46:34they're minor
- 46:34optimizations.
- 46:35They're not sort of
- 46:36big overhauls because
- 46:38there just isn't the
- 46:39people to do it.
- 46:41Yeah.
- 46:41You made a point in
- 46:42your PyCon talk that,
- 46:44you know, the reason
- 46:45that V8 got to be so
- 46:47optimized so fast is
- 46:48because it's got, you
- 46:50know, tens of millions
- 46:51of dollars of
- 46:51engineering put against
- 46:53it yearly.
- 46:54Right?
- 46:55I mean, it's kind of
- 46:57part of the browser
- 46:57wars.
- 46:58The new browser wars
- 47:00a bit.
- 47:00Yeah.
- 47:00From what I could
- 47:01work out, there's at
- 47:02least 35 permanent
- 47:04developers working on
- 47:05it.
- 47:05Just looking at the
- 47:06GitHub project, like if
- 47:07you just see the
- 47:09commit histories, like
- 47:10nine to five, Monday
- 47:11to Friday, 35 advanced
- 47:14C++ developers hacking
- 47:16away at it.
- 47:16Right.
- 47:16If we had that many
- 47:17people continuously
- 47:18working on CPython's
- 47:20like internals and
- 47:22garbage collection and
- 47:23stuff, we'd have more
- 47:24optimizations or bigger
- 47:25projects that people
- 47:26will try to take on
- 47:26probably.
- 47:27Yeah, absolutely.
- 47:27And the people who
- 47:28work on it at the
- 47:29moment, all of them
- 47:30have day jobs and this
- 47:32is not typically their
- 47:33day job.
- 47:33Like they managed,
- 47:34they've convinced their
- 47:35employer to let them do
- 47:36it in their spare time
- 47:38or, you know, one or
- 47:39two days a week, for
- 47:39example, and they're
- 47:41finding the time to do
- 47:42it.
- 47:42And it's a community
- 47:43run project.
- 47:44it's an open source
- 47:44project.
- 47:45But I think kind of
- 47:46going back to places
- 47:48where Python could be
- 47:50faster, like these kind
- 47:51of optimizations in
- 47:53terms of engineering,
- 47:54they're expensive
- 47:55optimizations.
- 47:56They cost a lot of
- 47:58money because they need
- 47:58a lot of engineering
- 47:59expertise and a lot of
- 48:01engineering time.
- 48:02And I think as a
- 48:03project at the moment,
- 48:04we don't really have
- 48:05that luxury.
- 48:06So it's not really fair
- 48:08of me to complain about
- 48:10it if I'm not
- 48:11contributing to the
- 48:12solution.
- 48:12Yeah, but you have a
- 48:13day job as well, right?
- 48:14But I have a day job and
- 48:15this is not day job.
- 48:16So yeah, I think there's,
- 48:18I think for what we use
- 48:20Python for most of the
- 48:21time, it's definitely
- 48:22fast enough.
- 48:23And in places where it
- 48:24could have optimizations
- 48:25like the ones that we
- 48:27talked about, those
- 48:28optimizations have
- 48:29drawbacks because, you
- 48:31know, adding a JIT,
- 48:32for example, means that it
- 48:34uses a lot more
- 48:35memory.
- 48:35like the Node.js
- 48:36example, the n-body
- 48:38problem, sure, it
- 48:39finishes it faster, but
- 48:40uses about five times
- 48:41more RAM to do it.
- 48:42Right.
- 48:43And PyPy uses more
- 48:44memory, like the JIT
- 48:46compiler, and also the
- 48:47startup time of the
- 48:48process is typically a
- 48:50lot longer.
- 48:50If anyone's ever tried
- 48:52to boot Java JVM
- 48:54cold, you know, like
- 48:55the startup time for
- 48:57JVM is pretty slow.
- 48:58.NET's the same, like
- 49:00the initial boot time
- 49:01for it to actually get
- 49:02started and warm up
- 49:03is time consuming.
- 49:05So you wouldn't use
- 49:06it as a, like a
- 49:07command line tool to
- 49:09write a simple script
- 49:10that you'd expect to
- 49:11finish in, you know,
- 49:12under 100 milliseconds.
- 49:13I think that that kind
- 49:14of highlights one of the
- 49:15challenges, right?
- 49:16It's if you thought your
- 49:18process was just going to
- 49:19start and be a web
- 49:20server or a desktop
- 49:21application, two
- 49:23seconds start of time
- 49:24is fine, or whatever
- 49:25that number is.
- 49:26But if it's solving this
- 49:27general problem, yeah, it
- 49:29could be running Flask as
- 49:30a microservice, or it
- 49:31could be, you know,
- 49:33replacing Bash, right?
- 49:35Like these are very
- 49:35different constraints and
- 49:37interests, right?
- 49:38Yeah.
- 49:38And there aren't really
- 49:39many other languages where
- 49:40there is one sort of
- 49:43language definition and
- 49:44there are multiple
- 49:45mature implementations of
- 49:47it.
- 49:47So, you know, with
- 49:49Python, you know, you've
- 49:50got Cython, you've got
- 49:52PyPy, you've got
- 49:53Numba, you've got
- 49:54LionPython.
- 49:55I mean, there's like a
- 49:56whole list of, you know,
- 49:59different, Jython, like
- 50:00different implementations
- 50:02of the language.
- 50:02And people can choose
- 50:04the, I guess, kind of
- 50:05pick which one is best for
- 50:07the problem that they're
- 50:08trying to solve, but use
- 50:09the same language across
- 50:10them.
- 50:10Whereas you don't really
- 50:11have that luxury with
- 50:12others.
- 50:12You know, if you're
- 50:13writing Java, then you're
- 50:14using JVM.
- 50:15There are, I mean, there's
- 50:16two implementations.
- 50:17It's the free one and
- 50:18the licensed one, but
- 50:20like that's pretty much
- 50:22as far as it goes.
- 50:22That's not exactly the
- 50:24same trade-off.
- 50:25Yeah.
- 50:25It's optimizing for
- 50:26money.
- 50:26That's not optimizing for
- 50:28performers or whatever
- 50:29necessarily.
- 50:30So one thing that I feel
- 50:32like comes around and
- 50:33around again in this
- 50:34discussion, and I'm
- 50:36thinking mostly of like
- 50:37PyPy and some of these
- 50:38other attempts people
- 50:39have made to add like
- 50:41JIT compilation to the
- 50:42language or other
- 50:43changes.
- 50:43It's always come back,
- 50:45it seems like, to
- 50:47well, it would be
- 50:48great to have these
- 50:49features.
- 50:49Oh yeah, but there's
- 50:51this thing called the
- 50:51C API.
- 50:52And so no, we can't
- 50:54change the GIL.
- 50:54No, we can't change
- 50:55memory allocation.
- 50:56No, we can't change any
- 50:57of these other things
- 50:58because of the C API.
- 51:01And so we're stuck.
- 51:02Yeah.
- 51:03I mean, I'm not saying
- 51:05I'm asking you for a
- 51:06solution here.
- 51:07like, I just, it feels
- 51:09like that is both the
- 51:12real value of Python in
- 51:14that like some of the
- 51:15reasons that we can still
- 51:16do insanely computational
- 51:18stuff with Python is
- 51:20because a lot of these
- 51:21libraries where they have
- 51:22these tight loops or
- 51:23these little bits of code
- 51:24deserialization or matrix
- 51:26multiplication or whatever,
- 51:27they've written that in C
- 51:29and then ship that as a
- 51:30wheel.
- 51:31And so now all of a sudden
- 51:32our code is not slow as
- 51:34doing math with Python,
- 51:35as fast as doing math
- 51:36with C.
- 51:37Yeah.
- 51:37I mean, so if you look
- 51:38at a NumPy, for example,
- 51:40if you're doing a lot of
- 51:41math, then you, you know,
- 51:42you could be using the
- 51:43NumPy library, which is
- 51:45largely compiled C code.
- 51:47It's not like you import
- 51:49it from Python and you run
- 51:50it from Python, but the
- 51:51actual implementation is a
- 51:53C extension.
- 51:54And that wouldn't be
- 51:55possible if CPython wasn't
- 51:58built in the way it is,
- 51:59which is that it is a
- 52:00ahead of time extension
- 52:02loader that you can run
- 52:03from Python code.
- 52:04Yeah.
- 52:05One project I do want to
- 52:06give a shout out to, I
- 52:07don't know if it's going to
- 52:08go anywhere.
- 52:08It's got a decent amount of
- 52:09work on it, but it's only
- 52:11got 185 GitHub stars.
- 52:13So take that for what it's
- 52:14worth.
- 52:14This thing called HPY, H-P-Y.
- 52:17Guido Van Rossum called this
- 52:20out on Python Bytes 179 when
- 52:22he was a guest co-host there.
- 52:24And it's an attempt to make a
- 52:27new replacement of the C API
- 52:31for Python, where instead of
- 52:33pass around pointers to
- 52:34objects, you pass basically
- 52:36pointers to pointers, which
- 52:37means that things that move
- 52:39stuff around like compacting
- 52:41garbage collectors or other
- 52:44implementations like JITs have a
- 52:46much better chance to change
- 52:47things without directly breaking
- 52:49the C API.
- 52:50You can change the value of the
- 52:51pointer pointer without, you
- 52:53know, having to reassign that
- 52:55down at that layer.
- 52:56So they specifically call out
- 52:58it's, you know, the current C API
- 53:00makes it hard for things like
- 53:02PyPy and Grail Python and JITon.
- 53:04And the goals are to make it
- 53:06easier to experiment with these
- 53:08ideas, more friendly for other
- 53:10implementations, reference
- 53:12counting, for example, and so on.
- 53:14So anyway, I don't know that's
- 53:15going anywhere, how much traction
- 53:16it has, but it's interesting
- 53:19idea.
- 53:20Yeah, no, I like the idea.
- 53:21And the C API, like, has come a
- 53:24long way, but it's got its
- 53:26quirks.
- 53:26I don't know, there's been a lot
- 53:27of discussions, and there's a lot
- 53:29of draft peps as well, you know,
- 53:31proposing kind of different
- 53:32designs to the C API.
- 53:34Yeah.
- 53:35So we're getting kind of short on
- 53:36time.
- 53:36We've discussed a bunch of stuff.
- 53:38I guess two other things I'd like
- 53:40to cover real quickly.
- 53:41One, we've talked about a lot of
- 53:44stuff in terms of computational
- 53:45things, but understanding memory
- 53:48is also pretty important.
- 53:49And we did just talk about the GC.
- 53:50It's pretty easy in Python to just
- 53:53run C profile and ask what my
- 53:55computational time is.
- 53:57It's less obvious how to
- 53:58understand memory allocation and
- 54:00stuff.
- 54:00And was it you that recommended
- 54:01Austin to me?
- 54:03Yeah.
- 54:04Yeah, so Austin is a super cool
- 54:06profiler, but does both CPU
- 54:08profiling, but also memory
- 54:09allocation profiling and tracing
- 54:12in Python.
- 54:13Do you want to tell people about
- 54:13Austin real quick?
- 54:14Yeah, so Austin is a new
- 54:16profiler written for Python code.
- 54:18It's a sampling profiler, so it
- 54:20won't, like other profilers, it
- 54:22won't slow your code down
- 54:23significantly.
- 54:23It's kind of basically sits on
- 54:26the side, just asking your app,
- 54:27you know, what it's doing as a
- 54:30sample.
- 54:30And then it will give you a whole
- 54:32bunch of visuals to let you see,
- 54:35like flame graphs, for example,
- 54:36like what's being called, what's
- 54:38taking a long time, which functions
- 54:40are chewing up your CPU, like
- 54:42which ones are causing the
- 54:43bottlenecks and then which ones are
- 54:45consuming a lot of memory.
- 54:46So if you've got a, you know, a
- 54:48piece of code that is, it is slow,
- 54:51the first thing you should probably
- 54:52do is just stick it through a
- 54:53profiler and see if there is a
- 54:56reason why, like if there is
- 54:57something that you could either
- 54:58optimize or, you know, you've
- 55:00accidentally done like a nested
- 55:02loop or something and Austin
- 55:05would help you do that.
- 55:06One of the things I thought was
- 55:07super cool about this, like the
- 55:09challenge I have so often with
- 55:10profilers is the startup of
- 55:13whatever I'm trying to do, it just
- 55:14overwhelms like the little thing I'm
- 55:16trying to test.
- 55:18you know, I'm like starting up a web
- 55:20app and initializing database
- 55:21connections and I just want to
- 55:22request a little bit of some
- 55:24paid and it's not that slow, but
- 55:26you know, it's just, I'm seeing all
- 55:28this other stuff around and I'm just
- 55:30like, I just want to focus on this
- 55:31one part of it and they've got all
- 55:33these different user interfaces, like
- 55:35a web user interface in a terminal
- 55:37user interface.
- 55:38They call it two, which is cool.
- 55:39And it gives you like a, like kind
- 55:41of like top or glances or one of
- 55:44these things that tells you right now,
- 55:45here's what the profile for the
- 55:47last five seconds looks like.
- 55:48And it gives you the call stack and
- 55:51breakdown of your code right now for
- 55:53like that five second segment, like
- 55:55updating in real time.
- 55:56That's super cool.
- 55:56Yeah.
- 55:57So if you want to run something and
- 55:58then just see what it's doing or you
- 56:00want to replay it.
- 56:01Why is it using a lot of CPU now?
- 56:02Yeah.
- 56:03Yeah.
- 56:03Yeah.
- 56:03That's, I really like that.
- 56:04That's super cool.
- 56:05All right.
- 56:06Also, you know, concurrency is
- 56:07something that Python has gotten a
- 56:09bad rap for in terms of slowness.
- 56:11I think with async and await and
- 56:14asyncio, if you're waiting on an
- 56:15external thing, Python can be ultra
- 56:17fast now, right?
- 56:18Like it's acing and awaiting waiting
- 56:21on like database calls, web calls
- 56:23with the right drivers, super fast.
- 56:26But when it comes down to the
- 56:28computational stuff, there's still
- 56:29the GIL and there's really not a
- 56:30great fix for that.
- 56:32I mean, there's multiprocessing,
- 56:33but that's got a lot of overhead.
- 56:35So it's got to make sense, right?
- 56:36Kind of like your plumber analogy,
- 56:38right?
- 56:38You can't do like one line function
- 56:41calls in multiprocessing or, you
- 56:43know, like one line computations.
- 56:45But the work that Eric Snow's doing
- 56:47with subinterpreters looks pretty
- 56:48promising to unlock another layer.
- 56:50Yeah.
- 56:50So it's out in the 3.9 alpha.
- 56:53If you've played with that yet, it's
- 56:55still experimental.
- 56:56So subinterpreters is somewhere in
- 56:59between multiprocessing and
- 57:01threading in terms of the like the
- 57:03implementation.
- 57:04So it will it doesn't spawn.
- 57:06So if you use multiprocessing, I
- 57:08mean, that's basically just saying
- 57:10let's hire another plumber and we'll get
- 57:13them to talk to each other at the beginning
- 57:15of the day and split up the tasks.
- 57:17Whereas subinterpreters, actually,
- 57:19maybe they're sharing the same van.
- 57:20I'm not sure where this analogy is going,
- 57:22but, you know, they use the same process.
- 57:25The subinterpreters share the same Python
- 57:26process.
- 57:27It doesn't spawn up an entirely new process.
- 57:29It doesn't have to load all the modules
- 57:32again.
- 57:33And the subinterpreters can also talk to
- 57:36each other.
- 57:36they can use shared memory to communicate
- 57:39with each other as well.
- 57:40But because they're separate
- 57:42interpreters, then technically they can
- 57:45have their own locks.
- 57:47So the lock that, you know, gets locked
- 57:49whenever you run any opcode is the
- 57:52interpreter lock.
- 57:52And this basically means that you can
- 57:55have two interpreters running in a
- 57:57single process, each with its own lock.
- 57:59So it can be running different
- 58:01operations at the same time.
- 58:03Right.
- 58:04They would automatically run on separate
- 58:05threads.
- 58:06So you're basically running multi-threading
- 58:08and it can also use multi-CPU.
- 58:10That'd be great.
- 58:11Fundamentally, the GIL is not about a
- 58:14threading thing per se.
- 58:16It's about serializing memory access
- 58:19allocation and deallocation.
- 58:21And so with the subinterpreters, the
- 58:24idea is you don't directly share pointers
- 58:26between subinterpreters.
- 58:27There's like a channel type of
- 58:29communication between them.
- 58:30So you don't have to take a lock on
- 58:32one when it's working with objects
- 58:34versus another, like they're entirely
- 58:36different set of objects.
- 58:37They're still in the same process space,
- 58:39but they're not actually sharing
- 58:40pointers.
- 58:40So you don't need to protect each
- 58:41other.
- 58:42Right.
- 58:42You just have to protect within each
- 58:44subinterpreter, which has a
- 58:45possibility to let me use all six of
- 58:47my cores.
- 58:48Yeah, absolutely.
- 58:49You can't read and write from the same
- 58:50local variables for that reason,
- 58:52which you can do in threading.
- 58:54But with subinterpreters, it's kind of
- 58:56like a halfway halfway between just
- 58:58running a separate process.
- 58:59Yeah.
- 58:59It probably formalizes some of the
- 59:02multi-threading communication styles
- 59:04that are going to keep things safer
- 59:05anyway.
- 59:06Definitely.
- 59:07Yeah.
- 59:07All right.
- 59:08Let's talk about one really quick
- 59:09thing before we wrap it up.
- 59:10Just one interesting project that
- 59:12you've been working on.
- 59:13I mentioned that you were on before
- 59:14about some security issues, right?
- 59:16Yeah.
- 59:16I want to tell people about your PyCharm
- 59:18extension that you've been working on.
- 59:19Yeah.
- 59:19So I've been working on a PyCharm
- 59:21extension called Python Security.
- 59:23It's very creatively named.
- 59:25It's available.
- 59:27Take the straightforward.
- 59:28Yeah, exactly.
- 59:29So it's basically like a code checker,
- 59:32but it runs inside PyCharm and it will
- 59:34look for security vulnerabilities that
- 59:37you may have written in your code and
- 59:39underline them for you and in some
- 59:41cases fix them for you as well.
- 59:42So it will say the thing you've done
- 59:44here is really bad because it can cause
- 59:46someone to be able to hack into your
- 59:48code and you can just press the quick
- 59:50fix button and it could fix it for you.
- 59:52So it's got actually over a hundred
- 59:55different inspections now.
- 59:56And also you can run it across...
- 59:58Should I use the YAML.load still?
- 1:00:00Is that good?
- 1:00:00No.
- 1:00:01I think that was like the first checker,
- 1:00:05right?
- 1:00:05Actually, it was the YAML.load.
- 1:00:07Yeah, you can run it across the whole project.
- 1:00:09So you can do a code inspection across your project to like a code audit.
- 1:00:12And also it uses PyCharm's package manager.
- 1:00:15So it will go in and look at all the packages you have installed in your project and it will
- 1:00:21check them against either Snyk, which is a big database of vulnerable Python packages.
- 1:00:28Snyk.io uses their API.
- 1:00:31So it checks it against that or you can check it against like your own list.
- 1:00:36And also it's available as a GitHub action.
- 1:00:39So manage to figure out how to run PyCharm inside Docker so that you can run PyCharm from
- 1:00:46GitHub action.
- 1:00:47Wow.
- 1:00:48Yeah, you can write a CI/CD script in GitHub to just say inspect my code and it will just
- 1:00:55inside GitHub.
- 1:00:56You don't need PyCharm to do it, but it will run the inspection tool against your code repository.
- 1:01:01It just requires that it's open source to be able to do that.
- 1:01:03Okay, that's super cool.
- 1:01:04All right, well, we're definitely out of time, so I have to leave it there.
- 1:01:07Two quick questions.
- 1:01:08Favorite editor, notable package?
- 1:01:10What do you got?
- 1:01:10PyCharm and I don't know about the notable package.
- 1:01:14I don't know.
- 1:01:15Yeah, you've been too far in the C code.
- 1:01:16Yeah, I know.
- 1:01:17I'm like, what are packages?
- 1:01:18I think there's something that does install those, but they don't work down in C.
- 1:01:22Yeah, no, that's cool.
- 1:01:23All right, so people are interested in this.
- 1:01:26They want to maybe understand how CPython works better or how that works and where and why
- 1:01:31it might be slow so they can avoid that.
- 1:01:33Or maybe they even want to contribute.
- 1:01:35What do you say?
- 1:01:35Wait for my book to come out and read the book or read the real Python article, which is free
- 1:01:40and online.
- 1:01:40And it talks through a lot of these concepts.
- 1:01:43Yeah, right on.
- 1:01:43Well, Anthony, thanks for being back on the show.
- 1:01:46Great, as always, to dig into the internals.
- 1:01:48Thanks, Michael.
- 1:01:48Yeah, you bet.
- 1:01:49Bye.
- 1:01:49Bye.
- 1:01:49Bye.
- 1:01:50Bye.
- 1:01:51This has been another episode of Talk Python To Me.
- 1:01:54Our guest on this episode was Anthony Shaw, and it's been brought to you by Brilliant.org
- 1:01:59and Sentry.
- 1:01:59Brilliant.org encourages you to level up your analytical skills and knowledge.
- 1:02:04Visit talkpython.fm/brilliant and get Brilliant Premium to learn something new every
- 1:02:10day.
- 1:02:10Take some stress out of your life.
- 1:02:12Get notified immediately about errors in your web applications with Sentry.
- 1:02:17Just visit talkpython.fm/sentry and get started for free.
- 1:02:21Want to level up your Python?
- 1:02:23If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 1:02:28Or if you're looking for something more advanced, check out our new Async course that digs into
- 1:02:33all the different types of async programming you can do in Python.
- 1:02:36And of course, if you're interested in more than one of these, be sure to check out our
- 1:02:40Everything Bundle.
- 1:02:41It's like a subscription that never expires.
- 1:02:43Be sure to subscribe to the show.
- 1:02:45Open your favorite podcatcher and search for Python.
- 1:02:47We should be right at the top.
- 1:02:48You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:02:53and the direct RSS feed at /rss on talkpython.fm.
- 1:02:57This is your host, Michael Kennedy.
- 1:02:59Thanks so much for listening.
- 1:03:01I really appreciate it.
- 1:03:02Now get out there and write some Python code.
- 1:03:04I'll see you next time.
- 1:03:24Thank you.