On 27 Jul 2012, ir outgrape:

> Hello there,
>
> I'm new here and seldom use mail list. Please tell me if I did something  
> improper.
> Now I'm learning ansi C and socket programming these days. I read polipo  
> source code and saw this (in event.c):
>
> /* Let the compiler optimise the common cases */
> if(dsize == sizeof(void*))
>      memcpy(event->data, data, sizeof(void*));
> else if(dsize == sizeof(StreamRequestRec))
>      memcpy(event->data, data, sizeof(StreamRequestRec));
> else if(dsize > 0)
>      memcpy(event->data, data, dsize);
>
> I really don't understand why this is a "optimise". I'll appreciate if  
> someone can help.

Many compilers can optimize memcpy() for small n, but they're only going
to do this if n is known to be small at compile-time. The trick above
ensures that, in the first two branches, the compiler can detect this.

It's probably only useful in hot paths, but it can be useful there.

(Of course, the memcpy() in the libc might be optimized as well: e.g. on
Linux with GCC and glibc, GCC can replace memcpy() with inline code for
small copies, but glibc has SSE optimizations and the like. The latter
are only likely to be useful for larger n, though, not for something as
small as a pointer: there, the function call overhead would dominate, so
if the compiler can realise that it can optimize memcpy() into inline
code in this case it is likely to yield a speedup even on a system with
optimized memcpy() in the libc.)


-- 
NULL && (void)

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Polipo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/polipo-users

Reply via email to