PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

memcpy or memmove?

just another speed Q, whats faster memcpy or memmove?

just wondering, I've some buffer funcs for screen shifting (left, right,up,down etc), e.g

Code:

void move_buffer_down(int x, int y,int size, PIXEL* source)
{
int xs = x * size;
int xy = x * y;
memcpy(screen_temp_p + xs,source, (xy - xs) * 2);
memcpy(screen_temp_p,source + xy - xs, xs * 2);
memcpy(source,screen_temp_p, xy * 2);
}


and was wondering if a move would be faster / slower?

-B
hi,

i think memmove is slower than memcpy, since memmove checks for overlapping memory regions, thus adding overhead.

cryo
I can't stress how important it is to profile code when you want to answer questions like this. Guessing at implementation details is a crap shoot at best.
man, the above example is used to move a buffer (normally a screen worth of 16bit pixels) down, it uses a temp buffer to first move the top section of the source (designated by size var) to a temp location, placing it at the bottom, the remaining section in the source to the top of the temp, then it copys it all back...

e.g

assuming the buffer is 5 rasters in height, and the size to move is 1 raster:

1
2
3
4
5

would be changed to....

5
1
2
3
4

normally I'd use it to move a whole screen say 5 rasters down, for scrolling etc.... (just redrawing over the section of screen that got wrapped around, raster 5 in above example)

i.e to move a buffer that is 240x320 (76800xWd) 20 pixels down in the x axis.. (assumes the buffer is 16 bits per "pixel", hence the *2's in the function:

move_buffer_down(240,320,20,screen_buffer_p);


is that what you wanted digby?
From my reading, Digby meant:

"Test both ways and see which is faster rather than assuming one way is better".

(And I concur as always :)).

You want to look at circular buffers though (well, sort of circular buffers). If you always plot in one or two chunks, then you never actually have to move memory around - for your example, you just plot row 5 as one chunk then plot rows 1-4 after it... no movement of memory necessary. Just keep a record of the next row you need to zap/change.

E.g. (space denotes moving the address in the source)

12345
5 1234
45 123
345 12
2345 1
12345

No movement necessary.

(And if you're actually reading stuff from screen-memory to write it back to screen memory, performance is going to be abysmal because the screen isn't buffered or cached on most devices).

Cheers,

Ref.
The buffers I'm using are not the real screen buffer, I understand what you mean about scrolling buffers, and thats what I would use, but Im introducing new data all the time e.g:

12345
23456
34567
45678
56789

etc etc (the rotated section gets drawn over, i.e where the 1 would be in line 2, gets replaced with a 6... etc.

(I could not move the rotated data that will get drawn over, but at the moment I'm leaving the funcs generic....)

-B
But if you're only moving the screen down or up then it doesn't matter if you're adding new rows... just add them where the row you've just removed was and you've still got a single buffer (if segmented) screen-sized buffer that you'll never have to move/copy in memory...
smarty pants.... ;-)

that could get messy with left and right shifting as well, my brain is not up to thinking about that this early in the morning....

you would have to have an x and y offset's into the buffer.... might not be too bad as long as you can do somthing simple to work out the writing to the buffer, i.e one it gets to the end, going back to the start.....

then what about copying the said buffer to the screen buffer...? I normally use a memcpy... this would have to be replaced with a custom func to handle it!

-B
You're right; with X shifts involved, it gets a little messier.. but not that much. I think you'll be alright if you're plotting into your "buffer" in rows and you have an "extra" line at the end of the buffer. That way, you *still* have a contiguous block of memory to shunt into the screen memory.. well in one or two chunks anyway ;)

For the X, it's the same way of updating as the Y - you just plot to the last column(s) that went off the screen. Then you just use the offset to dump the buffer into screen memory and there's your X-Y scroller without any extraneous memcpys (and only a little bit of extra handling working on the buffer wrap-around... which is definitely faster than a large memcpy).

I'd advise getting some paper and drawing lots of pictures :) ... So long as you can start at any point and draw a full 320 pixel row you're alright. Columns are a little trickier and may need a wrap-around adding (like if(column>240) {column-=240;}).

For the sake of speed, I'd probably lose a bit of scrolling granularity on the PocketPCs and scroll 32-bits at a time = 2 pixels. The 1-2 pixel jump probably isn't all that visible and it'll be much faster plotting 32-bit chunks rather than worrying about aligning things or plotting 16-bits at a time.

Anyway, I'm sure you can work it out. I'm off to bed.

Cheers, g'night :),

Ref.
angedelamort · 16 Oct 2002, 03:52 UTC
This thread is really interesting :)

But I think in some occasion, the compiler can change your memcopy into a memmove instruction.

Here a quote from one of my book:
The behavior of memcpy is undefined when memory regions overlap, so the function memmove should be used in this case. However, since memcpy uses a faster algorithm, memcpy should be used when it is known that the memory regions are disjoint and speed is of concern.


So, Like someone said earlier, profiling should give you the answer for your question.But here a link that maybe will give you more ideas:
http://www.ddj.com/documents/s=865/ddj0165m

For the sake of speed, I'd probably lose a bit of scrolling granularity on the PocketPCs and scroll 32-bits at a time = 2 pixels. The 1-2 pixel jump probably isn't all that visible and it'll be much faster plotting 32-bit chunks rather than worrying about aligning things or plotting 16-bits at a time.


Here, I've experienced something weird with the 32-bit copy instead of 16-bit. After profiling my game, it was going faster with 16-bit copy rather than 32-bit copy... Don't ask me why... I didn't have time to look the assembly lol.

If you want better performance, you could write your own mini-memory manager, so you'll be able to do everything you want with your buffers and data alignment :P

Hope it help
adlm
I wouldn't be surprised if memmove actually resulted in a call to memcpy under the hood if the source and destination didn't overlap during the copy operation.
Atm speed is not a "huge" factor, and my "game" is pushing good frame rates on my e-125, I suspect it may only be an arm game in the end but. Once I add music, sound and the final touches, it's bound to slow it down..... (beside this game is just a "quickie".... (see other post on random 2D Platform generation)

-B
Brendan - there's never a real excuse for not doing it "properly", IMHO :P

AngelDeLaMort - sounds weird. I can understand 32-bit being slower if you gave memcpy a non-32bit-aligned source/destination... but other than that it should be faster. Post the assembler if you want and we can ogle it. :)

Cheers,

Ref.
angedelamort · 16 Oct 2002, 16:03 UTC
Like I said ref, it was weird :P It's an old project... and I don't want to see this code again :P But if anything like that happend again, I'll send it to you with pleasure lol