translate the following content into chinese: In my previous video, I talked about how…
- User
translate the following content into chinese: In my previous video, I talked about how providing the size of your variables in your code can help compilers speed up your programs. I ended that video by mentioning that you might want your types to grow or shrink dynamically at runtime. But that's a problem because, in low-level programming languages, compilers complain when you are not explicit about sizes. So, you can't declare custom types with arrays, unless you give the array a specific size. That means your array will have that size during the entire lifetime of your program. By the end of this video, you will understand the reasons behind this limitation. Today, we are going to take a look at the stack. That memory region that everybody tells you is super fast, and you should always try to put data on it, but almost nobody tells you why. A stack is a data structure that follows the last-in-first-out principle. This means that the last element added to the stack is the first one to be removed. Usually, its size is limited, so if we push too many elements and run out of space, we say the stack has overflowed, which may sound familiar to you. Although recently the site is dead due to the rise of large language models, like me. I mean, like GPT-4. Anyway. The stack in this video is not a software stack, although there is a relationship, as you'll see. What we are studying right now is the execution stack, sometimes called the program stack, or even the hardware stack. When we run a program, theoretically, there is nothing preventing us from writing our values anywhere in memory. Even though we are specifying the size of our values, randomly placing them could result in wasted space. To comprehend this, it's crucial to consider the role of the operating system. You might be familiar with the concept that the operating system always acts as an intermediary between programs and hardware. Well, memory is no exception. When a program is executed, it cannot simply begin storing data anywhere in physical memory, as other programs might already be utilizing some of that space. Therefore, before, it must request memory from the operating system. The operating system, in turn, will search for available spaces where our program can write. If we try to read from or write to a memory address that the operating system hasn't allocated to our program, it will promptly terminate the program's execution for safety reasons. This is when we encounter the infamous error known as "segmentation fault, core dumped," from which this channel derives its name. The important part here is that the operating system doesn't allocate memory precisely to the amount we need. Instead, it provides a memory chunk where we can freely read and write. However, if we don't organize our data and just place it anywhere, we might face this issue where there is sufficient free memory, but we can't store data due to the lack of contiguous space. This problem is commonly referred to as "external fragmentation." In this case, the only way to store our value in memory is to request another chunk. If we had organized our data more efficiently, there would be no requirement to request additional memory. It's worth noting that asking for more memory can be considerably expensive in terms of performance. As a brief preview, this is why many recommend minimizing the use of the heap. We'll delve deeper into this topic in my upcoming video about the heap, so consider subscribing for more details. Furthermore, when the operating system allocates memory to our program, it doesn't track whether we utilize that memory or not. Its sole awareness is that the specified memory region belongs to our program. If another program requires memory, it will have to put its data somewhere else, even if there is enough free contiguous memory in our program memory region. But memory is finite. So, what do we do if all the memory is already in use by other programs? Once again, the answer lies with the operating system. Modern operating systems are specifically designed to address this type of issue. In the absence of available memory, they leverage storage space as if it were additional memory. Have you ever seen that partition called "swap" in Linux systems? Well, now you know what it does. This is what we call "virtual memory," and it is very useful because it not only creates the illusion of having much more memory than is actually present, but it also gives the appearance of writing directly to physical memory. So as programmers, we don't have to worry about all the underlying concepts. I could spend 30 minutes talking about virtual memory, but that's out of the scope of this video. However, it's important to note that relying on storage to extend main memory is a double-edged sword. Storage is thousands of times slower than RAM, and this feature that operating systems provide should be used with caution. In my previous video, someone commented that in modern days, the size of our data in memory might not seem crucial due to the virtually unlimited memory available. While it's true that memory appears limitless for us as programmers, it's essential to exercise caution and consider that performance still plays a significant role. And speaking of performance, it turns out that even memory can be a bottleneck. Despite the immense speed of modern CPUs, fetching data from memory incurs a noticeable cost. To address this, manufacturers integrate a feature called cache into modern chips. Cache is essentially a small, dedicated memory inside the processor where a copy of a memory region is stored. This way, when the CPU requires data for manipulation or an operation, it can promptly retrieve it from the cache instead of searching for it in the main memory. When the necessary information is present in the cache, we refer to it as a cache hit. However, if the required data is not in the cache, it results in a cache miss. In such cases, the CPU is compelled to wait for the data to be fetched from the main memory, incurring additional time. Once again, this is completely invisible to us, the programmers. In this case is the hardware and not the operating system who decides what memory region is kept in cache. If we care about performance, the best we can do is to keep our data as compact as possible. This way, there is a higher probability that it will fit in the cache. This proximity increases the chances of a cache hit. This new concept of keeping data as close as possible to the CPU is what low-level people know as locality. And there you have the second reason to keep data compact, performance. Now you might be thinking, "What the hell all of this has to do with the stack?" Well, it turns out if we want to keep data compact, a good way to organize things is using a stack. Let's take a look. The first thing we can do to speed things up is to pre-allocate memory when our program starts. This way, every time we need to store some value, we already have somewhere to write it. Since our goal here is to avoid requesting memory from the operating system on the fly, the size of this pre-allocated region remains fixed throughout the entire lifetime of our program. Therefore, we must be careful when determining its size. While there's nothing preventing us from allocating gigabytes to this region, it's essential to consider the simplicity of our program. If it only uses a couple of variables, it may never fully occupy such a large memory space. As mentioned earlier, this would result in memory wastage, as the operating system would reserve that free space for our program, preventing other programs from utilizing it. For this reason, the size of this pre-allocated region is typically kept small. The exact size depends on the compiler we are using, but generally, it won't exceed a few megabytes. This might seem insufficient at first glance, but if you do the math in only one megabyte, you can fit more than 130,000 64-bit integers. When our program starts, the main function serves as our entry point. Each time a new variable is encountered, its value is stacked into this designated region. This is where the term "the stack" becomes apparent. The memory address marking the beginning of the stack is referred to as the stack origin, while the stack pointer indicates the current topmost datum on the stack. Of course, the stack pointer is stored somewhere. It can be done in memory, but modern architectures incorporate a register exclusively dedicated to holding the stack pointer value directly within the CPU. Consequently, the processor doesn't need to wait for it to be fetched from memory, or even from the cache. One of the reasons why memory allocation on the stack is highly efficient lies in the constant guidance provided by the stack pointer. To write something onto the stack, all that's required is to fetch the stack pointer address, add one to that value, and the result is the memory address where we can write more data, and that's it. That's all it takes to allocate memory on the stack. As you'll discover in my upcoming video, this process is not as straightforward when allocating memory on the heap. In that scenario, we must request memory from the operating system, wait for it to locate available space, and due to the system not returning the exact amount of needed memory, we must employ fancy strategies to avoid fragmentation. And all of those are extra steps that make the process slower. When a function is called, all its local values are pushed onto the stack. But it's crucial to remember the starting point of these local values because, when the function concludes, we need to reset the stack pointer to its position just before the function calls. These distinct sub-regions are known as stack frames. When a function being called is expected to return a value, such as in this example, we allocate a distinct space called the return link. Following this, we start pushing our local values. From the function's conclusion, the return value is directly written into the return link. Subsequently, the stack frame of the callee function is removed, except for the returning value, which now integrates into the stack frame of the caller function. Here, it's important to note that, for the compiler to generate efficient machine code, it requires knowledge of the exact size of the return link. This is why systems programming languages mandate specifying the size of the value a function is returning, which you do by providing a return type. Finally, let's consider some important limitations. In this example, we write some values in the stack, a number, an array, then more numbers, and let's say then we want to add some elements to the array. See the problem here? Adding elements to an array on the stack means we might have to overwrite other values that we might need later. This is exactly what I talked about in my previous video. The compiler will complain if you don't specify the size of any array, even if the array is a member of a custom type. Sorry if I made you wait a lot for this answer, but I think everything I've talked about up to this point was necessary to understand this. When you specify the size of an array, the compiler gains precise knowledge about the amount of space required to accommodate that value in the stack. But to the finite size of the stack, even with provided sizes, exceeding this limit can lead to a program crash, resulting in a stack overflow error. This scenario is particularly pertinent when dealing with recursion. When a function calls itself, new stack frames are successively pushed into the stack for each recursive call. If a base case isn't defined to halt the recursion, the program will keep adding stack frames until it overflows. Furthermore, even with a base case, a stack overflow can occur if the program doesn't reach it before surpassing the stack's capacity. Hence, there are instances where using iterative solutions is recommended over recursive ones. It's essential to clarify that the behavior of the stack is influenced by the programming language and its compiler. What you've seen in this video might not precisely mirror the implementation in every usage, therefore it's important to understand that I aim to simplify concepts for the purpose of illustration in these videos. In summary, today we learned that maintaining compact data is crucial for reducing memory usage by avoiding fragmentation and enhancing performance through an increased probability of cache hits. Specifying the size of our variables enables compilers to organize our data more efficiently and predictably in the stack. This organization facilitates the handling of function calls and local variables in a seamless and speedy manner. We also delved into the limitations of the stack. It possesses a size constraint and lacks the flexibility for types to dynamically grow or shrink. Additionally, the stack operates as a single-threaded entity. In the context of multithreading, each thread necessitates its own stack, posing a limitation when attempting to share memory between threads. All these challenges find a common solution in what is known as "the heap," a topic that we will explore in a future video. And that is a very, very simplified version of how the stack works. Hopefully you now have an idea of why it is so fast.
- Assistant