06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Hacker News (YC) · · 1:14:45 runtime

Functional Mechanical Sympathy [video]

Comments

In the wiki JavaScriptTypeScriptRustPython Also namedWebpack, Zed Industries, Biome, Meta
Transcript 7257 words · auto-generated captions

Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.

Hello. >> All right. Ah, hello. Wait, where should I be looking? There. Okay. Anyway. Oh, right there, maybe. [laughter] >> Yeah. Good morning. Um, so I'm going to steal like five minutes of your time and just say welcome everyone to Fun Arc 2026. It's our fourth year doing this. And this year we have a and I'm very excited about this keynote. Uh, a keynote from Richard Feldman. Uh I think Richard is a leading uh voice in functional software and really in the software industry. Richard I hope you feel the same way. Uh he's coming to us

um from Zed Industries and he's also the

uh uh I don't know author runner uh showrunner of the software unscripted podcast and you also may know him as the creator of the rock link programming language. That's ROC not ROK. Um, and

then with that, uh, Richard, we have lots of time, so if you want to go over, please go ahead. Uh, and the floor is yours. >> Be careful what you wish for. I know you got a coffee break after this, so I'll try not to interrupt that, but hopefully we'll also have some time for questions at the end. All right, great. Uh, thanks so much for the lovely intro. Um, can everyone see my presentation? >> Yes, we can. It looks great. >> Great. All right, this is functional mechanical sympathy. I'm Richard Feldman. So if we look at just weekly downloads in 2026, here are some numbers that pertain to functional programming 5 million, 14 million, 32 million. These

are not downloads of people using packages from functional languages. These are people using TypeScript. This is FPTS. This is a functional programming and TypeScript library. This is Ramda, which is a combination of TypeScript and JavaScript. So JavaScript programmers, same group of people. And then this is effect uh which is another functional effects library. There's a lot of people doing functional programming in the JavaScript TypeScript world. It's arguably the largest group of functional programmers on the planet depending on how you want to measure that. Even though these are not functional languages, the point is people who use JavaScript like functional programming. A lot of them do. Now if we look at the state of JavaScript tooling here in 2026, there's a bit of a surprising picture here.

Okay, there's a lot of JavaScript tooling written in TypeScript. That's not very surprising. Webpack, rollup, prettier, post CSS, but these are all about 10 years old. This is not really the latest and greatest of the JavaScript tooling in 2026. For that, you look at more like Rust. So, Turbopac, RSpack, SWC, Roll Down, Biome, OXC, and then there's a little bit of Go, but one of the Go ones is sort of outside because TSC is the TypeScript compiler that's written in Go now. They rewrote it from Typescript to Go. And in fact, most of these Rust ones are rewrites of earlier things like Webpack to Turboac uh roll up to roll down.

These are rewrites from JavaScript from JavaScript programmers who like functional programming a lot to different languages that are not JavaScript but which are also not functional languages. So where's all the JavaScript tooling in like functional languages? There actually has been a bit uh specifically in Okamel. So flow and fastpack were both popular or or at

least somewhat popular tools written in Okamel. Flow these days is pretty much only used by Meta which created it. Everybody else uses TypeScript and Fastpac uh I believe is just not really used at all these days. So this is a little bit weird. You would think certainly like since there's multiple very popular uh JavaScript tools written in Go and Obam and Go you know have a lot in common in terms of you know I want I want something that's faster than JavaScript. What's the deal? Why are people not choosing functional languages? Well, I think the the the fact that the language community that does tons of FP is choosing Rust and Go over dedicated FP languages is something that should give us pause and ask us why might this be? But to be fair, it's not just the JavaScript and TypeScript community that's doing this. Um, if you look at like what are some of the fastest growing languages today, I think you can make a pretty strong case that Rust and Go are the two most popular or increasingly popular languages in 2026.

