user602774
user602774

Reputation: 1103

are array initialization operations cached as well

If you are not reading a value but assigning a value

for example

int array[] = new int[5];

for(int i =0; i < array.length(); i++){
array[i] = 2; 
}

Still does the array come to the cache? Can't the cpu bring the array elements one by one to its registers and do the assignment and after that write the updated value to the main memory, bypasing the cache because its not necessary in this case ?

Upvotes: 0

Views: 70

Answers (1)

twain249
twain249

Reputation: 5706

The answer depends on the cache protocol I answered assuming Write Back Write Allocate.

The array will still come to the cache and it will make a difference. When a cache block is retrieved from it's more than just a single memory location (the actual size depends on the design of the cache). So since arrays are stored in order in memory pulling in array[0] will pulling the rest of the block which will include (at least some of) array[1] array[2] array[3] and array[4]. This means the following calls will not have to access main memory.

Also after all this is done the values will NOT be written to memory immediately (under write back) instead the CPU will keep using the cache as the memory for reads/writes until that cache block is replaced from the cache at which point the values will be written to main memory.

Overall this is preferable to going to memory every time because the cache is much faster and the chances are the user is going to use the memory he just set relatively soon.

If the protocol is Write Through No Allocate then it won't bring the block into memory and it will right straight through to the main memory.

Upvotes: 1

Related Questions