Latest / Talk Python To Me / #347: Cinder - Specialized Python that Flies
Transcript
- 0:00The team at Instagram dropped the performance bomb on the Python world when they open-sourced
- 0:04Cinder, their performance-oriented fork of CPython.
- 0:08It contains a number of performance optimizations, including bytecode inline caching, eager evaluation
- 0:14of coroutines, a method-at-a-time JIT, and an experimental bytecode compiler that uses
- 0:20type annotations to emit type-specialized bytecode that performs better in the JIT.
- 0:25While it's not a general-purpose runtime we can all pick up and use, it contains many
- 0:29powerful features and optimizations that make their way back to mainline Python.
- 0:34We welcome Dino Velen to the show to dive into Cinder.
- 0:37This is Talk Python To Me, episode 347, recorded November 29th, 2021.
- 0:57Welcome to Talk Python To Me, a weekly podcast on Python.
- 1:00This is your host, Michael Kennedy.
- 1:02Follow me on Twitter where I'm @mkennedy, and keep up with the show and listen to past
- 1:06episodes at talkpython.fm.
- 1:08And follow the show on Twitter via at Talk Python.
- 1:11We've started streaming most of our episodes live on YouTube.
- 1:14Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming
- 1:20shows and be part of that episode.
- 1:23This episode is brought to you by Sentry and TopTal.
- 1:26Please check out what they're offering during their segments.
- 1:28It really helps support the show.
- 1:30Dino, welcome to Talk Python To Me.
- 1:33Hi, Michael.
- 1:34Thanks for having me.
- 1:34I'm really excited to talk to you.
- 1:36You've been involved in a lot of projects that I've wanted to talk to you about over the years
- 1:41and haven't yet.
- 1:42So we're going to touch on a couple of those.
- 1:44But we've got some really big news around Sender and some performance stuff that you all over at
- 1:51Instagram are doing to try to make Python faster.
- 1:53You did a really cool Python keynote, or not keynote, but talk on that.
- 1:56So we're going to dive deep into this alternate reality runtime of Cpython called Sender that you
- 2:03all have created.
- 2:04That's going to be a lot of fun.
- 2:05Yeah.
- 2:05And it's a slightly alternate reality.
- 2:07It's not that much of an alternate reality.
- 2:09Just a little bit.
- 2:10Before we do, though, let's just hear your story.
- 2:13How'd you get into programming in Python?
- 2:15I started programming when I was a teenager.
- 2:18I got into computers initially really through BBSs.
- 2:22Oh, yes.
- 2:23Maybe.
- 2:24Was this pre-internet?
- 2:25This was pre- this is like dial up only.
- 2:28You would dial into the BBS?
- 2:30Oh, my gosh.
- 2:31Yeah.
- 2:31Like, you know, I had a modem.
- 2:33Someone else had a modem sitting in their home waiting for people to call in.
- 2:37You'd log in, send emails.
- 2:39Post messages.
- 2:40Take your turns on games.
- 2:43Log out.
- 2:44And someone else could log in and respond to your emails.
- 2:47It was so amazing.
- 2:48And send email meant wait for another BBS to dial in to connect to that one to like sync
- 2:54its local batch of emails.
- 2:55It was like peer-to-peer emails.
- 2:57So weird.
- 2:58Yeah.
- 2:58I mean, or like there's a lot of local emails, right?
- 3:01Where it's just on.
- 3:02Right.
- 3:02You're waiting for the other person to have a chance to log in.
- 3:05But yeah, there's also that network, like a couple of different big networks.
- 3:10It was such a different time.
- 3:12It was such a different time.
- 3:13I was not super into this as much.
- 3:16My brother was really into it.
- 3:18We had two phone lines so that we could do more of this.
- 3:21Did you ever play Trade Wars or any of the games that were on there?
- 3:24Trade Wars was awesome.
- 3:25It was so good.
- 3:27I think I would still enjoy Trade Wars.
- 3:28It was so good.
- 3:29I was still playing Trade Wars in college.
- 3:31We like formed teams and like we're trying to take over some Trade Wars that was available
- 3:37over the internet, actually, that you could like Telnet into and play.
- 3:41And a lot of this BBS stuff had sort of found a home over Telnet for a while, hadn't it?
- 3:47Yeah.
- 3:47Yeah.
- 3:47I think the main BBS software that I used there was used in St. Louis where I grew up,
- 3:54which was World War IV with, I think it's still around and like available for you to
- 4:01like, if you really want to host it on some internet server, but who's going to do that?
- 4:07Incredible.
- 4:08Okay.
- 4:09So how's the BBS story fit into the programming side of things?
- 4:12The BBS software that kind of was really popular, you could get a license to it for 50 bucks
- 4:19and you got a source code for it along with it.
- 4:22And there's a big active modding community.
- 4:25And so, you know, I started off like taking people's mods and applying them and then trying
- 4:30to make my own mods and like just ended up teaching myself C and initially very poorly
- 4:38taught myself C, but then, you know, finally got good at this at some point.
- 4:43How fun.
- 4:44Yeah.
- 4:44Did other people use your mods or were you running your own BBS or anything like that?
- 4:49Where'd this surface?
- 4:50I did a really bad job at running my own BBS.
- 4:53I petitioned my parents for a second phone line, but I also wanted to use it for phone calls.
- 4:59So to call my BBS, we had to like dial in.
- 5:01And then like I had this device where you could punch four extra codes and it would connect you
- 5:06to the modem.
- 5:07So that was kind of annoying and didn't make it the world's most popular BBS.
- 5:11And it was rather short web.
- 5:13Heard some of the automation.
- 5:14Yeah.
- 5:14Yeah.
- 5:15But I've published my mods.
- 5:17My friends ran BBSs.
- 5:19They pick up some of the mods.
- 5:21I don't know that I was the most popular modder out there.
- 5:24I should go and see if I could find them.
- 5:26That might be terrifying though.
- 5:27Yeah, that might be terrifying, but it could also be amazing.
- 5:30Let's wrap up the BBS side of things with putting some bookends on the timeframe here.
- 5:35What was the beginning baud rate and end baud rate of your BBS time?
- 5:394,400 to 57.6K.
- 5:43Yeah.
- 5:44So you took it all the way to the end there, but 2,400 probably meant it didn't require
- 5:48putting the phone on to a device like the war games.
- 5:53Never that bad.
- 5:56Fantastic.
- 5:57All right.
- 5:58How about Python?
- 5:59How'd you get to that?
- 6:00I got into Python in a very weird way because I started working on a Python implementation.
- 6:04Having really never touched or used Python before.
- 6:09Obviously, I'd heard about it and it's kind of like significant white space.
- 6:12That sounds weird.
- 6:13And, but ended up really loving working on it on Iron Python, really loving the language
- 6:21and the way it was designed.
- 6:23It's a very, it gave me a very weird outlook on Python, I think, just because, you know,
- 6:30I knew all sorts of weird corner cases about Python and the language and all the details
- 6:36there.
- 6:37But then didn't really know much about libraries and things like that.
- 6:41And to some extent that continues today, but I get to write a lot more Python code today
- 6:48too.
- 6:48Sure.
- 6:48But like always having been on the implementation side is a little strange.
- 6:53It is strange.
- 6:55And it is, I guess it would be a weird way to get to know the language.
- 6:58So I feel like one of the real big powers of Python is that you can be really effective
- 7:03with it with a super partial understanding.
- 7:06Like you could have literally no idea how to create a function and you could still do useful
- 7:10things with Python.
- 7:11Whereas if you're going to jump in and create Iron Python, which we'll talk about in a second,
- 7:18you have to start out, what are these meta classes and how do I best implement dynamic,
- 7:23you know, dynamic objects and all this stuff.
- 7:26That's like the opposite of starting with a partial understanding.
- 7:29Well, how do imports work?
- 7:33That was a big thing.
- 7:34Yeah, yeah, yeah.
- 7:34I remember when I learned about that, I'm like, wait, this is like running code.
- 7:38It's not like an include file or a statically linked file or, you know, adding a reference
- 7:45in .NET or something like that.
- 7:47It's nope.
- 7:48It just runs whatever's in the script.
- 7:50And it happens to be most of the time it defines behaviors, but it doesn't have to.
- 7:53Yeah.
- 7:53And like, how do you pick what's going to get imported?
- 7:57And yeah, the semantics there are so complicated.
- 8:03Yeah.
- 8:03There are some oddities of Python, but in general, it seems to be working well for people.
- 8:09But I can see as implementing it, it could, you know, you could definitely be pulling some
- 8:13hair out.
- 8:13And I mean, so many things implemented are just, they're super safe.
- 8:17Like they make a lot of sense.
- 8:19There's just some weird corner cases that you run into that are, it's like, what's going on
- 8:26here?
- 8:26Yeah.
- 8:26When I worked on Iron Python, we couldn't look at the source code that's like Python,
- 8:30which made things really interesting.
- 8:32Okay.
- 8:33Because this predates .NET being open source and all that kind of stuff, right?
- 8:38Yeah.
- 8:38And so you don't want to be poisoned, poisoned by the ideas.
- 8:42Yeah.
- 8:43Okay.
- 8:44How interesting.
- 8:44iPhone was open source, but this was when Microsoft was still very much figuring out how they wanted
- 8:51to approach open source.
- 8:53And we're still very cagey about it.
- 8:56It was very interesting.
- 8:57Yeah.
- 8:58They've come a long way and many companies have, I would say.
- 9:01It's still, there's some idiosyncrasies, I guess there, but certainly it's a different
- 9:06time now than it was then.
- 9:07This was like what, 2008, 2009 timeframe-ish or 2005 maybe?
- 9:14Yeah.
- 9:142005, 2006.
- 9:16I think it was around Iron Python 1 out.
- 9:192006 sounds about right.
- 9:21Yeah.
- 9:21So that's a while ago.
- 9:22Yes.
- 9:23It doesn't sound that long ago to me, but honestly, it's a while ago.
- 9:26Yeah.
- 9:27It's like remembering the 90s is not 10 years ago.
- 9:29It's true.
- 9:31It's definitely true.
- 9:33All right.
- 9:34How about day to day?
- 9:35What are you doing now?
- 9:36Your Instagram, right?
- 9:37Basically, I work on our fork of CPython, which we call Sender.
- 9:41And my job is to make, my entire team's job is to make Instagram run more efficiently.
- 9:48I mean, obviously, Instagram is a very large website that has a lot of traffic and it's a
- 9:53very large Django app.
- 9:56So we just spend our time trying to improve CPython and, you know, very specifically trying
- 10:04to improve CPython for Instagram's workload.
- 10:07We're very driven by kind of that as our sole direction.
- 10:12And so it lets us make some interesting decisions and drive some interesting decisions.
- 10:18But it's just really spending the day looking at what we can do to improve performance and
- 10:25going off and implementing that and making Instagram a little bit faster.
- 10:29So when we talk about Python and Django running Instagram, I put up a little post here of
- 10:34something I did yesterday just to have some Instagram stuff to show.
- 10:38Is that talking about the website?
- 10:40Is that the APIs behind the scenes?
- 10:42Like when you say Django runs Instagram, what are we talking about here?
- 10:47So it's the website.
- 10:48It's the APIs.
- 10:49There's obviously some parts that aren't Django, but kind of everything that people's devices
- 10:55are interacting with is going through the Django front end.
- 10:59And there's also a bunch of like, you know, if we have asynchronous processes that need to
- 11:04kick off and run in the background, that's kind of all handled by a Django tier as well.
- 11:09So it's a good chunk of what's going on.
- 11:13Yeah.
- 11:13Nice.
- 11:14This is probably one of the, if not the largest Django deployment there is, right?
- 11:18This is a lot of, a lot of servers we're talking about, right?
- 11:20I would assume so.
- 11:21I don't know.
- 11:22There might be something else pretty big out there.
- 11:25Yeah.
- 11:25I feel like the talk at the 2017 PyCon, remember that when we used to go to places where there
- 11:31are other people and we would go and like be in the same room and stuff.
- 11:33That was so nice.
- 11:34I know.
- 11:35It was so weird.
- 11:36And there was a cool Instagram talk about, I believe that one was about disabling the
- 11:42GC or something like that.
- 11:44And I feel like they said in that talk, at least at that time, that was one of the largest,
- 11:48not the largest Django deployment.
- 11:49Yeah.
- 11:50And we no longer disable the GC.
- 11:52We fixed the memory leak.
- 11:53So that's good.
- 11:54Okay.
- 11:55We're going to talk a lot about memory.
- 11:58And honestly, this whole conversation is going to be a bit of a test, an assessment of my
- 12:05CPython internals.
- 12:07But I think that's okay because a lot of people out there don't know super in-depth
- 12:12details about CPython.
- 12:13And I can play the person who asks the questions for them.
- 12:17Awesome.
- 12:17Awesome.
- 12:17I can try to answer questions.
- 12:19Sure.
- 12:21Well, we'll keep it focused on the part that you've been doing.
- 12:23But during your talk, you mentioned a couple of things.
- 12:27First, you said, okay, well, when we're running over on Django, we're running on, you say UWSGI.
- 12:34I feel like this is a micro.
- 12:36It used to be like a-
- 12:37Micro Whiskey.
- 12:39Yeah.
- 12:39Micro Whiskey.
- 12:40I don't know.
- 12:40UWSGI, Micro Whiskey, whatever it is.
- 12:42Yeah.
- 12:42I feel like all these projects that have interesting names should have a press here to hear how
- 12:46it should be pronounced.
- 12:47It should be WSGI or Whiskey.
- 12:49How do it?
- 12:50Anyway, this micro Whiskey you guys are running on and understanding how it creates child processes
- 12:57and forks out the work is really important for understanding some of the improvements that
- 13:03you've made and some of the areas you've focused on.
- 13:06So maybe we could start a little bit by talking about just the infrastructure and how actually
- 13:12the execution of Python code happens over at Instagram.
- 13:16So in addition to UWSGI, it's running on Linux, which is probably not surprising to you.
- 13:23Literally zero people are surprised now.
- 13:26Yeah.
- 13:27I thought it was a Windows server.
- 13:28Come on.
- 13:29Or Solaris.
- 13:31Yep.
- 13:32Or a Raspberry Pi cluster.
- 13:34Come on.
- 13:34That'd be awesome.
- 13:36So like one of the common things that people take advantage on Linux is fork and exec, where
- 13:43you start up a master process and then you fork off some trial processes and they can share
- 13:51all the memory of that master process.
- 13:53So it's a relatively cheap operation to go off and spawn those trial processes and you get
- 14:01a lot of sharing between those two processes, which reduces kind of the memory that you need
- 14:06to use and all that good stuff.
- 14:09And so the way UWSGI is working is that, you know, we are spawning our master process, going
- 14:17off importing kind of all of the website.
- 14:21Like we try to make sure that everything gets loaded initially and then spawn off a whole bunch
- 14:27of worker processes, which are going to actually be serving the traffic.
- 14:31And if something happens to one of those worker processes, then the master will come in and
- 14:38spawn a new worker to replace it.
- 14:40Yeah.
- 14:40That kind of goes on and on and on.
- 14:42And it's also not just about durability.
- 14:45It's also about scalability, right?
- 14:48If one of the worker processes is busy working on a request, well, there might be nine others
- 14:54and the supervisor process can look and say, OK, well, I got some requests got to be processed
- 15:00here.
- 15:01This one's not busy and sort of scale it out.
- 15:03And that also helps a lot with Pythons, GIL and stuff.
- 15:08You can just throw more of these worker processes at it to get more scalability.
- 15:11And at some point that kind of hits the database limits anyway.
- 15:15So it doesn't really matter that much, right?
- 15:16Yeah.
- 15:16And I think like you would ski can auto tune.
- 15:19I don't know exactly all the details of our settings.
- 15:21Yeah.
- 15:22There's a lot of advanced settings in there.
- 15:24Yeah.
- 15:24Yeah.
- 15:25Like it can, you know, tune for memory for stall workers.
- 15:30It's pretty smart.
- 15:32Yeah.
- 15:33But like, yeah.
- 15:34There's actually a really interesting, I don't know.
- 15:37Have you, maybe you've seen this.
- 15:38There's a really interesting post called configuring UISG for production deployment over on
- 15:45Bloomberg tech talking about all these knobs that they turn to make it work better and
- 15:50do these different things.
- 15:51And it's super interesting if these, like these tuning knobs are unfamiliar to Python people.
- 15:56Yeah.
- 15:57Yeah.
- 15:57But the important takeaway here is when we're talking about running your code on a single
- 16:01server, we're talking about five, 10, 20 copies of the same process running the same
- 16:06code with the same interpreter.
- 16:08Yeah, exactly.
- 16:10You guys pay for bigger cloud instances.
- 16:12No, I mean, you have your own data centers, right?
- 16:14So you probably get bigger VMs.
- 16:16This portion of Talk Python To Me is brought to you by Sentry.
- 16:21How would you like to remove a little stress from your life?
- 16:24Do you worry that users may be encountering errors, slowdowns, or crashes with your app right
- 16:30now?
- 16:30Would you even know it until they sent you that support email?
- 16:33How much better would it be to have the error or performance details immediately sent to you,
- 16:38including the call stack and values of local variables and the active user recorded in the
- 16:43report?
- 16:43With Sentry, this is not only possible, it's simple.
- 16:47In fact, we use Sentry on all the Talk Python web properties.
- 16:50We've actually fixed a bug triggered by a user and had the upgrade ready to roll out as we got
- 16:56the support email.
- 16:57That was a great email to write back.
- 16:59Hey, we already saw your error and have already rolled out the fix.
- 17:02Imagine their surprise.
- 17:04Surprise and delight your users.
- 17:06Create your Sentry account at talkpython.fm/sentry.
- 17:10And if you sign up with the code talkpython, all one word, it's good for two free months
- 17:15of Sentry's business plan, which will give you up to 20 times as many monthly events as
- 17:21well as other features.
- 17:22Create better software, delight your users, and support the podcast.
- 17:26Visit talkpython.fm/sentry and use the coupon code talkpython.
- 17:33And so that impacts a lot of the decisions that we make.
- 17:37We can talk about those more later.
- 17:40I think another interesting thing about RUWSD and our deployments in general is that we're
- 17:48also redeploying every 10 minutes when developers are...
- 17:52Yeah, I saw that and that blows my mind.
- 17:54So tell me about this rapid redeployment.
- 17:57It blows my mind too.
- 17:59And when I started it at Facebook, I guess now Meta, but it was Facebook back then, you
- 18:04go through a process called Bootcamp where you spend your first several weeks just learning
- 18:09about Facebook.
- 18:10And one of the first things you learn is like, Facebook.com redeploys every three to four
- 18:14hours.
- 18:14I'm like, that's insanely fast.
- 18:16And then land on Instagram.
- 18:18We were redeploy every 10 minutes.
- 18:21It's like, what?
- 18:22Yeah, that's incredible.
- 18:23Can you talk about why that is?
- 18:25Is there just that many improvements in code changes going on?
- 18:30Or is there some other like balancing reason that this happens?
- 18:34Like a DevOps-y thing?
- 18:35I don't know what all the original reasoning is.
- 18:39It has a very nice...
- 18:41So one of the nice things about deploying a lot is when something goes wrong, it's not
- 18:46hard to figure out what caused things to go wrong.
- 18:49So you're not looking at...
- 18:50Right.
- 18:50There's a bunch of small changes and each one gets deployed.
- 18:54So you're not going back to the last six months or whatever, right?
- 18:56Yeah, exactly.
- 18:58Or I mean, even...
- 18:59I mean, each of those deployments has a good number of changes in it.
- 19:04And even if it was like four hours, it would be a huge number of changes that you have
- 19:10to track things down through.
- 19:12And it's also like, it's really satisfying from a developer standpoint in that, you know,
- 19:18you land your change and it's rolling out in half an hour.
- 19:21So I don't think anyone...
- 19:24I don't know all the original reasoning, but I don't think anyone would really want to change
- 19:28it just because it actually has some significant benefits.
- 19:32It makes things interesting and challenging in some ways too.
- 19:34But otherwise, I think it's really nice.
- 19:36Yeah.
- 19:36I...
- 19:38It just never ceases to frustrate me or blow my mind how there's these companies just have
- 19:45extended downtime.
- 19:46Like, I'm not talking...
- 19:48We pushed out a new version and in order to switch things in and out of the load balancer,
- 19:52there's five seconds of downtime.
- 19:54Or we got to run a database migration and it creates a new index and that's going to take,
- 19:58you know, one or two minutes.
- 20:00I'm talking...
- 20:00We're going to be down for six hours on Sunday.
- 20:04So please schedule your work around.
- 20:06I'm just like, what is wrong with these companies?
- 20:09He's like, how is it possible that it takes so long to deploy these things?
- 20:15And if they had put in some mechanism to ship small amounts of code with automation, then
- 20:23they would just not be in this situation, right?
- 20:26Like, they would just...
- 20:27They would git pushed somewhere and then something would happen and then they would have a new
- 20:32version of the site, right?
- 20:34It always baffles me when I end up at a website and it's like, we're currently down for service.
- 20:40It's like, what?
- 20:41Yes.
- 20:42There's a website.
- 20:44You're not supposed to do that.
- 20:45The most insane thing...
- 20:47I'll get off this thing, but it drives me crazy.
- 20:50The most insane thing is I've seen websites that were closed on Sunday.
- 20:53I'm like, what do you mean it's closed on Sunday?
- 20:55Yeah.
- 20:55Just go and turn it off when you go home?
- 20:58Like, you know, it's open Monday to Friday sort of thing.
- 21:01This is like, it was like a government website and I don't know why it had to be closed,
- 21:05but apparently it had to be closed.
- 21:08Yeah.
- 21:08We have engineers standing by Monday through Friday to process your requests by hand.
- 21:12Exactly.
- 21:14We got to push the button.
- 21:15No one's there to push the button.
- 21:16Okay.
- 21:17So I guess one more setting the stage stories here or things to know is that you run these servers quite close to their limits in terms of like CPU usage and stuff like that.
- 21:32And then also you said one of the areas that you focus on is requests per second as your important metric.
- 21:39Do you want to talk about those for a moment?
- 21:41Sure.
- 21:41So I don't know what the overall numbers under normal load are.
- 21:48You know, I don't think the CPU load is necessarily super high.
- 21:52But what we want to know at the end of the day is like how many requests can we serve under peak load?
- 21:59And so what we can actually do is take traffic and route it to a set of servers and drive that traffic up to where the server is under peak load.
- 22:11And we see how many requests per second a server is able to serve at that point, which gives us a pretty good idea of kind of what the overall level of efficiency is.
- 22:23So when we make a change, we can basically run an A-B test where we take one set of servers that don't have the change, drive them up to peak load and compare it against another set of servers that have the change and drive those set of servers up to peak load.
- 22:41And then compare between the two and see how many requests per second we end up getting and what the change is.
- 22:49And we can do that to a decent amount of accuracy.
- 22:53I think kind of like when we kick off a manual test, we try to strive for within 0.25%.
- 22:59When we're doing releases of sender, I think we try to push it a little bit further by doing more runs.
- 23:05So we get down to like 0.1% or something like that.
- 23:09So we have a pretty good idea of what the performance impact of what those changes are going to end up looking like.
- 23:15I think that makes a ton of sense.
- 23:16You could do profiling and obviously you do.
- 23:19And we do that too.
- 23:20Yeah.
- 23:21But in the end of the day, there's a bunch of different things, right?
- 23:25If I profile against some process and say, well, this went this much faster in terms of CPU, maybe it took more memory.
- 23:35And at production scale, it turns into swap, which means it's dramatically, you know, there's a bunch of pushes and pulls in there.
- 23:43And this pragmatic, just like, let's just see what it can take now is interesting.
- 23:48You all are in this advantaged situation where you have more traffic than any given server can handle, I would imagine.
- 23:56Yes.
- 23:56So you can tune in.
- 23:59We actually run on one server.
- 24:00Exactly.
- 24:01We have a backup server.
- 24:03It hasn't been rebooted in seven years.
- 24:06You have the ability to say, well, let's just tune some of our traffic over to this one particular server to sort of see this limit.
- 24:14Whereas a lot of companies and products don't, right?
- 24:19Like I use this thing called Locust.io, which is just a fantastic Python framework for doing load testing to actually know the upper bound of what my servers can handle.
- 24:32Because we get a lot of traffic, but we don't get 30,000 requests a second, lots of traffic, right?
- 24:38And so I think this is really neat that you can actually test in production sort of beyond integration tests, not test that it works right, but send real traffic and actually see how it responds.
- 24:49Because really, that's the most important thing, right?
- 24:51Does it do more or does it do less than before?
- 24:52And, you know, you brought up profiling and we still have to use profiling sometimes to like, you know, 0.25%, 0.1%.
- 25:01That's still a lot of noise.
- 25:03And like, so if there's some little micro optimization, we can still be like, okay, well, what's this function using after the change, you know, kind of across some percentage of the entire fleet, which is kind of amazing because the profiling is just running on production traffic sampled.
- 25:22So for smaller things that ends up becoming super important.
- 25:27Right. And you're making a ton of changes as we're about to dive into, but they're additive or multiplicative or something like that, right?
- 25:34So if you make this thing 1% faster, that 5% faster, this 3% faster, all of a sudden you could end up at 20 to 30% faster in production, right?
- 25:42Yeah.
- 25:42And how do you do that math?
- 25:44Well, we just add up the percents.
- 25:45Exactly.
- 25:47Where's Cinder? Here we go.
- 25:49All right.
- 25:50So when I saw this come out, when did you all make this public?
- 25:54Shortly before PyCon?
- 25:56Yeah, that's right.
- 25:57Yeah.
- 25:58Which would put it like February, March, something like that.
- 26:01Yeah, something like that.
- 26:02That sounds exactly right with this eight months ago.
- 26:04So this is under the Facebook incubator.
- 26:08You guys got to rename this.
- 26:10It should be meta.
- 26:11Yeah.
- 26:11I wonder whose job that is.
- 26:13I mean, permalinks.
- 26:14Come on.
- 26:14So it doesn't matter all that much.
- 26:18It's Instagram, I guess.
- 26:19But let me just read the first opening bit here.
- 26:23I think there's a lot to take away just from the first sentence.
- 26:25Cinder is Instagram's internal performance-oriented production version of CPython 3.8.
- 26:32So performance-oriented.
- 26:34We've been talking about performance.
- 26:35We're going to get into a lot of the cool things you've done.
- 26:37Production version.
- 26:38So you guys are running on this on Cinder?
- 26:41Mm-hmm.
- 26:41Fantastic.
- 26:42And we redeploy like once a week?
- 26:45You redeploy the Cinder or CPython runtime, right?
- 26:48Yeah.
- 26:49Yeah, yeah, yeah.
- 26:50So like the source that's up here is, yeah, if you go back and look at maybe a week ago
- 26:57is what we're probably running in introduction at any given time.
- 26:59Right.
- 26:59Okay.
- 27:00Fantastic.
- 27:00And then CPython 3.8, because you've made a lot of changes to this that can't really move
- 27:08forward.
- 27:08So you picked the one, I'm guessing that was the most current when you first started,
- 27:13most current and stable, and just started working on that, right?
- 27:16So we do upgrade.
- 27:17Okay.
- 27:17It was previously built on CPython 3.7.
- 27:21Oh, cool.
- 27:21Okay.
- 27:22There's hundreds or, I don't know if we're yet up into thousands of changes yet.
- 27:29But there's a lot of dips that we've applied.
- 27:33It's been, I mean, we've been working on it for, I've been working on it for three years
- 27:39now and it predates me.
- 27:41So we've upgraded 3.7.
- 27:43We're going to upgrade to 3.10 next, which we're actually starting early next year.
- 27:50So it's just a big and bold process.
- 27:52And you've also contributed some stuff from Cinder to 3.10.
- 27:56So that'll be, that'll be interesting as well.
- 27:58That probably actually makes it harder to merge rather than easier.
- 28:01We hope that makes it easier.
- 28:04That is one of the things.
- 28:06I guess you could drop that whole section, right?
- 28:08You could just say, you know what?
- 28:09We don't even need this whole enhancement because that's just part of Python now, right?
- 28:13Okay.
- 28:13Yeah.
- 28:14That is the incentive for us.
- 28:15One of the incentives for us to contribute.
- 28:17Yeah.
- 28:19Yeah.
- 28:19Itamar out in the live stream in the audience says, we're close to 2,000 commits.
- 28:25Oh my gosh.
- 28:25Yeah.
- 28:26That's awesome.
- 28:26Itamar's going to be, he is now our kind of full-time dedicated resource to help us upstream
- 28:34things.
- 28:34Oh, fantastic.
- 28:35That's, oh, to upstream it.
- 28:36Itamar's job is to take the work you're doing here and then work on getting that into CPython
- 28:42properly.
- 28:43Okay.
- 28:43We could have been doing such a better job.
- 28:46I think we've upstream some little things, some slightly more significant things, but it's
- 28:52something that we really need to be working on more.
- 28:54Oh, that's fantastic.
- 28:55We've got someone who's dedicated to it and obviously he's not just doing it in a vacuum.
- 29:00We're going to help him.
- 29:02But having someone drive that and make sure it actually happens is super important.
- 29:07Yeah.
- 29:07That's really cool.
- 29:08I suspect that he and Lucas Lingo will become friends.
- 29:11Yeah.
- 29:12Lucas will be on the receiving end of that a lot.
- 29:16I bet.
- 29:16giving it the developer and residents over at CPython.
- 29:20Cool.
- 29:20All right.
- 29:21So I guess let's talk about this.
- 29:25Is it supported?
- 29:26So right now the story is you guys have put this out here as sort of a proof of concept.
- 29:32And by the way, we're using it, but not we expect other teams and companies to take this
- 29:38and then like just run on it as well.
- 29:39Right.
- 29:40This is probably more to like work on the upstreaming side.
- 29:43Is that the story?
- 29:44Yeah.
- 29:45And like let people know what we're doing.
- 29:47If someone wants to pick it up and try it, that's great.
- 29:50It's just mainly we're focused on our workload and making it faster and can't commit to helping
- 29:59people out and making it work for them.
- 30:01Right.
- 30:01But as you just said, you are working on bringing these changes up to CPython and you already
- 30:07have to some degree.
- 30:08So that's, you know, that's pretty good.
- 30:10I guess it also lets you all take a more specialized, focused view and say, you know what?
- 30:17We want to make uWSGI when it works off child processes.
- 30:21We want to make it that happen better and use less memory.
- 30:25And we're going to focus on that.
- 30:27And if it makes sense to move that to main Python, good.
- 30:30If not, then we're just going to keep those changes there.
- 30:33Right.
- 30:33Yeah.
- 30:33And then, I mean, that's happened, I think with like, we've done some work around
- 30:38immortalization of the GC heap, which is kind of a big improvement over not collecting.
- 30:44We were talking about earlier.
- 30:46And that didn't make sense for upstream CPython.
- 30:49And so it's okay.
- 30:50That's something that we just have to maintain.
- 30:51Cool.
- 30:52I was so excited when I saw this come out.
- 30:53I'm like, wow, this is the biggest performance story I've seen around CPython for quite a
- 30:58while.
- 30:58And now there have been some other things as well.
- 31:00We'll touch on at the end on how they come together.
- 31:03But maybe walk us through what is Cinder?
- 31:07What is this work?
- 31:08And we can dive into some of the areas, maybe.
- 31:10Sure.
- 31:11You have immortal instances highlighted.
- 31:13So we could start talking about that.
- 31:15You want to start with JIT first?
- 31:16I think.
- 31:16Yeah.
- 31:17Okay, sure.
- 31:17If you think JIT makes sense.
- 31:18Yeah, let's talk there.
- 31:19So the JIT isn't what I live on day to day.
- 31:21We have several other team members who are working on that full time.
- 31:26But it's obviously a huge part of the performance story.
- 31:29So the JIT right now is it's a method at a time JIT.
- 31:34So it compiles each individual method.
- 31:36It's, again, very tuned for our workload.
- 31:39Kind of you can see here, like some of the descriptions of how to use this thing.
- 31:45And it's mentioning this JIT list file.
- 31:47So when we're using this in production, what happens is we compile all the functions ahead
- 31:54of time inside of the master process before we fork off all this worker processes.
- 31:59Because we want all that memory to be shared between the different processes.
- 32:05So that's kind of a unusual mode for a JIT to work in.
- 32:09Right, right.
- 32:11They don't normally think about children processes and forking.
- 32:13They just do their own thing, right?
- 32:15Yeah.
- 32:16It's just like, okay, I have this method.
- 32:17It's gone hot.
- 32:18It's time to jib it.
- 32:19So it's used in this weird way.
- 32:22At some point, we need to, I think, add support for kind of normal jitting methods when they
- 32:28get hot.
- 32:29Like we were at the point where we're talking about using Cinder a little bit beyond Instagram
- 32:34within meta.
- 32:36And so at that point, people are going to need something that isn't so heavily tuned to
- 32:41UWSC.
- 32:42The JIT does, it's, you know, entirely, we kind of boom in the full stack.
- 32:48So it uses, I think, is it ASM shit?
- 32:51Yeah.
- 32:52It uses a library to do the X64 code generation.
- 32:56Other than that, we go from a high level representation.
- 33:01How close is the high level representation to just Python's bytecode?
- 33:06There is a pretty good set of overlap.
- 33:11There are also a lot of opcodes which kind of turn into multiple smaller things.
- 33:18So like off the top of my head, I think like making a function involves setting several
- 33:24different attributes on it at the end.
- 33:26So there's something that says, make me this function, which is just a single opcode in CPython.
- 33:32And there's several different opcodes which are setting those fields on it.
- 33:37So it's pretty close, but maybe slightly lower level.
- 33:41There's also a lot of opcodes in there for just kind of super low level operations.
- 33:47So one of the things, the thing that I spend most of my time working on is static Python.
- 33:52And so we added a bunch of things that support primitive math and simple loads and stores of
- 33:59fields and lower level things like that.
- 34:01So it's a mix.
- 34:02Yeah, the static Python that we're going to talk about is super cool.
- 34:05And is that possible because the JIT, like you can do whatever you want and then the
- 34:09JIT will see that and then adapt correctly?
- 34:12The JIT's really important to it because like it takes things that are usually tons of instructions
- 34:19and turns them into a single instruction or a couple of instructions.
- 34:23It's not 100% required.
- 34:25Like we support it in the interpreter loop and kind of our goal is to do no harm and generally
- 34:32like at least get the normal performance.
- 34:34But the JIT being able to resolve things statically and turn them into simple loads is super important.
- 34:40So from HIR, like we turn that into an SSA form and run a bunch of optimizations over it.
- 34:47I think one really interesting optimization is rough count removal.
- 34:52So we can see kind of these objects are either borrowed or just like that we'd have extra rough counts happening on them that we don't need to actually insert.
- 35:04And we can just elide all of those, which is super awesome.
- 35:09This portion of Talk Python and me is brought to you by TopTal.
- 35:12Are you looking to hire a developer to work on your latest project?
- 35:16Do you need some help with rounding out that app you just can't seem to get finished?
- 35:21Maybe you're even looking to do a little consulting work of your own.
- 35:24You should give TopTal a try.
- 35:26You may know that we have mobile apps for our courses over at Talk Python on iOS and Android.
- 35:32I actually used TopTal to hire a solid developer at a fair rate to help create those mobile apps.
- 35:38It was a great experience and I can totally recommend working with them.
- 35:42I met with a specialist who helped figure out my goals and technical skills that were required for the project.
- 35:47Then they did all the work to find just the right person.
- 35:50I had short interviews with two folks.
- 35:53I hired the second one and we released our apps just two months later.
- 35:57If you'd like to do something similar, please visit talkpython.fm/TopTal and click that hire top talent button.
- 36:05It really helps support the show.
- 36:09There's a lot of interesting stuff happening around memory that you all are doing.
- 36:13Yes.
- 36:14But one of them is this ref count and you make assumptions that are reasonable.
- 36:20Like when I'm in a method call of a class, I don't need to increment and then decrement the self object.
- 36:27Because guess what?
- 36:28The thing must be alive because it's doing stuff, right?
- 36:31And then it sounds like also maybe with constants, like the number one doesn't need its ref count changed and stuff like that.
- 36:36You notice that and go, you know what?
- 36:38We're just going to skip that.
- 36:39One of the things we've done is the immortalization of objects.
- 36:43And so we can also like the number one is going to be an immortal instance.
- 36:48And so in that case, we can be like, okay, yeah, we don't need to deal with ref counts on this.
- 36:53Unless, of course, like that number one ends up going off to somewhere that, you know, maybe doesn't understand the ref counting semantics of the JIT.
- 37:03In which case, maybe we do have to end up inserting them.
- 37:06Right.
- 37:06Or like if it's going through like an if else or something where one of the branches, we have to end up ref counting.
- 37:13So it's smart.
- 37:14So it's smart.
- 37:14And it's important because with immortal instances, our ref counts are a little bit more expensive than normal ref counts because we have to check to see if the object is immortal too.
- 37:24Right.
- 37:24So they're just doing an increment on a number.
- 37:26Yeah.
- 37:27Okay.
- 37:27So this, this immortal instances, this comes back to that memory thing that comes back to the turning off the GC, which you stopped turning it off.
- 37:35It sounds like immortal instances are a more nuanced way to solve that same problem.
- 37:39This is really about that fork and exact model.
- 37:42Yeah.
- 37:42So when we fork off those worker processes, they're initially sharing all the memory with the master process unless they happen to go off and write to it.
- 37:53And ref counts are a really big source of writing to that shared memory.
- 38:00And so what this does is takes all the objects that are present inside of the master process and runs through, marks them all as immortal.
- 38:09And then from then on out, the trial process will be like, oh, this thing's immortal.
- 38:15I'm not going to change the ref count.
- 38:17Okay.
- 38:17So this happens, you basically just scan the whole heap right before you do the fork and you're like everything, we're just going to clone this and it becomes unchangeable.
- 38:27And then we'll just, at least with regard to its ref count and we'll go from there.
- 38:31Yeah.
- 38:32And then as long as ideally we also don't, we shouldn't have a lot of global mutable state.
- 38:38I think people should be like, you know, if you think about what's in the master process, a process that's like classes and functions.
- 38:46And people shouldn't be really going off and mutating those things inside of the worker processes.
- 38:52Modules, yeah.
- 38:52It seems like something strange is happening if that's going on.
- 38:56Maybe let me ask you really quick or let you talk about really quickly this.
- 39:00The real benefit here is on Linux, when you fork off these processes, if the memory itself hasn't been changed, that can be shared across the 40 or 60 processes.
- 39:12But as soon as that memory changed, it has like a local copy has to be dedicated to that one worker process.
- 39:19So silly stuff, simple stuff like I want to pass this string around that happens to be global.
- 39:26And then it says, well, it's passed.
- 39:28So you've got to add ref to it, which means now you get 60 copies of it all of a sudden.
- 39:36Those really simple things, you are able to get lots better memory sharing, which then leads to cache hits versus cache miss and misses.
- 39:43And there's like all these knock on effects, right?
- 39:45Yeah.
- 39:45And it's not just the string itself, right?
- 39:48It's the entire page that the string lives on.
- 39:50So, you know, you might have a 15 byte string with, you know, a 16 byte object header and you end up copying 4K of memory.
- 40:01Because you changed a 6 reference number to a 7 or to a 5.
- 40:06Yeah.
- 40:07Fascinating.
- 40:08Okay.
- 40:09Do you think that Python, CPython itself could adopt this?
- 40:12Would it make sense?
- 40:13We tried to upstream it and there was resistance to it.
- 40:17I mean, it is touching something that's very core and it's going to be a bit of a maintenance burden.
- 40:22There are other reasons, I think, that people are now talking about wanting to have immortal instances.
- 40:29So Eric Snow has been working on sub-interpreters for a long time.
- 40:33And I think he has been interested in them recently for sharing objects between interpreters.
- 40:40And I think Sam Gross's work on NoGill might have some form of immortal instances as well.
- 40:47So maybe the core immortal instances support could land upstream at some point.
- 40:57But, you know, maybe the code that actually is walking the heat and is freezing every day, maybe that's very Instagram specific.
- 41:05And it doesn't have much value upstream.
- 41:08It seems to me that there's probably a set of things that would be good immortal instances for almost any Python process that starts up, right?
- 41:18Like before your code runs, everything there probably would be a good candidate for that.
- 41:23And, you know, there's potential, like it's kind of scary because ref counts are so frequent.
- 41:29And so adding extra code in the ref count process seems risky.
- 41:35But if you can't freeze enough stuff that was kind of there before the program started up, that's super core and happening a lot, then maybe it does actually end up making sense for other workloads too.
- 41:49Yeah, perhaps.
- 41:50Okay.
- 41:50So these immortal instances are one of the things that you all have done that's pretty fascinating.
- 41:55And also a huge win.
- 41:57Something like 5%.
- 41:58Yeah, yeah, that's right.
- 42:00It says right here.
- 42:00Big win in production, 5%.
- 42:03And does that mean 5% request per second?
- 42:06Is that when you say 5%, is that the metric you're talking about here?
- 42:09Yeah.
- 42:10Yeah.
- 42:10Have you thought about or tested, I'm sure you've thought about, like if this lets you run more worker processes off of increment that, that spawn worker process number?
- 42:21I think the developer who worked on this was doing, did look at that number and was looking at tweaking the number of worker processes.
- 42:30If I recalling, it got a little bit of pushback from people who were nervous about increasing it.
- 42:37Don't mess with this number.
- 42:38We never mess with this number.
- 42:39What are you doing?
- 42:41Yeah, but I hear you.
- 42:42I'm just thinking, you know, if it really does create more shared memory, maybe it creates more space on the same hardware for you to actually create more.
- 42:50And then that would just possibly allow even a bigger gain in request per second because there's more parallelism.
- 42:57I mean, given that it was such a big win, it could have just been that we were already under significant memory pressure and it got us out of significant memory pressure.
- 43:06Maybe we had the right number.
- 43:08Maybe we had too many hosts.
- 43:09I don't know.
- 43:10Yeah, yeah, perhaps.
- 43:11Perhaps.
- 43:11But still, 5% is, as one of the changes, is still a pretty big deal.
- 43:16Mm-hmm.
- 43:17All right.
- 43:17The next one on deck is strict modules.
- 43:21Let's talk about strict modules.
- 43:23I mean, we've talked about a little bit of things that are kind of related to this.
- 43:27You know what I'm saying?
- 43:28Like, if you have things that are going off and mutating your things in the master process, it's like, what?
- 43:33That's kind of crazy.
- 43:34So, strict modules work about performance, actually.
- 43:39There's a little bit of performance thought behind it, but now they're really, we're not considering them as a performance feature at all.
- 43:46They're more about a reliability feature.
- 43:48And so you brought up early on how, like, Python modules, just going off, executing some code.
- 43:55Who knows what that code's going to do?
- 43:56Right.
- 43:57So strict modules is an attempt to tame that process.
- 44:04And what we do is we run static analysis over the code.
- 44:09I mean, we are basically interpreting the code in a safe interpreter.
- 44:13And if the module has any external side effects or depends upon any external side effects, we don't allow it to be imported.
- 44:22And so we know that all the modules are side effect free that are strict.
- 44:27When you say they're side effect free, does that mean that the importing of them is side effect free or all of the functions are also side effect free?
- 44:35The importing of them.
- 44:36Their functions can do whatever they want.
- 44:38Got it.
- 44:38They can call functions from other modules.
- 44:41They can call functions from themselves.
- 44:43If they call those modules at the top level while doing the import, then those functions need to be side effect free.
- 44:50So where does this lead you?
- 44:51What do you get out of this?
- 44:52We git additional reliability.
- 44:54So like, you know, Instagram, as I think maybe we mentioned this being a big Bono monolithic application.
- 45:04Maybe we didn't get to that.
- 45:05Yeah, I don't think we talked about that.
- 45:07But this is not a hundred microservices type of thing, is it?
- 45:10No, it's one giant application.
- 45:12The thing that gets redeployed every 10 minutes is that giant application.
- 45:16That makes the redeployment even more impressive, by the way.
- 45:19Right?
- 45:20Yeah, I mean, maybe it's nice in that it's one giant application because you just have to redeploy one thing.
- 45:27Yeah, exactly.
- 45:29It's not a hundred different things you got to keep in sync all at the same time, right?
- 45:34Yeah.
- 45:34Our PEs make that happen.
- 45:36And it just happens behind the scenes as far as I'm concerned.
- 45:39So, you know, if you have like, you know, if you import one module and it depends on side effects from another module and then something changes,
- 45:50there's the import order, whether that's like state that things are depending upon, suddenly things blow up in production and your site doesn't work and everyone's really sad.
- 46:00So this is like, we want to get to a world where our modules are completely safe.
- 46:06We've used this.
- 46:07We've experimented doing other things with this.
- 46:09So like adding a hot reload capability.
- 46:12We know the modules are completely side effect free.
- 46:15Why not just patch the module in place and like let developers move on without restarting the website.
- 46:22It has the potential to kind of really change the way we store modules.
- 46:27So that we haven't gone down this route yet where instead of storing modules is a bunch of Python code that needs to run off and execute.
- 46:35Could we store modules as like, here's a class definition.
- 46:38Here's a function.
- 46:41And can we lazily load portions of the modules out of there?
- 46:45But we also have a really other different take on lazy loading that's in center now too.
- 46:51Okay.
- 46:51Yeah, that's pretty interesting.
- 46:53Because normally you can't re-import something because maybe you've set some kind of static value on a class.
- 47:04You've set some module level variable and that'll get wiped away, right?
- 47:08I mean, you can call reload on a module.
- 47:11But whether or not that's the safe thing to do, who knows?
- 47:16Exactly.
- 47:17Exactly.
- 47:18All right, cool.
- 47:19So I think one of the more interesting areas, probably the two that really stood out to me, are the JIT and StaticPython with the immortal objects being right behind it.
- 47:29But StaticPython, this is your area, right?
- 47:31What is this?
- 47:31Yeah.
- 47:31So this is an attempt to leverage the types that we already have throughout our entire code base.
- 47:40So Instagram is 100% typed, although there are still some many types flowing around.
- 47:47But you can't add code that isn't typed.
- 47:50So we know the types of things.
- 47:52Right.
- 47:53You're talking traditional just colon int, colon str, optional str, that type of typing.
- 47:59Yeah.
- 48:00Yeah.
- 48:00So why not add a compilation step when we're compiling things to PYCs instead of just ignoring the types?
- 48:10Why don't we pay attention to the types?
- 48:11Yeah.
- 48:12So we have a compiler that's written in Python.
- 48:15There's actually this old compiler package that started in Python 2.
- 48:21There's this external, there's this developer on GitHub, PF Falcon, who upgraded it to Python 3 at some point.
- 48:30And we upgraded it to Python 3.8 and made it match CPython identical for bytecode generation.
- 48:39So we have this great Python code base to work in, to write a compiler in.
- 48:44And we analyze the type annotations.
- 48:47And then we have runtime support and a set of new opcodes that can much more efficiently dispatch to things.
- 48:56There's a great, my coworker Carl Meyer had this awesome slide of calling a function during a Python talk.
- 49:04And it was just like pages, well, it was one page and a very, very tiny font of the assembly of what it takes for CPython to invoke a function.
- 49:13And then we're able to just directly call a function using the x64 calling convention.
- 49:19So shuffle a few registers around and admit a call instruction.
- 49:23That's awesome.
- 49:23It surprised me when I first got into Python, how expensive calling a function was.
- 49:29Not regardless of what it does, just the act of calling it.
- 49:33You know, coming from C# and C++, where you think so good in line by either the compiler or the JIT compiler and all sorts of interesting things.
- 49:40You're like, wait, this is expensive.
- 49:42I should consider whether or not I'm calling a function in a tight loop.
- 49:46There's so many things it has to deal with.
- 49:49The JIT has to deal with adding the default values then, and you don't know whether you're going to have to do that until you get to the function.
- 49:56It's got to deal with taking keyword arguments and mapping those onto the correct keywords.
- 50:03And that's one thing in static Python, we do that at compile time.
- 50:07If you're calling the keyword arguments, they turn into positional arguments because we know what we're going to.
- 50:13And we can just shuffle those around at compile time and just save a whole bunch of overhead.
- 50:19Yeah, that's fantastic.
- 50:20So the way people should think of this is maybe like mypyc or Cython, where it looks like regular Python, but then out the other side comes better stuff.
- 50:30Except for the difference here is you guys do it at JIT, not some sort of ahead of time pre-deployment type of thing.
- 50:36Yeah.
- 50:36And so the first thing we did with it was actually we had 40 Cython modules that were inside of the Instagram code base.
- 50:45And that was a big developer pain point and that those things had to be rebuilt.
- 50:49The tooling for like editing them wasn't as good because you don't get syntax color highlighting.
- 50:56And so we were able to just get rid of all of those.
- 50:58And those were heavily tuned, like using a bunch of Cython features.
- 51:03And so that really kind of proved things out that like, if we need to use low level features, we support things like permanent events if you want to use them.
- 51:12Instead of like having boxed variable size ends.
- 51:16So that was a good proving that it worked.
- 51:20And now I think it's more close to my PyC at runtime as we've been going through and converting other modules to static Python within the Instagram code base.
- 51:32Yeah, fantastic.
- 51:33You guys say that static Python plus sender JIT achieves seven times performance improvements over CPython on the type version of Richard's benchmark.
- 51:43I mean, obviously you got to be specific, right?
- 51:45But still, that's a huge difference.
- 51:47Yep.
- 51:47And some of that's like the ability to use primitive integers.
- 51:52Some of that's the ability to use b tables for invoking functions instead of having to do the dynamic lookup, which is something that both my PyC and Cython support.
- 52:03So lots of little things end up adding up a lot.
- 52:06And so that's just the JIT.
- 52:07Yeah, that's fantastic.
- 52:08Talk Python To Me is partially supported by our training courses.
- 52:13We have a new course over at Talk Python.
- 52:16HTMX plus Flask.
- 52:18Modern Python web apps hold the JavaScript.
- 52:20HTMX is one of the hottest properties in web development today.
- 52:24And for good reason.
- 52:25You might even remember all the stuff we talked about with Carson Gross back on episode 321.
- 52:30HTMX, along with the libraries and techniques we introduced in our new course, will have you writing the best Python web apps you've ever written.
- 52:38Clean, fast, and interactive.
- 52:40All without that front-end overhead.
- 52:41If you're a Python web developer that has wanted to build more dynamic, interactive apps, but don't want to or can't write a significant portion of your app in rich front-end JavaScript frameworks, you'll absolutely love HTMX.
- 52:54Check it out over at talkpython.fm/HTMX or just click the link in your podcast player show notes.
- 53:02You've talked about using primitive integers.
- 53:06And I've always thought that Python should support this idea somehow.
- 53:10Like, if you're doing some operation, like computing the square root or something, you take two numbers, two integers, and do some math, you know, maybe multiply, you know, square them, and then subtract them or something like that.
- 53:23And all of that stuff goes through a really high overhead version of what a number is, right?
- 53:30Like, instead of being a four or eight byte thing on a register, it's 50 bytes or something like that.
- 53:39As a pi object long thing that gets ref counted, and then, like, somewhere in there is the number bit.
- 53:46And that's awesome because it supports having huge numbers.
- 53:49Like, you don't ever see negative 2.1 billion when you're adding.
- 53:53You increment a number by one in Python, which is great.
- 53:55But it also means that at certain times you're doing math is just so much slower because you can't use registers.
- 54:02You've got to use, like, complex math, right?
- 54:05It sounds like you're doing this, like, let's treat this number as a small number rather than a pi object pointer drive thing.
- 54:14You know, JITs can handle this to some degree, right?
- 54:17And they can recognize that things are small numbers and generate more efficient code.
- 54:24I think when you had Anthony Yanni is talking about Pigeon doing this.
- 54:27Yeah.
- 54:28You know, if there's still some overhead there for dealing with the cases where you have to bail out and it's not that case.
- 54:35It's nice just having the straight line code that's there.
- 54:39You can also do type pointers, which, again, kind of handle that.
- 54:42Type pointers are kind of difficult on CPython because things expect pi object stars.
- 54:48And if that pi object star ever escapes to something that's not your CPython code, it's going to be very unhappy.
- 54:55Yeah.
- 54:55So this is, I mean, the nice thing is it's a relatively straightforward way to allow it.
- 55:00It was actually a little bit controversial in that, like, is this really what Python developers are going to expect?
- 55:07And are we going to have the right semantics there?
- 55:09And I think we have a to-do item to actually make things raise overflow errors if they do overflow instead of flowing over to negative 2 billion.
- 55:19That would be fantastic.
- 55:20I would personally rather see an overflow error than have it, you know, wrap around to the negative side or go back to zero if it's unsigned or whatever terrible outcome you're going to get there.
- 55:32Yeah.
- 55:33It's a much more reasonable behavior.
- 55:34We just, I guess we haven't been very motivated to actually go and fix that.
- 55:38Well, you're probably not doing the type of processing that would lead to that, right?
- 55:44You're probably not doing, like, scientific stuff where all of a sudden, you know, you took a factorial too big or you did some insane thing like that.
- 55:51There's probably not a single factorial in the entire code base, I would guess.
- 55:55Yeah.
- 55:55There's not a lot of math.
- 55:57There was, like, some, like, the only place where you've used primitive integers really was in the existing conversion, in the conversion of the existing Cython code where people had resorted to them.
- 56:08Right.
- 56:08Because it probably started as an int 32 or an int 64, right?
- 56:11Yeah.
- 56:12Yeah.
- 56:13Like, they had that option available to them.
- 56:15They used it.
- 56:16It's not, like, something that we're going through and sprinkling in in our random Python code.
- 56:21Because, like, yeah, we don't do much math.
- 56:22It's very object-oriented.
- 56:24Lots of function calls.
- 56:26Lots of classes.
- 56:27Yeah.
- 56:28Absolutely.
- 56:29All right.
- 56:30There's a lot of other good things that you talked about that are not necessarily listed right here.
- 56:35sort of, kind of, stuff with async and await.
- 56:38It sounds like you guys use async and await a lot.
- 56:41Is that right?
- 56:41Yeah.
- 56:41The entire code base is basically async.
- 56:43There was a big conversion, a big push to convert it right as I was starting.
- 56:50And now everything basically is async.
- 56:53Unless, obviously, it's not as...
- 56:55Wait a minute.
- 56:55I heard that async and await is slow.
- 56:57Why would you ever use that?
- 56:58Because it allows additional parallelization.
- 57:00Oh, yeah, yeah.
- 57:01Because multiple requests can be served by the same worker.
- 57:04Sure.
- 57:05Well, you know, whenever I hear those, I see examples of, like, we're just calling something as fast as you can.
- 57:11And it doesn't really provide...
- 57:13There's not an actual waiting, right?
- 57:15Like, the async and await is really good to scale the time.
- 57:18When you're waiting, do something else.
- 57:20And a lot of the examples say, well, this is slower.
- 57:22There's, like, no waiting period.
- 57:23But you know what is a really good slow thing?
- 57:25An external API and a database.
- 57:27And it sounds like you guys probably talk to those things.
- 57:29And yes.
- 57:30And the no waiting case is actually what this eager co-routine evaluation is all about.
- 57:37Like, yeah, sometimes we're talking to a database.
- 57:39But sometimes you have a function that's like, have I fetched this from the database?
- 57:44Okay, here it is.
- 57:46I don't have to wait for it.
- 57:47Otherwise, I'll go off and fetch it from the database.
- 57:50Right.
- 57:50If there's an early return before the first await.
- 57:53Exactly.
- 57:54There's not a huge value to calling this, right?
- 57:56Yeah.
- 57:57So tell us about this eager co-routine evaluation, which deals with that, right?
- 58:00Yeah.
- 58:01So this lets us run the function up to the first await and only go off and kind of.
- 58:08So normally what happens is you produce your co-routine object, schedule that on your event loop,
- 58:17and then eventually it'll get called.
- 58:19And now when you call the function, it's going to run.
- 58:21It's going to immediately run up to the first await.
- 58:24And if it doesn't hit that first await, it's just going to have the value that's produced.
- 58:28And you're not going to have to go through this big churn of going through the event loop with this whole co-routine object.
- 58:36Yeah, that's fantastic.
- 58:36Yeah.
- 58:37Yeah.
- 58:37Yeah.
- 58:37Yeah.
- 58:37It is slightly different semantics because now you could have some CPU heavy thing, which is just like not sharing with CPU with other workers, which is a great.
- 58:52And I think it can end up kind of, I think there can be some slight differences on what the scheduling happens, like where you could have observable differences, but we haven't had any issues with that.
- 59:05So I think it's might be a little bit controversial, but it's such a big when that it makes a lot of sense for us.
- 59:11It certainly could change the order.
- 59:13If you're doing, here's a whole bunch of co-routines and a bunch of awaits and stuff, and then you ran them in one mode, the sort of standard mode versus this, you would get a different order.
- 59:23But, you know, I mean, it sounds like you're going to ultimately put the same amount of CPU load on.
- 59:28I mean, async and await runs on one thread anyway, generally.
- 59:32Yeah.
- 59:33Unless you do something funky to like wrap some kind of thread or something, but in general, it still runs there.
- 59:39I would hope that most people aren't super dependent upon the order.
- 59:43If you're dependent upon the order and you're doing threading or something like that, you're doing it wrong.
- 59:49Yeah.
- 59:50The fairness issue might be a bigger issue.
- 59:53Yeah, yeah, yeah.
- 59:54Yeah.
- 59:54For us, it makes a lot of sense.
- 59:56Yeah, that's really cool.
- 59:57All right.
- 59:58Another one was shadow code or shadow byte code.
- 1:00:01Yeah.
- 1:00:02So this is our inline caching implementation.
- 1:00:04We've had this for a few years.
- 1:00:08Python 3.11 is getting something very similar.
- 1:00:12So we kind of expect that our version will be going away.
- 1:00:16We'll have to see if there's any cases that aren't covered or if there's any performance differences.
- 1:00:22But basically, it's nearly identical.
- 1:00:25We have an extra copy of the byte code, which is why it's called shadow byte code, which we can mutate in the background and replace the normal opcodes with specialized ones.
- 1:00:38So if we're doing a load adder and that load adder is an instance of a specific type, we can just say, okay, well, we know that this load adder doesn't have a type descriptor associated with it.
- 1:00:52Descriptor associated with it, like a get set data descriptor.
- 1:00:57We know that the instance has a split dictionary, which is the way CPython shares dictionaries, dictionary layout between instances of classes.
- 1:01:08We know this attribute is at offset two within split dictionary.
- 1:01:13So we just do a simple type check and make sure that the type is still compatible and go off and look in the instance dictionary and pull the value out.
- 1:01:22Instead of going through and looking up all those other things that I've just described, which is kind of what you have to do every single time on a normal load adder.
- 1:01:31Yeah, that's really cool.
- 1:01:32Is this something that could come back to CPython?
- 1:01:35I think the fact that they've gone off and built their own version 3.11 means that's not going to happen.
- 1:01:40But the idea lives there.
- 1:01:44Yes.
- 1:01:44Yeah.
- 1:01:45Yeah.
- 1:01:45Okay.
- 1:01:45Awesome.
- 1:01:46So we're getting short on time here, but maybe you could just highlight really quickly, stepping back one feature point on the asyncio stuff is the send receive without stop iteration stuff that you did.
- 1:02:02And then that getting upstreamed as well already.
- 1:02:05Yeah.
- 1:02:05Yeah.
- 1:02:05So that was adding.
- 1:02:07So I did work on this developer Vladimir Mativ worked on this and that was adding in a, I think he added in a new set of slots for actually achieving this at the end of the day.
- 1:02:22And sender.
- 1:02:23And sender.
- 1:02:23And sender.
- 1:02:23And sender.
- 1:02:23And sender.
- 1:02:23We have a type flag that says this type has these additional slots.
- 1:02:27And so we can call the send function and the receive function and get back and be done.
- 1:02:34That's kind of did this thing return a result?
- 1:02:37Did this thing throw an exception?
- 1:02:39And here's the result.
- 1:02:41Yeah.
- 1:02:41So that instead of producing the stop iteration on every single result, we just return the result.
- 1:02:48And that is obviously big with coroutines because coroutines are generators at the end of the day.
- 1:02:55Yeah.
- 1:02:56That's fantastic.
- 1:02:57Everything can get more efficient by not allocating on sort of hidden behind the scene exceptions, right?
- 1:03:03Yeah.
- 1:03:03All right.
- 1:03:04Well, there's a bunch of cool stuff here.
- 1:03:06And I'm really happy to hear that you and your team and Edomar out there are working on bringing this stuff over.
- 1:03:11Because I was so excited when I saw it.
- 1:03:13And then I saw, is it supported?
- 1:03:14Like, not really.
- 1:03:15You really shouldn't use this.
- 1:03:16I'm like, oh, but it looks so good.
- 1:03:18Like, I want so much of this stuff to be moved over.
- 1:03:20So that's cool.
- 1:03:21And I think some of it will be difficult to move over.
- 1:03:24Like, in moving the entire JIT over, the JIT's written in C++.
- 1:03:28Obviously, the CPython core developers were open to C++ for a JIT at one point in time with unladen swallow.
- 1:03:35Whether or not that feeling has changed, who knows.
- 1:03:40But it's a big piece of code to drop in.
- 1:03:42So one thing that we really want to do going forward is actually get to the point where the big pieces of sender are actually just pip installable.
- 1:03:51So we'll work on getting the hooks that we need upstreamed.
- 1:03:55One thing that the JIT relies on a lot is dictionary watchers that we can do really super fast global loads.
- 1:04:02And we have a bunch of hooks into, like, type modification and function modification that aren't super onerous by any means.
- 1:04:10Yeah.
- 1:04:11So if we can get those upstream, then we can make the JIT just be, here, pip install us.
- 1:04:15And so hopefully we can get those upstreamed in 3.11 and have pip install sender start working.
- 1:04:24Yeah.
- 1:04:25That'd be awesome.
- 1:04:26Yeah.
- 1:04:26So, yeah, really good work on these.
- 1:04:27I guess let's wrap up our conversation here because we're definitely short on time.
- 1:04:31But, you know, there's the other projects, which I'm going to start calling the Shannon Plan that Mark and Guido are working on.
- 1:04:39They've been working on for a year.
- 1:04:41And then there's Pidgin, which, by the way, Anthony Shaw has taken over, but you created Pidgin, right?
- 1:04:48Yep.
- 1:04:48That's awesome.
- 1:04:49Well done on that.
- 1:04:51On a whim at a playtime.
- 1:04:54Exactly.
- 1:04:55And Sam Gross's work on the NoGill stuff.
- 1:04:59All of this seems to be independent, but in the same area as those things.
- 1:05:04Where do you see the synergies?
- 1:05:05Do you see any chance for those to, like, come together?
- 1:05:07Is that through some kind of pip putting the right hooks in there and other people plugging in what they want?
- 1:05:12Or what do you see there?
- 1:05:14It'd be great if these could come together a little bit.
- 1:05:16Yeah.
- 1:05:16In a lot of places, we're working on independent things.
- 1:05:21Obviously, Pidgin is a JIT and we're a JIT.
- 1:05:23With different goals to some degree, right?
- 1:05:25Yeah.
- 1:05:26But, I mean, also very similar and overlapping goals.
- 1:05:30I think there'll probably have to be discussion of, like, what the future of JITs look like in CPython.
- 1:05:37Like, is that something that's part of the core?
- 1:05:39Or is that something that should live on as being external?
- 1:05:43Or is there going to be a grand competition and at one point one of the JITs will win?
- 1:05:48Who knows?
- 1:05:49It's a good discussion that should probably take place.
- 1:05:52The hooks for JITs are there.
- 1:05:54And between what Brett and I added for Pidgin and Mark Shannon's Vector Call work that happened several releases ago,
- 1:06:03I think JITs have a pretty good foundation for booking in and replacing code execution.
- 1:06:08They probably need other books to, you know, get into other things like the Dictionary Watchers that I mentioned.
- 1:06:15But, like, we can keep working on books.
- 1:06:18Other things have less overlap.
- 1:06:20So, hopefully we can all kind of work in our own streets and work to improve things and make those available to Python developers in the best way that's available.
- 1:06:32And not be stomping on each other's shoes or do profiting work too much.
- 1:06:37Yeah, absolutely.
- 1:06:37Well, it's an exciting time.
- 1:06:40I feel like a lot of stuff is sort of coming back to the forefront.
- 1:06:42And it feels like...
- 1:06:44So much performance work.
- 1:06:45Yeah, for sure.
- 1:06:46It feels like the core developers are open to hearing about it and taking on some of the, you know, the disruption and complexity that might come from it.
- 1:06:54But still, it could be valuable, right?
- 1:06:56Mm-hmm.
- 1:06:57It's absolutely going to be valuable.
- 1:06:59Yeah.
- 1:07:00I feel like there's enough pressure from other languages like Go and Rust and stuff.
- 1:07:03Oh, you should come over to our world and forget that Python stuff.
- 1:07:07You're like, hold on, hold on, hold on.
- 1:07:08We can just...
- 1:07:09We can do that too.
- 1:07:10But we've got to...
- 1:07:11We can get faster.
- 1:07:12Yeah.
- 1:07:12Well, this is awesome work.
- 1:07:13Thanks for coming on and sharing.
- 1:07:15Thank you for having me.
- 1:07:16Yeah, you and your team are doing...
- 1:07:18Now, before you get out of here, got the final two questions.
- 1:07:20Okay.
- 1:07:21You're going to write some...
- 1:07:22Let's do notable PyPI package first.
- 1:07:24So is there some library or notable package out there that you come across?
- 1:07:28Like, oh, this thing's awesome.
- 1:07:29People should know about whatever.
- 1:07:31So does it have to be PyPI?
- 1:07:33No, any project.
- 1:07:34So as I said, I have a very weird relationship with Python, right?
- 1:07:38As using mainly from the implementation side.
- 1:07:42So I think my favorite package is the standard library.
- 1:07:45Okay.
- 1:07:46Right on.
- 1:07:47And if I had to pick something out of the standard library, I think one of the coolest parts is
- 1:07:51mock.
- 1:07:52It's been an interesting integration with static Python, but like it, like seeing the way people
- 1:07:59use it and drive their tests, it's kind of really kind of amazing.
- 1:08:03Yeah, I agree.
- 1:08:04It's definitely a very cool one people should certainly be using.
- 1:08:07And now if you're going to write some Python code, you might also have special requirements
- 1:08:10that shift you in one way or the other.
- 1:08:12But what editor are you using?
- 1:08:13Oh, I use VS Code pretty much.
- 1:08:15Well, I use VS Code.
- 1:08:17I use nano when I need to make a quick edit from the command prompt.
- 1:08:21Yeah, cool.
- 1:08:21I'm a fan of nano as well.
- 1:08:22Like, let's just keep it simple.
- 1:08:23It's just give me a nano.
- 1:08:25Let me edit this thing over the shelf.
- 1:08:26It has syntax cover highlighting that.
- 1:08:29It's so advanced.
- 1:08:31It's awesome.
- 1:08:31Cool.
- 1:08:32No, no, I use it as well.
- 1:08:33All right.
- 1:08:33Well, Dino, thank you so much for being here.
- 1:08:35Final call to action.
- 1:08:36People are excited about these ideas.
- 1:08:38Maybe they want to contribute back or try them out.
- 1:08:40What do you say?
- 1:08:41I mean, try out sender.
- 1:08:42Yeah, it's unsupported.
- 1:08:43But, you know, if you have thoughts on it, that's cool.
- 1:08:46You do have instructions on how to build it right here.
- 1:08:48So you could check it out.
- 1:08:49There's a Docker container.
- 1:08:50Yeah.
- 1:08:51Okay.
- 1:08:51Yeah.
- 1:08:52So it's pretty easy to give it a shot.
- 1:08:55You know, like, it might be harder to get it up and running in a perf-sensitive environment.
- 1:09:01If you want to try out Static Python, that'd be cool.
- 1:09:04Or Strict Modules.
- 1:09:05And give us any feedback you have on those.
- 1:09:08Fantastic.
- 1:09:09All right.
- 1:09:09Well, thanks for being on the show.
- 1:09:10Great to chat with you.
- 1:09:11Thank you, Michael.
- 1:09:12Yeah, you bet.
- 1:09:13Bye.
- 1:09:13See ya.
- 1:09:14See ya.
- 1:09:14This has been another episode of Talk Python To Me.
- 1:09:18Thank you to our sponsors.
- 1:09:20Be sure to check out what they're offering.
- 1:09:21It really helps support the show.
- 1:09:23Take some stress out of your life.
- 1:09:25Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:09:30Sentry.
- 1:09:30Just visit talkpython.fm/sentry and get started for free.
- 1:09:35And be sure to use the promo code TALKPYTHON, all one word.
- 1:09:39With TopTal, you get quality talent without the whole hiring process.
- 1:09:44Start 80% closer to success by working with TopTal.
- 1:09:47Just visit talkpython.fm/TopTal to get started.
- 1:09:52Want to level up your Python?
- 1:09:54We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:09:58Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:10:03And best of all, there's not a subscription in sight.
- 1:10:06Check it out for yourself at training.talkpython.fm.
- 1:10:09Be sure to subscribe to the show.
- 1:10:11Open your favorite podcast app and search for Python.
- 1:10:14We should be right at the top.
- 1:10:15You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:10:20and the direct RSS feed at /rss on talkpython.fm.
- 1:10:25We're live streaming most of our recordings these days.
- 1:10:28If you want to be part of the show and have your comments featured on the air,
- 1:10:31be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:10:36This is your host, Michael Kennedy.
- 1:10:37Thanks so much for listening.
- 1:10:39I really appreciate it.
- 1:10:40Now get out there and write some Python code.
- 1:10:42I'll see you next time.
- 1:11:03Thank you.