Um, so this is the Jet Brains survey. Jet Brains is a maker of IDE. Um, they've been doing these surveys for several years. This is the most recent one, so from 2025 since we're obviously in the middle of 2026. um they have this language promise index that puts together a variety of factors to sort of answer the question which languages are the most promising in terms of growth. So number one was TypeScript. Okay, TypeScript came out in 2013. Uh here's Rust at number two which came out in 2012. Python which came out in 1991, so 30 plus years ago. And then Go from 2009. Okay. Now saying that Python, which is like a 30-year-old language, is one of the most promising new languages. Okay, maybe it's not a new language, but I guess the point is just that Python's getting used more and more. And if we're being honest, like Typescript is really just adding type checking to JavaScript from 1995. The fact that this is being counted as a separate language, I think you could really give the credit to JavaScript here. It's just that people are increasingly liking type-checking in the Python world, the way that you add types to Python is using a separate tool that's not considered a separate programming language. So, if it were, that would probably be number three instead. The point here is that of sort of newer languages, that is to say, languages only created in the last 15-ish years, it's really just Rust and Go. Now again, this is not just among the JavaScript community, which likes functional programming, this is sort of globally. Why is it that among newer languages, that is languages not created three decades ago or then maybe updated with types, why is it that these two languages are getting so much popularity?

Well, I think the rise of Rust and Go can be explained in part in terms of C++. So, C++ is a 40 plus year old language. Um, and its sort of claim to fame these days remains the same as what it was when it came out, which is just like it's really really great performance. Um, some some more tools than what C gives you. But of course, the downside is always that it's been very complex. And a more recent downside that people care about more these days is that it's unsafe. As in it's memory unsafe. Russ's claim to fame is that it's just as fast as C++ or you can write programs that are just as fast but they're memory safe. And then Go is memory safe unless you disregard a little small edge case in the language.

Um it's almost as fast as Rust but much much simpler. So it's attacking the complexity part. Now again you could look at languages like this and you could say well yeah we've got languages in the functional programming world that are you know some amount of fast and some amount of memory safe. Um although they should all be in theory memory safe give or take FFI or give or take using functions with unsafe in the name. Um but for some reason they're not catching on the way that Rust and Go are. Now in the old days when a new language got popular it was usually object-oriented and people would say oh that's the explanation. It's everyone wants to use OP. But even though object-oriented languages are extremely popular still today Rust and Go are not. So people are okay with using a different paradigm, in this case procedural with their new language that they're adopting. It's just they're not choosing FP. Why is that? Well, there are a lot of different factors there, but I do think that one of the factors that we don't talk enough about in the FP community is performance. So I want to talk about performance requirements for a second. Now, to be fair, for some projects, performance is kind of a nice to have. It's like if it runs faster, great. It feels nicer to use. That's very nice. But for other projects, performance is an actual requirement.

It's like a hard there's a certain line and if you can't get the performance over this level, the project just won't work. So I had a project like this. Um this is actually the the project that got me into my first functional language. Um and it was software for 100,000 K plus words uh books like writing really really long form writing. I had initially tried to write this thing that I was writing in Google Docs and it just was not working. It got so sluggish that, you know, I would try to type a letter and I could see this visible latency. So, I wanted to build something for myself, but it had to be able to handle documents that were this long fast enough or else I just couldn't use it. And that really narrowed down the the scope of what I was able to use.

I also really wanted it to be in the browser. And so, fortunately, the browser was capable of handling that, just not the way that Google Docs was doing it. Um, but if I couldn't find a functional language that was going to help me with that, I wasn't going to use a functional language. Now, as it happened, I also had some correctness and complexity problems. And so, I was in the market for something functional because I had heard that functional languages could help with this. And sure enough, they did. But there was always that performance requirement like I cannot play in this sandbox. I can't work in this domain unless I can get the performance to be good enough.

Fortunately, right around the time that I was doing this, this blog post came out called Blazing Fast HTML in Elm. And this was how I got into the Elm programming language which was the first functional programming language that I used on a real project that was like sort of in anger as opposed to just you know playing around with something. Um in that blog post there are some benchmarks and it's comparing Elm uh in terms of rendering speed on a common app that lots of different um technologies at the time were benchmarking themselves against and Elm was comparing quite favorably to some of the mainstream alternatives. So Angular and React were two very very popular ways of doing the same kind of solving the same kinds of problems as Elm was. That is to say browser based user interfaces. Elm was doing way better than React, way better than Angular. It wasn't the fastest. Elm wasn't coming out in in this blog post and claiming, oh, we are the absolute fastest thing that you can possibly get.

There was this thing called Mercury, which was running even faster than Elm, but Mercury was kind of this niche thing that almost nobody was using. So if I'm coming to this and looking at this like I am somebody who wants to build a project I'm considering this functional technology seeing this really appeals to me because I say oh this is actually a lot faster than the mainstream alternatives that I would be considering. I'm not looking for such blazing fast, you know, performance at all costs that I would turn to something like Mercury. And so the functional benefits that I get in terms of expecting that my application is going to be easier to maintain, easier to understand, that I would get from Elm start to come into play. If I needed absolute maximum performance, okay, fair enough. I'm going for Mercury. And also, if I need maximum performance, maybe I'm just always going to turn to something like Rust or Zigg or C or C++. and I'm never even going to consider a functional language with a garbage collector because my performance requirements are just so astronomically high. The thing is the vast majority of software in the world has performance requirements that are somewhere below that absolute maximum tier of performance.

So, another good blog post that um this is not something that I personally use, but I liked how they presented their their work uh was called benchmarking Go versus Node versus Elixir. And basically, they did this uh stress test of some different um web servers in terms of how many requests per second can they handle and just like really trivial requests before they start falling over. So, this gives you sort of a baseline of you know, how much traffic can these things possibly handle even setting aside the work that the servers themselves are doing. So the Noode.js server maxed out around 25k requests per second. Uh the Go one didn't uh max out in in sense of like you know stopped being able to handle more requests but it did start slowing down. The performance started degrading around 70k requests per second. They were going all the way up to 100k. Um Go didn't get stopped at 70k. It was able to handle the 100k even though it was slower. Um but the elixir one uh was able to handle all 100k requests. Now, this was not without downsides because if they showed on the graph like Elixir did have more CPU usage than Go. In other words, although Go was slowing down, it was overall using the hardware more efficiently than Elixir was. So, that's, you know, a reasonable trade-off there.

But again, as with the Elm example, we can see if I'm in the market for something functional, this is a really great pitch saying, look, are you looking for a functional experience where you can hopefully, you know, have a better experience developing your program, but also from a performance perspective, you can say, yeah, this is going to have consistent performance all the way up to 100,000 requests per second. That's what this, you know, this benchmark in this blog post is saying. Whereas, if you use Go, you're going to see performance degradation around 70K. and and if you're using node, you're going to see it sort of max out and not even be able to handle more than 25k.

This is a nice pitch because it tells me, okay, if if my performance requirements are such that I really want to be able to handle a lot of concurrent requests, this is a great selling point for elixir. One more. This is warp and hasll compared to Engine X and C. So engine X is a very highly optimized C web server. Granted, this one actually all three of these are from like 10 plus years ago, so I don't know how much the these still apply today. The point is more about, you know, reading them and and looking at what are they saying and what are they able to claim um you know relative to what was true at the time.

So here they showed that you know with three plus workers warp was able to serve more requests per second than engine X. So if you just looked at warp versus engine X and you've only got one worker in other words they're just handling you know one thread one request at a time. um they were, you know, EngineX was winning out, but as soon as you started increasing the number of workers, in other words, being able to use multiple cores, being able to use multiple threads, like doing more work concurrently, Warp started outpacing EngineX. So whereas the previous one with Elixir was about how many concurrent connections can you handle before you start seeing performance degradation, this is more about how does your throughput improve as you're able to, you know, use more of a multi-core server or something like that. the more concurrent work you can throw at this thing, does that increase throughput?

And if so, by how much? And here they were showing that warp was actually able to use those resources more effectively and get you more throughput compared to even something like EngineX which is written in C. Now the reason for this of course is that EngineX is sort of limited to using like the operating system threads whereas Haskell can have more of a green thread co-outine type thing going on. Um but there was a detail that I thought was pretty interesting. So one of the quotes they said in this piece is some people believe that functional programming languages are slow or impractical which I think is true. A lot of people I think do believe that in the functional programming style new data are frequently created and data allocation occurs that is to say heap allocation um regularly enough for context switching.

So this is there interesting um two points to be made here. one is that they're talking about well in a functional programming language like HA hasll especially if you're using persistent data structures which we're going to talk more about later um you do get a lot of sort of memory traffic where you're creating new objects on the heap or or new allocations on the heap and then deallocating them on a pretty regular basis and on the one hand all else being equal that's a downside because heap allocations are famously one of the you know sort of on on the more expensive side of things that you can do compared to like arithmetic or conditionals. uh but the corresponding upside here is that this provided a very natural point for the Haskell program to say oh we you know we can do a context switch here safely whereas in the other case you might have to do something like an interrupt because you're not sure exactly where you can find a consistent point to do a context switch um without like you know interrupting the program.

So this is an an interesting way of turning something of a something that's a downside uh by default into an upside in a specific context of a concurrent web server. So this the sort of the theme of these three posts is that these are ways to pitch performance of functional programming or anything else in a way that I think is appealing and comes across to me as somebody who has performance requirements for my application. Whether it's a UI like in the case of Elm or whether it's a web server that needs to handle lots of concurrent connections like in the case of Elixir or I just care a lot about throughput like in the case of warp.

These are appealing I think for some reasons that we can enumerate. First they have all three of these have relevant benchmarks with compelling numbers. So relevant meaning like you know these are benchmarks for for that actually matter to my performance requirements whether it's like rendering speed or it's number of concurrent connections or it's throughput of those connections. if that's what I care about for my use case, that's what my performance requirements are based on, then I'm like, great, these benchmarks give me a sense of confidence. Um, and then of course the numbers have to be compelling. If if they show that, you know, it's it's only a tiny bit better or it's it's worse than the uh the alternatives, that's not going to be appealing to me. They have mainstream alternatives included in the benchmarks.

So, not just saying, for example, oh, I'm comparing to the previous release of my technology or I'm comparing to uh, you know, nothing at all. I'm just saying like, oh, here are my benchmarks. Enjoy. Um, this gives me a sense of like, well, if I were to choose this functional technology as opposed to the others, you know, what am I going to get in comparison? And then finally, these are e easily achievable results in practice. In other words, I'm not going to have to contort my code and write really hard to maintain unpleasant code in order to get these numbers, which is sometimes what is required to get some performance numbers. They say, yeah, no, this is just I'm using the technology in the normal way. I'm using it in the way that you know I would want to use it um where I'm not sacrificing the benefits of functional programming. Um I guess I use it in the normal way and I get these results. So let me contrast this with unappealing performance. So this would be like a pitch for you know an FP technology or any technology that I would say is unappealing. It's basically the opposite of these. So if it's got irrelevant benchmarks where it's like hey they're benchmarking something that I don't care about. this is not relevant to my performance requirements for my project or or maybe there are no measurements at all which is sometimes unfortunately something that comes out you get a bit of a handwave it's like oh this is going to be fast because it's functional programming and Moors law end of Moor's law and concurrency and immutability makes concurrency easy handwave handwave handwave performance that's not the same thing as having benchmarks because there's just so many ingredients that go into performance that just saying well you know asmtoic something something it just doesn't cut it anymore for people who are trying to build things and have actual real performance requirements as part of their project. They need to see benchmarks. They need to see measurements, numbers, and of course, comparisons to mainstream alternatives, not comparisons to niche things that they weren't considering anyway or comparisons to nothing at all. Um, you got to have measurements and comparisons to mainstream alternatives. And then, of course, finally, you know, unachievable results are not particularly helpful.

There was an article which was absolutely not trying to be a a pitch for uh functional programming but it was kind of talking about if you were using closure I think it was uh and you're insisting on not doing foreign function interfaces to lower level languages um you know how close can you get to like C levels of performance and the conclusion was well if you if you write this closure in an absolutely horrendous way look you can get it to be a lot faster which is interesting for a blog post that's mostly about a curiosity which is what this one was but if someone had been trying to actually pitch that as a way to write closure. I think that would have fallen really flat because people are saying like, well, if I'm going to write code this horrendous, why don't I just use C or something like that instead, which is going to get even faster performance and might actually look less horrendous and be easier to maintain than contorting the language in this way.

