From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10063 invoked by alias); 15 Sep 2014 08:35:54 -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 10041 invoked by uid 89); 15 Sep 2014 08:35:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=AWL,BAYES_50,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 15 Sep 2014 08:35:49 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s8F8ZltB000361 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 15 Sep 2014 04:35:47 -0400 Received: from zebedee.pink ([10.3.113.10]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s8F8ZkrE008492; Mon, 15 Sep 2014 04:35:46 -0400 Message-ID: <5416A4E1.1010000@redhat.com> Date: Mon, 15 Sep 2014 08:35:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.8.0 MIME-Version: 1.0 To: Hei Chan , "gcc-help@gcc.gnu.org" Subject: Re: is portable aliasing possible in C++? 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> In-Reply-To: <1410748615.48628.YahooMailNeo@web165004.mail.bf1.yahoo.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00082.txt.bz2 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.