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,eca91508b02e7e97 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.germany.com!newsfeed.freenet.de!news.osn.de!diablo1.news.osn.de!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Amount of copying on returned constant 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: <5dfsfnF1qav8bU1@mid.individual.net> Date: Sat, 16 Jun 2007 08:48:04 +0200 Message-ID: <15bf7x8niwgcz.horig9zyjn4p.dlg@40tude.net> NNTP-Posting-Date: 16 Jun 2007 08:47:58 CEST NNTP-Posting-Host: d9e5db67.newsspool3.arcor-online.net X-Trace: DXC=G?Z[;D6GjoYaoembcbF;DQMcF=Q^Z^V3X4Fo<]lROoRQFl8W>\BH3YR\TG?<^IdHjSDNcfSJ;bb[UIRnRBaCd On Fri, 15 Jun 2007 19:19:00 +0300, Alex R. Mosteo wrote: > I know that some "in" arguments may be passed as copies or as references, at > compiler discretion (this is one of these things that "programmers > shouldn't care about", many times quoted). > > I wonder however about results of functions, that are not modified. Look for > example at the Element function of the new Ada.Containers. They return the > stored item, that may well be a quite large controlled tagged type, for > example. > > Now, many times I want to query an element just for read-only purposes. I'm > faced with two options: > > 1) Just call Element on the container Key/Index, and be done with it. > > 2) Do a Find+Query_Element, which requires defining an extra procedure and > somewhat breaks the flow of control, but ensures no copying. > > I tend to go with 1) because of laziness and the "no premature optimization" > rule. In C++ I could use constant references. Now, I wonder if > > a) is there something in the ARM that prevents an equivalent transparent > optimization in the Ada side (returning the reference when it is detected > that the returned object is not modified)? No [*], but a way to help the compiler to know what you wanted is: declare Item : Element renames Get (Collection, Key); -- Please, don't make new objects, if you can begin ... -- Use Item > Thanks in advance, have a nice week-end. Same to you. --------------- * In Ada 95 there were limited types returned strictly by reference, but this semantics was crapped in Ada 2005. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de