comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Efficiency of returning big objects
Date: Mon, 25 Sep 2006 15:09:45 +0200
Date: 2006-09-25T15:09:46+02:00	[thread overview]
Message-ID: <1b9xod9z329dc.kjk6pplxdnqh$.dlg@40tude.net> (raw)
In-Reply-To: 4npvh0FbgbuhU1@individual.net

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



  reply	other threads:[~2006-09-25 13:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-25 12:14 Efficiency of returning big objects Alex R. Mosteo
2006-09-25 13:09 ` Dmitry A. Kazakov [this message]
2006-09-25 13:51   ` Alex R. Mosteo
2006-09-25 15:24 ` Georg Bauhaus
2006-09-25 16:41   ` Alex R. Mosteo
2006-09-25 19:31 ` Jeffrey R. Carter
2006-09-26  7:45   ` Alex R. Mosteo
2006-09-26  0:33 ` Adam Beneschan
2006-09-26  7:43   ` Alex R. Mosteo
replies disabled

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