I want to call out a really common example that I think uh is is not usually used as a performance pitch to be fair, but something that I think honestly contributes to this perception that functional programming isn't good at performance and is sort of like slow and clunky to paraphrase what the uh the Warp article was saying. So, this is something I've seen. This is one of the first things I ever saw when I got into FP, which is maybe 15 plus years ago now. Um, this is I'm going to say it claims to be a an elegant quicksort implementation uh in Haskell and it's demonstrating look how concisely and elegantly you can implement quote unquote quicksort in Haskell. I see a bunch of problems with this as somebody who has a passing understanding of what quicksort is. Quicksort is an algorithm that originally written in you know lower level languages like C. And its whole point is that you take an array of contiguous elements and you iterate through it a number of times and you do in place mutation to swap elements until it has been sorted in place. Some of the important characteristics of quicksort are one it is completely stack safe. So there's no chance that you're going to get stack overflow from doing quicksort unless you know you don't have enough stack space to even call the function in the first place. It does no heap allocations of any kind and it runs pretty quickly, hence the name quicksort. Okay, this implementation of quote unquote quicksort immediately I mean okay so it does sort but like it's not quick and it is in no way the quicksort algorithm. [laughter] This is just not quicksort. Calling it quick sort is ridiculous. So the first thing that it does is a recursive call to itself that's not tail recursive. So it's not going to get ained into a for loop. It's it's going to be stack unsafe. you absolutely can blow this out by calling this function. Then it does++ which is going to concatenate two linked lists together. So we're already starting with we're not even working on uh a flat array of elements which is what quicksort one of several things quicksort the real quicksort relies on for performance. And this is a heap allocation of course because it's a a linked list. Then we're doing another concatenating a linked list around a single element. So we're not even you know causing something on here. This is because of the square brackets. This is another entire linked list. This is another allocation right here. or just insert this in the middle. Another plus+. So there's another concatenation of another linked list. And that link list also comes from another recursive sort. And then of course filter is also capable of uh removing allocations and causing you know more memory traffic because the whole point of it is you're potentially dropping things out of the linked list and then needing to make other um elements to to come in. So

honestly, this function is about as far from actual quicksort as you can get. But it's not only named quicksort, which to me is ridiculous, but also it's being pitched as like, oh, look how great functional programming is. I actually gave a talk back in 2021 about it was mo mostly focused on the the rock programming language which I've been developing for several years and benchmarking um a functional implementation of quicksort with tail recursion which actually does compete you know competitively with um with real quicksort in like C++ and uh and we did in go and and Java and JavaScript and actually measured the numbers. Um, if you try to put this in that comparison, you basically can't because we were quick sorting, I think it was a million 64-bit integers or floatingoint numbers.

And if you try to do that on this, you you just it will not terminate or or will crash or run out of memory or blow the stack or like this is just not quicksort. And we should not be calling it quicksort. We should not be using this example for anything to be totally honest. I mean, I think if the idea that this is a pitch for functional programming is completely misguided. This is a pitch against functional programming because it gives the impression that we don't understand how performance works that we would think that code like this is quicksort or is quick or is a good idea at all. It's a terrible idea. No one should do it. And frankly, I think we should all stop sharing this example around. Okay, let me get off my soap box about quicksort.

So, let's [clears throat] talk about some actual functional specific performance. uh and then we can talk about some sort of functional agnostic performance and how we can put the two together with mechanical sympathy to make functional programming code that actually runs fast. We can start competing with these uh Rust and Go programs. So this is the uh the blog post that I think inspired what ended up being uh Elm's blog post. Um, so this is by David Nolan, who's a a maintainer of Closure Script, and he basically talks about how if you're doing this very popular style of UI rendering that was popularized by React, uh, where you've got this concept of a virtual DOM, without getting into too much detail, um, what he figured out was that there was a very very valuable performance optimization for this style of UI that lots of people want to do if you're using persistent data structures. In other words, because of immutable data.

Now, persistent data structures like the link list we were just talking about that have all these heap allocations. On the one hand, like I said, that does by default have some performance penalty, but it does mean that you can do before and after comparisons between one rendering and the next rendering to see if anything changed very very quickly, which is to say you can just check to see if the memory address changed. If you're doing in place mutation, whether or not the memory address changed is of no help to you because like, yeah, well, the memory address didn't change, but the memory that it points to might very well have changed because we're doing in place mutations. But if you're not doing in place mutations, and instead you're doing persistent data structures, where the only way that they can change is with a new allocation, which means a new memory address, suddenly asking the question, did this change or did this entire subtree of my data model change, is just a 64-bit integer comparison.

you're just comparing two memory addresses, it's extremely extremely fast to answer that question. And as it turns out, if you can answer that question very very quickly in this style of UI rendering, you can skip doing a massive amount of work from one frame to the next. So David figured this out through Closure Script and then Evan Shiplicki who made Elm figured out a way to apply this to Elm, which of course also does persistent data structures and that was where the blazing fast HTML blog post came from that got me into Elm. So this is a great example of taking something which out of context in the abstract is a performance downside namely that persistent data structures do a lot of heap allocations and turning it into a benefit that actually can make you outpace popular alternatives like react because it's so much of a benefit that it more than overcomes the the baseline downside. Um now there's also some sort

of functional um agnostic performance. So these are techniques that are sort of just bread and butter mechanical sympathy making your programs run faster that don't necessarily have anything to do with persistent data structures or other things that you find in uh functional programming. So this is from the uh that warp blog post that I talked about earlier. Um they talked about like how did we get warp to compete with engine X and they're basically just like look these are just things that are always a good idea. So issuing as few system calls as possible. This is talking about like uh at the very low level when you're you know implementing the web server sis calls you know where you have to go to the operating system and do a full context switch the CPU has to save all the registers and then into memory and then switch back into the operating system kernel and then do the work and then restore all the registers from memory. Every time you switch back and forth from operating system land till user land that's expensive. And so they're just like hey can we figure out how to make warp you know minimize sys calls which of course engine x is doing too. So if they wanted to compete with EngineX, they needed to do this sort of bread and butter just ordinary performance optimization. Specialized functions and avoiding recalculation.

Again, just bread and butter. Do less work. If you can do less work, generally speaking, program is going to run faster. Avoiding locks. We're going to talk a little bit about locks later. So this is like mutxes and things like that. Um again, like they have a cost uh whether or not there's contention and of course they can also cause contention which can slow things down even more. So figuring out a way to design your system to avoid locks whether it's functional or imperative is a good way to improve performance. And then finally using proper data structures. So byte string over string. We're going to come back to this example again later because this is a pretty important one to think about.

Byte string for those who aren't familiar with Haskell um is basically different from string in that string in Haskell is a linked list of Unicode characters and byte string is a flat array of bytes. So the important difference here is that string has the the usual characteristics you would associate with a persistent data structure like a link list in terms of like appending is very cheap never requires a re reallocation although it does require a heap allocation you don't have to like reopy the entire contents byte string is the opposite byte string is more like what you would find in most imperative languages and the reason that they say using proper data structures is that in practice it turns out that this is just not a case where using a linked this is a good idea. Um, you know, in the case of Elm and Closure Script, this did, you know, using link lists turned out to be a big benefit. Generally speaking with strings, I've heard of almost no use cases where a link list is what you want. So for this use case, I think they're right in saying it's it's a proper choice to use byte string with a flat array rather than string with a linked list. Okay. So which brings us to

this concept of mechanical sympathy. So the idea of thinking about what does the hardware want? like what does the hardware do well with? Even though from a program architectural perspective, we want to be as zoomed out as we can be. We want to be thinking about the high level these functional concepts, composing functions and things like that. At the end of the day, performance is kind of driven by what the hardware wants and what the hardware likes. And so mechanical sympathy is this idea of being aware of and thinking about what does the hardware want even as we are architecting our programs in a functional style trying to keep both in our heads at the same time because at the end of the day that's kind of the only way to get performance. So this comes from a Formula 1 driver apparently um the quote was you don't have to be an engineer in this case like a race car engineer to be a racing driver but you do have to have mechanical sympathy. So what he's saying here is essentially that, you know, you don't need to know how to build the car. You don't need to be like an actual, you know, person who builds the racing cars themselves, but if you don't understand how the car works, if you don't understand like the low level of the hardware, it's going to be really hard to be effective as a driver of that car because you're not going to know what to do to make the car go as fast as possible. It might be nice to think that we could, but in practice, it just doesn't work that way if you actually want to really go fast. So I would adapt this to programming to be you don't have to be a hardware engineer to make a slow program into a fast program. But you are probably going to need mechanical sympathy. In other words, you are probably going to need to understand what the hardware is doing at a low level. Even if that's not going to be driving every single individual choice we make in our programs if we want them to go fast, we do need to be aware of this stuff and actually be thinking about it.

