Jump to content
  • Advertisement
Sign in to follow this  
L. Spiro

Fast Approximation to memcpy()

This topic is 601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After creating fast approximations for sin() and cos(), I found that one of the heaviest functions on a per-frame basis is std::memcpy() (and std::memset()).
This is bothersome, so I decided to work on a cheap approximation for these routines too.

Post your fastest, cheapest approximations to std::memcpy() here!


L. Spiro

Share this post


Link to post
Share on other sites
Advertisement

i wont even start to code that in asm: wasnt that just a loop, asm loop ?

like in single c++

for (int i=0; i < len; i++)

destination = source;

 

you anyway pass an array of addresses of the source 

 

 

the less instructions you write the better

:wub:

 

 

somehow i cant imagine to just throw set of bytes, into other memory region

Edited by WiredCat

Share this post


Link to post
Share on other sites
void kyloMemCpy(void *dest, void *src, size_t n)
{
   char *cdest = (char *)dest;
   for (int i=0; i<n; i++)
       cdest = 0; // most memory gets initialised to zero by something, so statistically this is usually correct
}

Share this post


Link to post
Share on other sites

Just map the same physical region to another virtual address. Same content, zero copy. Different addresses!

Hahaha, that too! :D

 

This is a treacherous way of doing it, though. Unless you map the memory CoW, writing to one block of memory will (obviously) modify the other. Which is almost certainly not what the unwitty observer would expect to happen. But if you map it CoW, you have the same insidious behaviour that people writing servers experience with fork.

 

You fork, this took slightly under half a millisecond, and it's all fine and dandy, everything just works, life is good. And then either the parent or the child process writes to memory. Oh fuck... suddenly everything is so slow, I cannot understand why there are so many page faults. Latencies in the hundreds of milliseconds. What's going on! (This is for example a well-known "issue" with redis. Not really an issue but a technical necessity, but still... well-known enough to have its own FAQ page, and about 30k web sites writing about it.)

 

The nasty thing is that the reason why all these faults happen all of a sudden is not obvious (well... it is, but... not to you, at that time -- from your perspective there is no reason why they should happen). A second earlier, all was good, and now every memory access causes a fault. You didn't even touch anything! And to top it off with sugar, it's operating-system dependant...

 

And then, to maximize your joy, you are running on a Linux system with huge pages enabled, and you no longer understand a thing...

Share this post


Link to post
Share on other sites

Don't forget about overcommiting. Enjoy processes dying out of nowhere without any warning or explanation.

 

As for speed, reminds me of a situation I had with memcmp. Apparently at least on some implementations it's optimized for comparing large chunks of data when I was using it to compare a few bytes instead - but not a fixed amount so the compiler can't just unroll it. And this was in a tight loop. Turns out that the overhead of preparing for a large comparison outweighted the benefit, to the point that simply adding a simple check for the first byte (since most comparisons would fail) would speed it up like crazy.

 

Just because the standard library is probably better optimized than what you can do doesn't necessarily mean it'll be best optimized for your actual case.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!