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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,eca91508b02e7e97,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Alex R. Mosteo" Newsgroups: comp.lang.ada Subject: Amount of copying on returned constant objects Date: Fri, 15 Jun 2007 19:19:00 +0300 Message-ID: <5dfsfnF1qav8bU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: individual.net 0OASJLffnozPzEAaQfn/GwYXlRlbBhG6NJK8ZaFA9uDYrXdVA= User-Agent: KNode/0.10.5 Xref: g2news1.google.com comp.lang.ada:16198 Date: 2007-06-15T19:19:00+03:00 List-Id: Hello, before I go digging into assembler listings I'd like to ask here in case someone has the answer ready. 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)? b) If not, do you know of compilers that do this in practice? (Specially interesting for me would be GNAT at -O2/-O3). Failing these, I guess I could define constant accesses for use in my own functions, but I find this not very Ada-like. Any other ideas? Thanks in advance, have a nice week-end.