From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12863 invoked by alias); 15 Sep 2014 11:07:10 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 12852 invoked by uid 89); 15 Sep 2014 11:07:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 X-HELO: nm32-vm6.bullet.mail.bf1.yahoo.com Received: from nm32-vm6.bullet.mail.bf1.yahoo.com (HELO nm32-vm6.bullet.mail.bf1.yahoo.com) (72.30.239.142) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 15 Sep 2014 11:07:08 +0000 Received: from [98.139.215.142] by nm32.bullet.mail.bf1.yahoo.com with NNFMP; 15 Sep 2014 11:07:06 -0000 Received: from [98.139.212.219] by tm13.bullet.mail.bf1.yahoo.com with NNFMP; 15 Sep 2014 11:07:06 -0000 Received: from [127.0.0.1] by omp1028.mail.bf1.yahoo.com with NNFMP; 15 Sep 2014 11:07:06 -0000 Received: (qmail 24753 invoked by uid 60001); 15 Sep 2014 11:07:06 -0000 Received: from [219.79.58.155] by web165006.mail.bf1.yahoo.com via HTTP; Mon, 15 Sep 2014 04:07:06 PDT References: <1410390231.39617.YahooMailNeo@web140202.mail.bf1.yahoo.com> <54115917.1040602@redhat.com> <1410477938.56522.YahooMailNeo@web140205.mail.bf1.yahoo.com> <5412AF85.1080200@redhat.com> <1410562688.66898.YahooMailNeo@web140201.mail.bf1.yahoo.com> <5413F0D4.5010806@redhat.com> <1410748615.48628.YahooMailNeo@web165004.mail.bf1.yahoo.com> <5416A4E1.1010000@redhat.com> Message-ID: <1410779226.12412.YahooMailNeo@web165006.mail.bf1.yahoo.com> Date: Mon, 15 Sep 2014 11:07:00 -0000 From: Hei Chan Reply-To: Hei Chan Subject: Re: is portable aliasing possible in C++? To: Andrew Haley , "gcc-help@gcc.gnu.org" In-Reply-To: <5416A4E1.1010000@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00083.txt.bz2 On Monday, September 15, 2014 4:36 PM, Andrew Haley wrote: 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? Your statement that the trick "is now hallowed by the standard" makes it sounds like at some point GCC won't guarantee it work anymore. > 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. Sorry, it is a typo -- I mean "compiler is unlikely able to avoid making *a copy* if memcpy() is used". Using the unsafe reinterpret_cast (C fashion cast), it won't have an extra copy. Using memcpy(), the compiler will have to make a copy because it sees that few lines, for example, down, the program tries to manipulate the copy. > 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. So I guess there is no way to avoid a copy if the code manipulates the member of the union, right? I understand that union and memcpy() would guarantee alignment. I was just hoping that there is a way of guaranteeing alignment without an extra copy. Sounds like there is no way? > 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.