On 15/09/14 03:36, Hei Chan wrote: > > This is an interesting thread. > > I think it is very common that people try to avoid making a copy > from the buffer filled by recv() (or alike) to achieve lowest > latency. > > Given that > 1. The "union trick" has always worked with GCC, and is now hallowed > by the standard. So it sounds like GCC might change in the future. Why? > 2. Somewhere in the code that might manipulate the buffer via > somehow casted packed C struct. Hence, any compiler is unlikely > able to avoid making call if memcpy() is used. I don't understand what you mean by this. You can always write a function which takes a pointer to a character type and calls memcpy() to copy it into any scalar type, and it won't unnecessarily call anything; or if it does that's a missed-optimization bug. > Then, I have the following questions: > A. I use GCC and portability isn't an issue. What is the best type > punning method to achieve lowest latency? A union. You need a union to guarantee alignment. > B. Let's say portability is important. What's the best type punning > method to achieve lowest latency? It seems like memcpy() is the > only choice? A union. In practice, this seems to work everywhere. If you are really standards-pedantic, use memcpy(). Andrew.