![]() ![]() This padding in the layout can make data structures significantly larger. The compiler wastes a few bytes here and there to ensure the each value is naturally aligned - CPU time is saved, but memory is wasted. To avoid overhead due to the unaligned memory accesses, C and C++ place data at locations such that the values are naturally aligned in memory. Additionally, the processor needs to paste the bits from the two reads into a single 64-bit value which also takes time. However, if the floating point number is not aligned (unaligned), it straddles two words in memory and two read must be performed (one read for b0 through b3 and another read for b4 through b7). ![]() If the double precision floating point number is naturally aligned, the entire value starting at address+0 can be read with a single memory access (as can be seen in the diagram below.) For example, a double precision floating point value is made up of eight bytes (b0 through b7) and we assume the memory width is eight bytes. To improve the speed of memory accesses and processor performance, each memory access on modern processors deals with a larger group of bits than a single eight bit byte, typically, 64 bits (eight bytes) or 128 bits (16 bytes). This natural alignment has a cost, and it can lead to larger data structures. ![]() Using natural alignment allows the processor to avoid doing multiple memory operations to access a single value. Natural alignment describes the practice in which the address of the data type is a multiple of the size of the data type. However, to allow the processor to more quickly quickly access data stored in these basic types, the datum is placed on a natural alignment. Groups of bytes are used in C (and other languages) to form basic types such as short, int, long, float, and double. Memory in modern computers is addressable as bytes (eight binary digits). You think to yourself, "That doesn't add up! Why is the application using so much more memory?" The reason it doesn't add up is that you didn't take into account the memory space being wasted in the layout of the data structures. You do some quick "back of the envelope" calculations to estimate the amount of memory your application should be using, based on the size of each element in some key data structures, and the number of those data structures in each array. This could be preventing you from running as many containers on a single system as you expected, it could be causing performance bottlenecks, and it could even be forcing you to pay for more memory in your servers. Maybe you have so much memory in your computer that you never have to worry about it - then again, maybe you find that some C or C++ application is using more memory than expected. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |