This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Code change suggestions c/o memcpy





With the below code I have seen significant performance improvements in my
memory copying.  I compiled my code at -O3 level and tested it on an Intel
based Linux box.

Following is the timing results from my test runs today.  First column is
the number of bytes being copied.  Second and third columns are the average
nanoseconds required to copy the memory. I was running loops of 100,000
with a gettimeofday before and after the loop to get the averages. It
breaks even somewhere between 800 and 1000.  After that there is a constant
overhead with the extra function call and two mods.

                    nanoseconds
bytes        memcpy       fastcpy
10               59                   19
20               59                   19
30               61                   25
50               76                   30
100             86                  34
200             113                69
500             211                144
1000           209                218
1500           252                258

The results are not staggering but could add up to significant savings
depending on how many memcpy's a program executes.  The same method of
memory manipulation works good for comparing memory.

void* fastcpy(void* dest,void* src,int len)
{
    if(len > 800 || (unsigned long)dest%sizeof(long) || (unsigned
long)src%sizeof(long))
        return memcpy(dest,src,len);
    long long* lldest = (long long*)dest;
    long long* llsrc  = (long long*)src;
    if(len >= sizeof(long long))
    while(len >= sizeof(long long))
    {
        *lldest = *llsrc;
        lldest++;
        llsrc++;
        len -= sizeof(long long);
    }
    int* idest = (int*)lldest;
    int* isrc  = (int*)llsrc;
    if(len >= sizeof(int))
    while(len >= sizeof(int))
    {
        *idest = *isrc;
        idest++;
        isrc++;
        len -= sizeof(int);
    }
    char* cdest = (char*)idest;
    char* csrc  = (char*)isrc;
    switch(len)
    {
    case 3:
       *cdest++ = *csrc++;
    case 2:
       *cdest++ = *csrc++;
    case 1:
       *cdest++ = *csrc++;
    }
    return dest;
}

Scott Yeager
407-771-5328


                                                                                                                                       
                      "FSF General                                                                                                     
                      Contact Address          To:       Scott Yeager/CIMG/CVG@CVG                                                     
                      via RT"                  cc:                                                                                     
                      <info@fsf.org>           Subject:  Re: [gnu.org #206026] Code change suggestions                                 
                      Sent by: www-data                                                                                                
                      <www-data@gnu.org                                                                                                
                      >                                                                                                                
                                                                                                                                       
                                                                                                                                       
                      07/12/2004 01:00                                                                                                 
                      PM                                                                                                               
                      Please respond to                                                                                                
                      info                                                                                                             
                                                                                                                                       
                                                                                                                                       




On Mon, Jul 12, 2004 at 08:57:23AM -0400, scott.yeager@convergys.com via RT
wrote:
>    It was recently suggested that I submit change suggestions to GNU when
I
> develop faster methods of doing common things.  Is this the correct place
> to submit change suggestions?

Dear Scott,

If you would like to make a suggestion for improvement to a particular GNU
program or library, please contact its maintainers with your suggestion.
You can usually find contact information in the program documentation; if
not, try looking it up in our Free Software Directory
(http://www.gnu.org/directory/index.html).

If you suggestion is more general, you can send it here.

Best regards,

--
Brett Smith, Free Software Foundation
Become a card-carrying member of FSF:
     http://member.fsf.org/
Help support our work for FSF and the GNU project:
     http://svcs.affero.net/rm.php?r=fsfinfo








Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]