Unfortunately for us, hardware is extremely imperative. So as a very quick case and point of this, this is the CMP instruction. a comparison instruction like the CPU instruction. I've got a link to the x86 uh reference book or n x86 reference book and uh here's a quote from it. The CMP instruction compares the first source operand with the second source operand. So we're comparing two integers and sets the status flags in the E flags register according to the results. In other words, even when we're just comparing two things, which is about as readonly a concept as you can get. How does the CPU do that? It looks at the two things, compares them, and then mutates a register to store the

result. This is just how CPUs work. I mean, basically, you could think about it as like CPUs read and mutate the equivalent of global variables because semantically registers are basically global variables. There's just one global namespace of them. There's a fixed hard-coded number of them. And every single CPU operation that you do is just going to be mutating one of those. It's going to stomp over whatever result was in there before. There's no thread protection on any of this stuff. This is just unfortunately the reality of what CPUs do. They read and mutate global variables all the time. Um all control flow in a CPU is done with go-tos. So this is whether it's a conditional jump or an unconditional jump. Those are all just go-tos. Go to is syntax sugar for the thing that the uh the processor does which is a jump.

Um this is how all control flow is done. It's as far a cry as you can get from the functional style. And of course functions themselves don't exist. functions were a concept that we made up and then sort of implemented on top of these hardware primitives that we have access to. But actually in the earliest days of programming, people didn't use functions because they hadn't figured out a way to make you originally they were called like sub routines or procedures and things like that. Um but certainly they didn't have the concept of like closures with captures and lambdas and things like that because the hardware just doesn't have those concepts. So, if we're going to have mechanical sympathy, unfortunately, this does mean that we're going to have to be aware of some of these concepts that are extremely imperative even though we're architecting our programs in a functional style.

Um, one of the one of the most impactful things that I ever read in my entire programming career um is this numbers every programmer should know. The original chart uh was from 2003 by Jeff Dean and there's a 2020 update from uh Colin Scott which is what I'm going to use. I'm going to use the 2020 numbers here. Um these are not necessarily like exactly perfectly measured numbers. But the the really critical part here is the relative sort of uh orders of magnitude the the shape of the growth of these numbers. So, I want to just go through these numbers really quick because I think they're really really instructive and really they really changed my understanding of what matters and what's fast when it comes to uh like the lowest level hardware. So, basically like all right, [clears throat] one nanocond for an L1 CPU cache hit. We're going to talk about CPU cache in a second in a little bit more detail, but this is basically like if I want to read something out of memory and that value in memory is actually on the CPU's little memory cache. it's much smaller than your actual gigabytes of memory in in your machine. Um, this takes about one nanocond to read that value out of there. Um, a branch misprediction, which is basically where you have a conditional, so like an if and an else.

And the CPU did not correctly guess ahead of time whether it was going to be an the if path versus the else path. Um, CPUs do actually try to guess that ahead of time. And there's some advantages uh if it guesses correctly. But if it guesses incorrectly, that's going to cost about three nonds. triple the amount of time that it takes uh to to get a cache hit in this L1 CPU cache. If it mispredicts if it if it predicts it correctly, then oh, you're you're you're in great shape. It's going to be much cheaper than that. Um 4 nconds for an L2 CPU cache hit. So there's usually three uh levels of CPU cache. So L1 is the fastest one. It's the closest to the core like to the um to the registers.

And then CPU L2 is bigger. It has more

memory, but it's a little bit slower. So four nconds versus 1 ncond. So four times as much if it couldn't find the value it was looking for in L1, but it did find it in L2. That's a 4x penalty. By the way, before I go any further in this chart, I want to note that I didn't actually put arithmetic on this chart. Uh because like things like plus and minus and compare are so cheap that in many cases the CPU can actually just find time to do them while waiting for more expensive instructions. So they are literally free in the sense that they actually do not increase the runtime of your program. Like in some cases you could actually write a program and you could just add a little plus instruction in there, add a little minus instruction in there and it literally would not change the runtime of your program because the CPU just finds time to do it while waiting for something um more expensive like going to memory. So yeah, those arithmetic is uh the meme that I've

Related videos

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 537 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks