When did the CPU cache line turn red after writing?

I work in C # and want to avoid unsafe code if possible. If I have an object or an array whose size is filled for the cache line, and I want to write in each field of the object or the index of the array, will the CPU wait until all entries occur before you clear the written line or will it be reset earlier, when only one or several records occurred?

If I want the reset to occur only after all the entries in the string have occurred, should I do all the entries at the end of the procedure in quick succession? I know that CPU and cache negotiation protocols may differ from this, I'm looking for a general answer for the correct rule.

+6
source share
2 answers

Will the processor wait until all records are completed before being written to the line, or will it be reset earlier when only one or several records have occurred?

The CPU can start the line earlier, but only if this set is under high pressure from another access belonging to the cache. This is unlikely. Caches are structured to avoid prematurely flushing recently acquired data.

Do I have to make all the entries at the end of the procedure in quick succession?

In general, yes. Temporal locality is important, which means that caches work best when access is grouped by time. Other tricks may apply. For example, you can try to β€œheat” the cache line by fictitiously writing to your structure to the desired record. This allows some level of parallelism memory to be used, in which the kernel loads a cache line when executing intermediate code. By the time you are making real entries, the likelihood that the cache line will be ready in L1 will be better.

In general, be very careful about unnatural actions in your code to improve cache performance. Caches do a pretty good job, left only to themselves. You should always measure performance before and after any changes. What you think could be an improvement could hurt. If your program has multithreading, another large worm of worms with a cache conflict between the kernels may enter the game.

+2
source

Naturally, the processor will try to make as little access to memory as possible, but this does not necessarily mean that "your" memory block will be stored in the cache for as long as you want it.

Typically, a memory block is read once and written once, but there is no guarantee. Something may happen that interrupts your code, and the system may decide to flush this cache line to free up space for something else. The entire memory area can be completely removed from memory and flushed to disk, so when your code runs, it will crash the page, which will load the memory again.

Having your emails closer to time will, of course, make it more likely that the memory will be stored in the cache throughout this operation.

+1
source

All Articles