This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
We concluded the latest live session yesterday. Unfortunately, I forgot to start recording until we were already 10-15 minutes into the session, so the initial part is missing. But nothing significant was lost. The recording starts just at the right time when were doing an overview of the CPU microarchitecture.
We covered how things work at the hardware and kernel level in the context of the following optimizations used in 1BRC:
* Read vs mmap performance
* Static vs dynamic allocation of work in a multithreaded design
* Thrashing of the L1 cache when dealing with shared data between threads
* Design of a cache friendly hash table
Due to lack of time, we could not cover discussion of branch prediction related optimizations, but we did talk about them in the previous session, catch it at the following link
PS: This was the first time I was using my iPad to draw things on the Zoom whiteboard and for some reason it kept freezing while drawing. I will try to find better alternatives to do this for the future sessions.
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_01Yeah. So if you have a hit in the TLB cache, we'll probably be able to access it in around one cycle. Whereas if you have a TLB miss, then you have to do a whole page table walk. And In Linux, the page table these days is four to five levels, depending on which version of Linux you are on. So four level page tables means you have to access four different
SPEAKER_01pieces of data in the main memory. So all those four levels of the base table are somewhere in the physical memory. And then you have to go and read those levels to be able to translate and address. So if your base table is four levels long, you have to do four memory accesses to translate and address. And one read access to the main memories takes about 100 cycles. So when you have to do full-page table walk. In the worst case, it can be 400 cycles to do a translation.
SPEAKER_01So it is amongst all these things, a TLB miss is the most expensive thing that can happen to your program. So you don't really want to have a TLB miss, which is why this cache is very important. Okay. Now I'll try to get into the specific details of these things
as we talk about the
SPEAKER_01specific optimizations. So we are not going to go deeper right now, but we'll try to interleave things as we discuss the 1brc optimizations. Before we start to talk about that, are there any questions about this part? Anything which is not clear? I sort of gave an overview rather than...
detailed explanation so but you can feel free to ask anything which is you want to
SPEAKER_01discuss in more detail because this is more like an open session.
SPEAKER_00Yeah, so I was wondering that you mentioned the three main optimizations is like the read syscall versus memory map and the branch and cache. So I understand the branch and cache. So how is that, so is it like, is the, how that reads is called, no, how the map is better than reads is called, is it related to how this, how the, everything works here?
SPEAKER_01Yeah, it sort of does in a way. So let's talk about that. I think that's an interesting thing to talk about. Um, so before I talk about read versus mmap, let me ask, uh, is everyone familiar with, um,
SPEAKER_01what mmap, uh, is, uh, how mmap works? I am pretty sure, uh, people know how to read a file, but mmap sort of some people are unfamiliar with. So Okay. Yeah, sure. So let me try to first give a high level overview of read over say a map, then we'll go into the detail of the underlying details,
SPEAKER_01which describe the trade off between the two. So I have actually for this, I can use a slide because writing on zoom is not really working great. Let me try to show the slides.
SPEAKER_01Okay. Uh, OK, so in most Unix-like systems, you have two system calls available to read a file. So one of them is the read system call.
So this is the signature of that system call if you want to call it using C.
SPEAKER_01So you have to give the file descriptor. So when you open a file, this is the file handle. In a way, you can think about it as a file handle that the operating system knows which file you are trying to read here. And then you have to give the buffer where you want the operating system to read and store the bytes that it reads from the file. This is where it will write. And then you have to tell it how many bytes that you want the operating system to read.
SPEAKER_01And it is a system call. So it is implemented within the operating system. So basically you are asking the operating system to go and read the file and write the result into this buffer. uh, in any programming language, whatever library that they provide to read a file is usually is a wrapper around the system. So for example, if you're familiar with Java, Java has a file reader class and you wrap around using a buffer data and underneath
SPEAKER_01all of that, whenever you use the preferred reader to read some number of bytes from that file underneath, it is always going to call the read system call to ask the operating system to read that many number of bytes. And Making a system call is an expensive thing to do. I'll explain in a bit why is it expensive, but take my word for it that when you have to do system calls,
they are more expensive than any instruction that you're trying to execute right
SPEAKER_01within your process. I mean, in the user space. So you don't want to do too many system calls. So in most programming languages, they will have a buffer implemented. So essentially, when you say in Python or Java or whatever, you say, I want to read some number of bytes on the file. the programming language itself will ask for more number of bytes from the operating system. So for example, if you in Java, you say, I want to read 10 bytes from the file,
but underneath it will go and ask for 8,000 bytes from the file if the file is that long.
SPEAKER_01The reason is that the program, it doesn't really cost too much. If you're going into the operating system, you might as well ask for more data. And even at the operating system level, even if you're asking for 10 bytes of data, the operating system is going to read one page of data, which is four kilobytes. Anyway, the operating system is going to read that many bytes from the file system. And you might as well transfer all of that data into the user space so that you don't have to make another system call in the near future.
So every programming language has some sort of buffer where it caches, extra number of bytes.
SPEAKER_01So that is what really happens. And whenever that buffer gets consumed totally by the application, then the next time you call read, again, the programming language runtime is going to make a system call the whole number of bytes. So that's how it works. And then you have an alternative. So there is another system call, which is called mmap. mmap is short for memory mapping.
SPEAKER_01So essentially, you can think of it like you are saying that map the bytes of the file into the memory of the process. So the memory of the process is organized as a bunch of virtual memory pages. And you are saying that just allocate a bunch of pages, and those pages are essentially backed by the file data.
SPEAKER_01And each of those pages has some sort of an address. And once you call a map, you will get hold of that address of the starting where those bytes of the file have been mapped. And then you can start reading from that address as if you created a byte array in your program or you just allocated an array at that address and you just start reading bytes at that array, from that array. And underneath,
whenever you access a byte at that address or at some offset from that address,
SPEAKER_01the operating system is going to load those bytes from the file, bring those into the address space of your process, and then give it to the application. So this has the advantage that you can specify beforehand that how many bytes that you want to map from the file and from what offset. So only those bytes will be mapped into the memory of your process. And after those pages have been mapped,
then you access those bytes as if you have eliminated
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...