public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Hei Chan <structurechart@yahoo.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: is portable aliasing possible in C++?
Date: Mon, 15 Sep 2014 02:37:00 -0000	[thread overview]
Message-ID: <1410748615.48628.YahooMailNeo@web165004.mail.bf1.yahoo.com> (raw)
In-Reply-To: <5413F0D4.5010806@redhat.com>

Hi,


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.

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.


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?
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?

Thanks in advance.





On Saturday, September 13, 2014 3:23 PM, Andrew Haley <aph@redhat.com> wrote:



On 12/09/14 23:58, haynberg@yahoo.com wrote:
>> Firstly, char types alias everything.
> (I'm not sure why you wrote that?)

Because the specification says so.  6.3.2.3, Pointers, in C9X.

> I know if you cast to char type,
> it can, but I'm going from a char type.
> 
>> Secondly, even if you call memcpy(), a compiler doesn't have to do any
>> copies if it can prove that the union you're reading into doesn't
>> escape.
> If the compiler can optimize away memcpy, why memcpy into a union, why
> not just to the type in question?  Or is memcpy into a union special?

You can copy the bytes from one object to another, and it has the
same effect.  I can't guarantee it generates the same code as a
union in all cases.

> In other words, if I write:
> 
>   msg p;
>   memcpy(&p, get_bytes(), sizeof p);  // assume the size OK
>   if (p.i)
>   // ...
> 
> Can the memcpy be optimized away, making it similar to the cast version 
> (but not undefined)?

Sure.  Try it.

>>>  Putting alignment/padding concerns aside
> FYI, I meant assume I'm on x86, or hardware that allows unaligned reads,
> and my struct doesn't have padding issues.  For example, sending a pragma
> packed struct over a socket.
> 
>>>  Another related, maybe more important, question is if GCC sees a
>>>  reinterpet_cast like this (without a may_alias type), is it free to
>>>  discard code or otherwise drastically change it due to the fact that
>>>  it’s undefined by the standard?
>>
>> Yes.  It may, and it does.
> At some point, I understand GCC began to optimize more heavily in
> strict-aliasing opportunities.  For future reference, when GCC makes
> changes like this, are they always mentioned in the release notes or 
> someplace else? 

Not AFAIK.  We take the view that GCC is free to optimize as much as
possible, subject to the constraints of the language.  And, of course,
we can't always predict what an optimization might do to every buggy
program.

Andrew.

  parent reply	other threads:[~2014-09-15  2:37 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-10 23:03 haynberg
2014-09-11  8:11 ` Andrew Haley
2014-09-11 23:25   ` haynberg
2014-09-12  8:32     ` Andrew Haley
2014-09-12 22:58       ` haynberg
2014-09-13  7:23         ` Andrew Haley
2014-09-13 11:45           ` Oleg Endo
2014-09-15  2:37           ` Hei Chan [this message]
2014-09-15  8:35             ` Andrew Haley
2014-09-15 11:07               ` Hei Chan
2014-09-15 11:21                 ` Andrew Haley
2014-09-15 11:29                   ` Hei Chan
2014-09-15 11:32                     ` Andrew Haley
2014-09-15 11:57                       ` Hei Chan
2014-09-15 13:21                         ` Andrew Haley
2014-09-15 13:31                           ` Hei Chan
2014-09-15 14:11                             ` Andrew Haley
2014-09-15 11:27                 ` Jonathan Wakely
2014-09-15 12:09                   ` Paul Smith
2014-11-02 23:55       ` Hei Chan
2014-11-03  9:34         ` Andrew Haley
  -- strict thread matches above, loose matches on Subject: below --
2014-09-09 23:13 haynberg
2014-09-10  8:17 ` Andrew Haley
     [not found] <A76FB9DDEDFA994BAF6B77704A4AF465BC2464@xchmbbal502.ds.susq.com>
2014-09-04 16:11 ` Andy Webber
2014-09-04 16:51   ` Andrew Haley
2014-09-04 17:18     ` Andy Webber
2014-09-04 17:23       ` Andrew Haley
2014-09-04 17:44         ` Andy Webber
2014-09-04 17:47           ` Andy Webber
2014-09-04 17:48           ` Andrew Haley
2014-09-04 23:11     ` Jonathan Wakely
2014-09-05  7:16       ` Andrew Haley
2014-09-05 14:19       ` Jason Merrill
2014-09-08  9:33         ` Richard Biener
2014-09-10 14:31           ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1410748615.48628.YahooMailNeo@web165004.mail.bf1.yahoo.com \
    --to=structurechart@yahoo.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).