Why does linux use one byte accesses in memset?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
criny
Posts: 7
Joined: Sun Jul 27, 2014 1:19 pm

Why does linux use one byte accesses in memset?

Post by criny »

recently, I knew linux kernel source use "memset by one-byte"

following is that source...( linux kernel version - 2.6.32.63 , lib/string.c )

Code: Select all

void *memset(void *s, int c, size_t count)
{
	char *xs = s;

	while (count--)
		*xs++ = c;
	return s;
}
above is very slow in my own kernel... so I use following source

Code: Select all

void kMemSet( void* pvDestination, BYTE bData, int iSize )
{
    int i;
    QWORD qwData;
    int iRemainByteStartOffset;

    qwData = 0;
    for( i = 0 ; i < 8 ; i++ )
    {
        qwData = ( qwData << 8 ) | bData;
    }

    for( i = 0 ; i < ( iSize / 8 ) ; i++ )
    {
        ( ( QWORD* ) pvDestination )[ i ] = qwData;
    }

    iRemainByteStartOffset = i * 8;
    for( i = 0 ; i < ( iSize % 8 ) ; i++ )
    {
        ( ( char* ) pvDestination )[ iRemainByteStartOffset++ ] = bData;
    }
}
why linux kernel source use that slow memset source??
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: why linux kernel source use one byte access in "memset"?

Post by bluemoon »

Have you actually measure the performance?

By the way your version has 2 problem:
- the function prototype is not compatible to standard
- it breaks alignment requirement, which is also why char* is used as a "base" version.

Note that modern compiler will replace memset call with builtin anyway, the lib's version is provided as a fall back.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: why linux kernel source use one byte access in "memset"?

Post by alexfru »

How do you know *it* is very slow? Are you sure you aren't doing silly things like zeroing out gigabytes of memory every other minute?

How do you know that the kernel actually uses this function and not another/better one or the one that the compiler could easily generate inline at the place of invocation?
criny
Posts: 7
Joined: Sun Jul 27, 2014 1:19 pm

Re: why linux kernel source use one byte access in "memset"?

Post by criny »

alexfru wrote:How do you know *it* is very slow? Are you sure you aren't doing silly things like zeroing out gigabytes of memory every other minute?

How do you know that the kernel actually uses this function and not another/better one or the one that the compiler could easily generate inline at the place of invocation?

thank u for teaching...
I was so silly....

I knew kernel don't use memset code in string.c... it's just fall back....
In real kernel binary source, memset is a version of gcc optimazation memset....

thank you guys
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Why does linux use one byte accesses in memset?

Post by sortie »

Your memset function is wrong. It doesn't have the standard prototype. Please note that even though gcc will likely replace memset calls with whatever optimization the compiler does, you are still required to provide a fallback memset implementation. The gcc documentation says you must provide memset, memcmp, memmove and memcpy. Very bad things will happen if it uses those and you didn't follow the standard API for those. You can still make and use your own kMemCpy function if you really desire, though you gain nothing from it, but don't be surprised if it gets optimized to a raw memcpy call.
Post Reply