comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Efficiency of returning big objects
Date: 25 Sep 2006 17:33:23 -0700
Date: 2006-09-25T17:33:23-07:00	[thread overview]
Message-ID: <1159230803.296443.27880@m7g2000cwm.googlegroups.com> (raw)
In-Reply-To: 4npvh0FbgbuhU1@individual.net

Alex R. Mosteo wrote:
> Hello,
>
> I had a concern over the efficiency of returning big (tagged) types......

> 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.
>
> 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?

If I'm understanding you correctly, in your original program, you'd
like it if Create simply returned a pointer to Borg, and then the
parameter to Check used the pointer to Borg, and no copying would be
done.  But then the following program wouldn't work right.  Note that
according to the Ada rules, since the function Create is *not*
return-by-reference, the Ada semantics indicate that the result of
Create is an anonymous object (6.5(21)); therefore, Borg and Y are not
supposed to denote the same object, and the rule about distinct access
paths (6.2(12)) doesn't apply to the example below.  I think that
expecting a compiler to figure out what's going on, so that it wouldn't
optimize the copy away in my example but *would* optimize it away in
yours, may well be too much to ask.

                              -- Adam


with Ada.Calendar; use Ada.Calendar;
with Ada.Text_Io;  use Ada.Text_Io;
procedure Ref is

   type Big_Thing is array (1 .. 64_000) of Character;

   type T is tagged record
      Dummy : Big_Thing := (others => 'c');
      X     : Integer   := 0;
   end record;

   Borg : T;

   procedure Check (Y : T) is
   begin
      Borg.X := 1133;  --- We can't let this modify Y!!!
      if Y.X /= 0 then
         raise Constraint_Error;
      end if;
   end Check;

   function Create return T is
   begin
      return Borg;
   end Create;

   Start : Time;
   Iters : Natural;

begin
   --  Pass - how?
   Iters := 0;
   Start := Clock;
   while Clock - Start < 1.0 loop
      Check (Create);
      Iters := Iters + 1;
   end loop;
   Put_line ("Pass value:" & Natural'Image (Iters));  
end Ref;




  parent reply	other threads:[~2006-09-26  0:33 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
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 [this message]
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