Michal Czardybon
Michal Czardybon

Reputation: 2865

How to force keeping some memory buffers in cache?

I have a program composed of two parts:

  1. a virtual machine of a graphical programming language,
  2. image processing routines.

The problem is that the virtual machine works fast enough as long as there are no big images processed. The drop of the performace of the virtual machine is about the factor of 5 after processing of a big image. I guess this is because memory buffers of the objects belonging to the virtual machine get removed from cache when a big image comes. Normally, a processor keeps a separate cache for code, separate for data, but not when my program is interpreted.

QUESTION: Is there any way to make it the same for interpreted code, i.e. to mark somehow a memory buffer as high-priority for cache memory, or to allocate somehow a memory buffer that will be guaranteed to stay in cache?

Let me add, that although image processing is much slower than intepreting programs, there happen to be cases when the second part becomes critical - think for example of postprocessing a set of points detected on an image - these are simple arithmetical operations that are too slow on a virtual machine then.

Upvotes: 2

Views: 1355

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294307

There is no pixie dust.

You need to write your code very carefully, in a cache friendly manner. Have a look at CPU Caches and Why You Care, absolutely positively get a copy of The Software Optimization Cookbook and read it carefully end-to-end.

As a side, OS platforms allow for process memory to be pinned (not swappable, which is a differnet topic from L2 code/data caching, but you're quite far from proving that L2 cache is the culprit in your case anyway...) but 101% cases the OS knows better than your app and preventing it from swapping results in worse performance, not better.

Upvotes: 2

Related Questions