From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4e04f7888d82ca71 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Efficiency of returning big objects Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <4npvh0FbgbuhU1@individual.net> Date: Mon, 25 Sep 2006 15:09:45 +0200 Message-ID: <1b9xod9z329dc.kjk6pplxdnqh$.dlg@40tude.net> NNTP-Posting-Date: 25 Sep 2006 15:09:46 CEST NNTP-Posting-Host: 7d01b225.newsspool4.arcor-online.net X-Trace: DXC=a3=\5l;P[j^PU8j_I0DN6_4IUK\BH3YR]`C49:aB4F]DNcfSJ;bb[UFCTGGVUmh?TN\HXHJ4e80^]@CRYcli23U X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:6723 Date: 2006-09-25T15:09:46+02:00 List-Id: On Mon, 25 Sep 2006 14:14:44 +0200, Alex R. Mosteo wrote: > I had a concern over the efficiency of returning big (tagged) types, since I > like to store them in indefinite collections (a la Object'Class) instead of > storing pointers to them. It is said that, as tagged types are passed by > reference, they are always efficiently passed around. However, when you > are /returning/ and not calling, I had my doubts, so I have made this > simple testcase (see at bottom). Same with arrays - when returning a slice of a non-local string, GNAT copies it. It does this even when the callee is inlined. > It has been compiled with just gnatmake -O3. It reveals that a copy is > created in the stack for returning every time. I was hoping that, as long > as the type were used as a constant, a reference would be returned and > passed around. This would be very interesting in collections, since we > could do, for example: > > Some_Map.Element (Key).Method_Or_Member > > without efficiency penalties. > > Now, in C++ I would force this behavior explicitly returning references (I > guess this is the usual practice, but I'm not sure). In Ada, I have no idea > how to match this efficient behavior. In Ada 2006 an equivalent would be an anonymous access type of the result. As ugly as C++, but works. > So, what do you think? Is there some error in my reasoning or testbed? Is > this a particular shortcoming of this compiler (gnat gpl 2006), or I'm > overlooking something that makes this very non-trivial to implement? You mean, checking if the scope of the name of the function result is in the scope of the object returned? It is a global + temporal variables optimizations, AFAIK. And the callee should be inlined. [ However, I suppose, GNAT would make a copy even if your Create were inlined. ] In the specific case: function Get (X : Container_Type; ...) return Element_Type; the compiler could know that the result is from X. But it were still possible something like: Get (Read_Container_From_File, First_Element); -- Shall copy -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de