Page 1 of 1

Why does linux use one byte accesses in memset?

Posted: Mon Aug 04, 2014 8:15 am
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??

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

Posted: Mon Aug 04, 2014 8:20 am
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.

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

Posted: Mon Aug 04, 2014 8:21 am
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?

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

Posted: Mon Aug 04, 2014 8:29 am
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

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

Posted: Mon Aug 04, 2014 9:01 am
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.