๐Ÿš€ ๐—– ๐—–๐—ฎ๐—ฐ๐—ต๐—ฒ ๐—ข๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป: ๐—•๐—ผ๐—ผ๐˜€๐˜๐—ถ๐—ป๐—ด ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ! ๐Ÿš€

Apurv upadhyay
2 min readOct 27, 2024

--

Cache optimization is a powerful technique to make your C programs run faster. Here are a few practical tips:

1๏ธโƒฃ ๐—”๐—ฐ๐—ฐ๐—ฒ๐˜€๐˜€ ๐——๐—ฎ๐˜๐—ฎ ๐—ฆ๐—ฒ๐—พ๐˜‚๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น๐—น๐˜†

โ€ข Access memory in a linear fashion to make the best use of the CPU cache.

  • Example: Iterating through arrays from start to end is much faster than random access!
for (int i = 0; i < size; ++i) {
array[i] = i * 2; // Accessing elements sequentially for better cache utilization
}

2๏ธโƒฃ ๐—ฆ๐˜๐—ฟ๐˜‚๐—ฐ๐˜ ๐—ฃ๐—ฎ๐—ฑ๐—ฑ๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐—”๐—น๐—ถ๐—ด๐—ป๐—บ๐—ฒ๐—ป๐˜

โ€ข Arrange struct members to minimize padding and maximize cache line efficiency.

  • ๐—ง๐—ถ๐—ฝ: Place smaller variables together and align the largest ones.
#include <stdio.h>

// A struct that demonstrates proper alignment for cache efficiency
struct Optimized {
char a; // 1 byte
// 3 bytes padding here for alignment of the next int
int b; // 4 bytes (aligned after 'a' for efficiency)
double c; // 8 bytes
};

// Function to demonstrate usage of the Optimized struct
void processStructs(struct Optimized* array, int size) {
// Accessing structs sequentially to make the best use of cache
for (int i = 0; i < size; ++i) {
array[i].b = i; // Modify integer value
array[i].c = array[i].b * 2.0; // Modify double value based on 'b'
}
}

// Function to print the struct contents
void printStructs(struct Optimized* array, int size) {
for (int i = 0; i < size; ++i) {
printf("Struct %d - a: %c, b: %d, c: %.2f\n", i, array[i].a, array[i].b, array[i].c);
}
}

int main() {
int size = 5;
// Allocate an array of Optimized structs
struct Optimized array[size];

// Initialize the structs
for (int i = 0; i < size; ++i) {
array[i].a = 'A' + i; // Assign letters starting from 'A'
array[i].b = 0;
array[i].c = 0.0;
}

// Process the array of structs
processStructs(array, size);

// Print the contents of the structs
printStructs(array, size);

return 0;
}

๐Ÿ” Code Explanation:

1. Struct Alignment:

โ€ข struct Optimized has fields arranged to minimize padding and align data efficiently:

โ€ข char a; (1 byte) is followed by 3 bytes of padding to align the int b field.

โ€ข int b; (4 bytes) is placed after the padding for alignment.

โ€ข double c; (8 bytes) follows b as it naturally aligns to an 8-byte boundary.

2. Sequential Access:

โ€ข The processStructs function accesses and modifies the structs in a loop sequentially. This makes the most of cache efficiency because elements are stored contiguously in memory.

3. Initialization:

โ€ข The main function initializes an array of struct Optimized with size 5 and assigns values to each structโ€™s fields.

โ€ข The processStructs function then processes the array to set values for b and c based on calculations.

4. Printing the Structs:

  • The printStructs function outputs the values of each struct in the array to verify the changes.

โค๏ธ Share Your Thoughts!

Feel free to repost โ™ป๏ธ if you found this helpful. For more great content like this follow ๐Ÿ›  Apurv Upadhyay. Until next time, happy coding! ๐Ÿš€

#HTTP #C #WebDevelopment #StatusCodes #ClientServer #DeveloperTips #WebStandards #APIDevelopment

--

--

Apurv upadhyay
Apurv upadhyay

Written by Apurv upadhyay

Principal Software Engineer at PeerIslands โ€ข Microsoft Azure Certified Architect Expert & DevOps Specialist โ€ข 7x Azure Certified โ€ข ex-Microsoft, Bosch