comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Initialization and Finalization of limited object "returned" by a function
Date: Fri, 12 Feb 2010 08:43:06 -0800 (PST)
Date: 2010-02-12T08:43:06-08:00	[thread overview]
Message-ID: <c66e7b5f-6c32-4eaf-ba8a-4c3d70d0e108@q2g2000pre.googlegroups.com> (raw)
In-Reply-To: 7tkl8mF2qbU1@mid.individual.net

On Feb 12, 1:27 am, "Alex R. Mosteo" <alejan...@mosteo.com> wrote:

> I remember an old thread I started on return by reference. My question then
> was if gnat was clever enough to optimize away copies in returned values
> used as constants, specifically using the container library. The answer
> empirically found was that it wasn't.
>
> While not the same thing as a C++ return by constant reference, if I
> interpret you correctly, this kind of return X : T; removes one copy of the
> object in the returning machinery, by using the destination as in-place
> receptacle, right? If so, I wonder if it's worth the attempt to use this in
> some speed critical code.

Maybe.  Doing it this way could remove one copy, according to the
language semantics.  However, the compiler can of course generate any
code it wants, if it has the effect that the language semantics
demand.

I did try this with one compiler:

   function Func1 return Rec is
      Result : Rec;
   begin
      Result.Component1 := <blahblah>;
      Result.Component2 := <blahblah>;
      return Result;
   end;

   function Func2 return Rec is
   begin
      return Result : Rec do
         Result.Component1 := <blahblah>;
         Result.Component2 := <blahblah>;
      end return;
   end Func2;

With optimization turned off, Func2 did generate one less copy.
However, the generated code passed the address of a "result object" as
a parameter to the functions, and this means Func2 would be using
indirection through this address to set the components, while Func1
would be setting them on the local stack frame.  Whether this
indirection adds any cycles, I don't know.  I'd guess that it
wouldn't, but this could depend on the processor.

But, assuming that there are no controlled components, there's nothing
preventing the compiler from generating the same code for Func1 as for
Func2, and eliminating the extra copy.  So I guess that using extended
return might be worth a try---try it both ways, and see if the
compiler generates faster code.

If there are controlled components, then the semantics are different,
because Func1 will have to assign Result to the "result
object" (requiring an "adjust" operation) and then finalize Result
(requiring a "finalize" operation).  The language gives compilers
permission to remove adjust/finalize pairs in some cases, but I don't
know if this is one of those cases.  If not, then this is a case where
extended return will definitely benefit you.

                                 -- Adam






  reply	other threads:[~2010-02-12 16:43 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-11  4:37 Initialization and Finalization of limited object "returned" by a function Hibou57 (Yannick Duchêne)
2010-02-11  9:51 ` Hibou57 (Yannick Duchêne)
2010-02-11 11:00 ` Ludovic Brenta
2010-02-11 11:33   ` Jean-Pierre Rosen
2010-02-11 23:15   ` Hibou57 (Yannick Duchêne)
2010-02-11 23:24     ` Robert A Duff
2010-02-12  5:41       ` Hibou57 (Yannick Duchêne)
2010-02-12 15:15         ` Robert A Duff
2010-02-12 16:27           ` Jean-Pierre Rosen
2010-02-12 17:53             ` Jacob Sparre Andersen
2010-02-12 18:05               ` Adam Beneschan
2010-02-13  1:59             ` Randy Brukardt
2010-02-12 16:57           ` Adam Beneschan
2010-02-12 18:07             ` mockturtle
2010-02-12 18:29               ` Hibou57 (Yannick Duchêne)
2010-02-12 19:09             ` Robert A Duff
2010-02-13  2:00               ` Randy Brukardt
2010-02-13  2:51                 ` Hibou57 (Yannick Duchêne)
2010-02-13 15:59                   ` Robert A Duff
2010-02-13 19:34                     ` Hibou57 (Yannick Duchêne)
2010-02-13 19:45                       ` Robert A Duff
2010-02-12 19:10             ` (see below)
2010-02-13  9:54           ` Dmitry A. Kazakov
2010-02-13 15:52             ` (see below)
2010-02-14 10:23               ` Dmitry A. Kazakov
2010-02-13 15:53             ` Robert A Duff
2010-02-14 10:59               ` Dmitry A. Kazakov
2010-02-14 22:00                 ` Hibou57 (Yannick Duchêne)
2010-02-11 15:16 ` Robert A Duff
2010-02-11 17:40   ` Adam Beneschan
2010-02-11 19:10     ` Robert A Duff
2010-02-11 21:51       ` Adam Beneschan
2010-02-11 22:49         ` Hibou57 (Yannick Duchêne)
2010-02-11 22:53           ` Hibou57 (Yannick Duchêne)
2010-02-11 23:08             ` Robert A Duff
2010-02-11 23:18               ` Hibou57 (Yannick Duchêne)
2010-02-12  0:48               ` Randy Brukardt
2010-02-12  5:37               ` Hibou57 (Yannick Duchêne)
2010-02-13  1:54                 ` Randy Brukardt
2010-02-12  5:39               ` Hibou57 (Yannick Duchêne)
2010-02-12 15:10                 ` Robert A Duff
2010-02-12 17:15                   ` (Hibou57) Yannick Duchêne
2010-02-12 19:07                     ` Robert A Duff
2010-02-12  1:05           ` Adam Beneschan
2010-02-12  2:35             ` Hibou57 (Yannick Duchêne)
2010-02-12  2:36               ` Hibou57 (Yannick Duchêne)
2010-02-12  2:36               ` Hibou57 (Yannick Duchêne)
2010-02-12  2:36               ` Hibou57 (Yannick Duchêne)
2010-02-12  2:37               ` Hibou57 (Yannick Duchêne)
2010-02-12  2:37               ` Hibou57 (Yannick Duchêne)
2010-02-12  2:37               ` Hibou57 (Yannick Duchêne)
2010-02-12  4:27                 ` Hibou57 (Yannick Duchêne)
2010-02-12  4:28                   ` Hibou57 (Yannick Duchêne)
2010-02-11 22:53         ` Robert A Duff
2010-02-11 23:41           ` Adam Beneschan
2010-02-12  0:22             ` Robert A Duff
2010-02-12  5:25         ` Hibou57 (Yannick Duchêne)
2010-02-12  9:27         ` Alex R. Mosteo
2010-02-12 16:43           ` Adam Beneschan [this message]
2010-02-12 19:11             ` Robert A Duff
2010-02-12  0:44     ` Randy Brukardt
2010-02-12  4:47     ` Hibou57 (Yannick Duchêne)
2010-02-12 18:02       ` Adam Beneschan
2010-02-12  4:49     ` Hibou57 (Yannick Duchêne)
2010-02-12  4:40   ` Hibou57 (Yannick Duchêne)
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox