Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

memcpy and CPU cache

326 views
Skip to first unread message

Rudolf Meier

unread,
Jun 14, 2008, 9:52:28 AM6/14/08
to
Hi

Just a little question... I have to hold a sorted table in the memory. Now,
I allocate the memory using VirtualAlloc (or shouldn't I?) and then I'm
spliting up my data structure into 4kB pages. Now when I have to move almost
the whole table one position to the back, so that I can insert a new element
in front, I use memcpy. The question now is, if this strategy is ok from a
performance standpoint. My expectation is, that most of the work can be done
in the CPU cache. But I'm not shure... so, my questions now:

1) does a CPU always have the memory that it is currently using in it's
cache? (at least the page that is accessed at this moment?) ... I'd say
yes... but I didn't find a clear indication that I'm right

2) is it ok to allocate memory using VirtualAlloc? ... someone told me, that
this could be a problem, since Windows does never move these memory parts
from the RAM to the HDD (swap file) which could cause problems...

3) is there a bether way to move memory in one memory page? or is this
already the most optimized way to do this??

thanks for infos
Rudolf

Bo Persson

unread,
Jun 14, 2008, 11:26:28 AM6/14/08
to
Rudolf Meier wrote:
> Hi
>
> Just a little question... I have to hold a sorted table in the
> memory. Now, I allocate the memory using VirtualAlloc (or shouldn't
> I?) and then I'm spliting up my data structure into 4kB pages. Now
> when I have to move almost the whole table one position to the
> back, so that I can insert a new element in front, I use memcpy.
> The question now is, if this strategy is ok from a performance
> standpoint. My expectation is, that most of the work can be done in
> the CPU cache. But I'm not shure... so, my questions now:
> 1) does a CPU always have the memory that it is currently using in
> it's cache? (at least the page that is accessed at this moment?)
> ... I'd say yes... but I didn't find a clear indication that I'm
> right

It works on chunks much smaller than the page size. Exactly how the
cache is organized varies between CPU models.

>
> 2) is it ok to allocate memory using VirtualAlloc? ... someone told
> me, that this could be a problem, since Windows does never move
> these memory parts from the RAM to the HDD (swap file) which could
> cause problems...

No problem.

>
> 3) is there a bether way to move memory in one memory page? or is
> this already the most optimized way to do this??
>

Not moving it at all is of course the fastest. Pehaps you can just
allocate a new page for the new data?


Bo Persson

Barry Schwarz

unread,
Jun 14, 2008, 5:13:24 PM6/14/08
to
On Sat, 14 Jun 2008 15:52:28 +0200, "Rudolf Meier" <me...@gmx.net>
wrote:

>Hi
>
>Just a little question... I have to hold a sorted table in the memory. Now,
>I allocate the memory using VirtualAlloc (or shouldn't I?) and then I'm
>spliting up my data structure into 4kB pages. Now when I have to move almost
>the whole table one position to the back, so that I can insert a new element
>in front, I use memcpy. The question now is, if this strategy is ok from a

If the two operands passed to memcpy overlap, the behavior is
undefined. In this situation, you need to use memmove.

>performance standpoint. My expectation is, that most of the work can be done
>in the CPU cache. But I'm not shure... so, my questions now:

Given the wide variety of CPUs in use, I would be very surprised it
either the compiler or the run-time library made any effort to deal
with cache.

>
>1) does a CPU always have the memory that it is currently using in it's
>cache? (at least the page that is accessed at this moment?) ... I'd say
>yes... but I didn't find a clear indication that I'm right

In a multi-tasking system, you have no idea how may instructions the
CPU will execute between two consecutive instructions in your code.
Whether or not the memory referenced by instruction 1 of your code is
still in cache by the time instruction 2 executes is unpredictable.

>
>2) is it ok to allocate memory using VirtualAlloc? ... someone told me, that
>this could be a problem, since Windows does never move these memory parts
>from the RAM to the HDD (swap file) which could cause problems...

Was that someone an expert on the internal functioning of Windows?
Which version of Windows was he referring to? Which Service Pack?
What options did the Administrator choose?

>
>3) is there a bether way to move memory in one memory page? or is this
>already the most optimized way to do this??

Have you done any testing to determine if this concern has any
noticeable impact on your programs performance? Premature or
inappropriate optimization frequently causes more grief than benefit.


Remove del for email

0 new messages