comp.lang.ada
 help / color / mirror / Atom feed
* Re: Access types vs. Record types as procedure parameters
  1999-08-19  0:00 Access types vs. Record types as procedure parameters Andrew L Moore {66003}
  1999-08-19  0:00 ` Robert Dewar
  1999-08-19  0:00 ` Larry Kilgallen
@ 1999-08-19  0:00 ` Matthew Heaney
  1999-08-19  0:00   ` Tucker Taft
  2 siblings, 1 reply; 5+ messages in thread
From: Matthew Heaney @ 1999-08-19  0:00 UTC (permalink / raw)


In article <37BC4F12.E33D50EE@gbr.msd.ray.com> , Andrew L Moore {66003} 
<alm@gbr.msd.ray.com>  wrote:

> I am debating on whether to pass a record object to a procedure or an
> access type to that record object.
>
> For example I have:
>
>      package body X is
>      type Record_Type;
>      type Array_Type is array (1 .. n) of Record_Type;
>      Array_Object : Array_Type;
>      procedure One;
>      procedure Two (Parameter : in Record_Type);
>      ---More procedures and functions
>      end X;
>      procedure One is
>      begin
>      for I in range 1 .. n loop
>            procedure Two(Parameter => Array_Object (I));
>      end loop;
>      end One;


Do it this way, because the record type is *probably* being passed by
reference anyway.

If you want to guarantee that the record object is passed by reference, then
declare it as tagged or limited:

  type RT is tagged record .. end record;

  type RT is limited record .. end record;


>      OR:
>      package body X is
>      type Record_Type;
>      type Array_Type is array(1 .. n) of Record_Type;
>      type Access_Type is access Record_Type;
>       Array_Object : Array_Type;
>      function Address_To_Pointer is new
> Ada.Unchecked_Conversion(System.Address, Access_Type);
>      procedure One;
>      procedure Two (Parameter : in Access_Type);
>      --More procedures and functions
>      end X;
>      procedure One is
>      Object_Ptr : Access_Type := null;
>      begin
>      for I in range 1 .. n loop
>
>          Object_Ptr := Address_To_Pointer(Array_Object(I)'Address)
>           procedure Two (Parameter => Object_Ptr);
>      end loop;
>      end One;

This is a total hack.  Do NOT do it this way.  The language provides clean
mechanisms for guaranteeing that your record is passed by reference, as I
mentioned above.

Another way closer in spirit to your 2nd method is to pass the record as an
access parameter:

      package body X is
      type Record_Type;
      type Array_Type is array(1 .. n) of aliased Record_Type;
                                          ^^^^^^^
      Array_Object : Array_Type;

      procedure One;
      procedure Two (Parameter : access Record_Type);
                                 ^^^^^^
      --More procedures and functions
      end X;

      procedure One is
      begin
      for I in range 1 .. n loop
           procedure Two (Parameter => Array_Object (I)'Access);
                                                        ^^^^^^
      end loop;
      end One;


Don't muck around with UC unless you know what you're doing, and you have a
compelling need.

You do NOT have a compelling need, so don't use UC.

> I am looking for the way that would provide the greatest speed of
> execution.  I know that passing a pointer to an object of a record type
> is more efficient that passing the record itself, but would that be
> negated by repeated calls to Unchecked_Conversion.

UC doesn't have any overhead.

> Basically, are there
> any advantages to using Unchecked_Conversion to get an access value to
> an object to then pass it to a procedure.

No, there are no advantages when you can declare a by-reference type (tagged
and/or limited) or pass the object as an access parameter.

--
Matt

It is impossible to feel great confidence in a negative theory which has
always rested its main support on the weak points of its opponent.

Joseph Needham, "A Mechanistic Criticism of Vitalism"




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Access types vs. Record types as procedure parameters
  1999-08-19  0:00 Access types vs. Record types as procedure parameters Andrew L Moore {66003}
@ 1999-08-19  0:00 ` Robert Dewar
  1999-08-19  0:00 ` Larry Kilgallen
  1999-08-19  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1999-08-19  0:00 UTC (permalink / raw)


In article <37BC4F12.E33D50EE@gbr.msd.ray.com>,
  Andrew L Moore {66003} <alm@gbr.msd.ray.com> wrote:
> I am debating on whether to pass a record object to a
procedure or an access type to that record object.
>
> For example I have:
>
>      package body X is
>      type Record_Type;
>      type Array_Type is array (1 .. n) of Record_Type;
>      Array_Object : Array_Type;
>      procedure One;
>      procedure Two (Parameter : in Record_Type);
>      ---More procedures and functions
>      end X;
>      procedure One is
>      begin
>      for I in range 1 .. n loop
>            procedure Two(Parameter => Array_Object (I));
>      end loop;
>      end One;
>
----------------------------------------------------------------
----------------------

Almost certainly the code above is passing the record by
reference. It is basically very unlikely that the compiler
would do anything else. Of course it is always possible
that a compiler generate deliberately horrible code, but
very unlikely.

>      OR:
>      package body X is
>      type Record_Type;
>      type Array_Type is array(1 .. n) of Record_Type;
>      type Access_Type is access Record_Type;
>       Array_Object : Array_Type;
>      function Address_To_Pointer is new
> Ada.Unchecked_Conversion(System.Address, Access_Type);
>      procedure One;
>      procedure Two (Parameter : in Access_Type);
>      --More procedures and functions
>      end X;
>      procedure One is
>      Object_Ptr : Access_Type := null;
>      begin
>      for I in range 1 .. n loop
>
>          Object_Ptr :=
Address_To_Pointer(Array_Object(I)'Address)
>           procedure Two (Parameter => Object_Ptr);
>      end loop;
>      end One;

The above code is perfectly frightful. It is highly
implementation dependent (how do you know you can do
unchecked conversions between address and access types?)
Are you perhaps restricted to Ada 83, if so all the more
reason not to use nasty code like the above. If you are
using Ada 95, then of course get rid of the junk unchecked
conversion (*) and use 'Access and an access parameter.

(*) if you absolutely must convert addresses to access values,
which is as above totally unnecessary and undesirable in this
case anyway, please do it the proper way with address to
access conversions.
> I am looking for the way that would provide the greatest speed
of
> execution.  I know that passing a pointer to an object of a
record type
> is more efficient that passing the record itself,

You know wrong! Indeed most programmers who try to outguess
a compiler and write low level code in an attempt to be more
efficient just don't begin to know enough to make the right
choices. This post is in fact something like a text book
example of this in action. Since the record is almost certain
to be passed by reference, passing a record as an IN parameter
is likely to generate absolutely IDENTICAL code to passing a
pointer.

> but would that be
> negated by repeated calls to Unchecked_Conversion.

More complete nonsense, the calls to Unchecked_Conversion, if
they work at all, of course generate no code whatsoever. Indeed
it is quite likely that the code generated by the two pieces of
code above is byte for byte identical.

Once more, please don't try to outguess the compiler unless you
have some reasonable idea of what code is being generated. I
have no idea why you think Unchecked_Conversion would generate
code here, but clearly you are operating with some very
significant misconceptions.

> Basically, are there
> any advantages to using Unchecked_Conversion to get an access
value to
> an object to then pass it to a procedure.

None whatever! Please write code in the clearest most readable
most portable manner possible, and trust the compiler to
generate reasonable code. There are cases where a programmer
who really knows what they are doing can improve matters by
using low level techniques, but

a) this is not such a case!
b) you are not such a programmer!

You really will do MUCH better not to try to improve performance
in this kind of manner. For sure, you do horrible damage to the
quality of the code, and at best you are unlikely to change the
performance, at worst you can damage the performance if you do
not know exactly what you are doing!

> Any assistance you could provide would be greatly appreciated.

If the above seems a bit harsh, sorry, but this really was a
particularly gruesome suggested replacement :-)



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Access types vs. Record types as procedure parameters
  1999-08-19  0:00 ` Matthew Heaney
@ 1999-08-19  0:00   ` Tucker Taft
  0 siblings, 0 replies; 5+ messages in thread
From: Tucker Taft @ 1999-08-19  0:00 UTC (permalink / raw)


Matthew Heaney wrote:
> 
> In article <37BC4F12.E33D50EE@gbr.msd.ray.com> , Andrew L Moore {66003}
> <alm@gbr.msd.ray.com>  wrote:
> 
> > I am debating on whether to pass a record object to a procedure or an
> > access type to that record object.
> ...
> Do it this way, because the record type is *probably* being passed by
> reference anyway.
> 
> If you want to guarantee that the record object is passed by reference, then
> declare it as tagged or limited: ...

If efficiency is your goal, there is no need to force the issue.
The compiler should pass by copy when that is more efficient (e.g.
when the record is very small), and pass by reference otherwise.

It sounds like you have been lulled into looking at the world
from a "C" perspective, where structs are either passed by copy,
or you need to explicitly pass a pointer.  In Ada, records are
almost always passed by reference, even when they are "IN" parameters.
The only time they would be passed by copy is if they
are very small or bit-aligned.

> --
> Matt
> 
> It is impossible to feel great confidence in a negative theory which has
> always rested its main support on the weak points of its opponent.
> 
> Joseph Needham, "A Mechanistic Criticism of Vitalism"

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Access types vs. Record types as procedure parameters
  1999-08-19  0:00 Access types vs. Record types as procedure parameters Andrew L Moore {66003}
  1999-08-19  0:00 ` Robert Dewar
@ 1999-08-19  0:00 ` Larry Kilgallen
  1999-08-19  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 5+ messages in thread
From: Larry Kilgallen @ 1999-08-19  0:00 UTC (permalink / raw)


In article <37BC4F12.E33D50EE@gbr.msd.ray.com>, Andrew L Moore {66003} <alm@gbr.msd.ray.com> writes:

> I am looking for the way that would provide the greatest speed of
> execution.  I know that passing a pointer to an object of a record type
> is more efficient that passing the record itself,

No, you cannot know that if you are using a compiler.  The machine
instructions generated to perform what you program are entirely up
to the compiler.  It knows what calling techniques are efficient,
probably better than you do.

Larry KIlgallen




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Access types vs. Record types as procedure parameters
@ 1999-08-19  0:00 Andrew L Moore {66003}
  1999-08-19  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andrew L Moore {66003} @ 1999-08-19  0:00 UTC (permalink / raw)


I am debating on whether to pass a record object to a procedure or an access type to that record object.

For example I have:

     package body X is
     type Record_Type;
     type Array_Type is array (1 .. n) of Record_Type;
     Array_Object : Array_Type;
     procedure One;
     procedure Two (Parameter : in Record_Type);
     ---More procedures and functions
     end X;
     procedure One is
     begin
     for I in range 1 .. n loop
           procedure Two(Parameter => Array_Object (I));
     end loop;
     end One;
     --------------------------------------------------------------------------------------
     OR:
     package body X is
     type Record_Type;
     type Array_Type is array(1 .. n) of Record_Type;
     type Access_Type is access Record_Type;
      Array_Object : Array_Type;
     function Address_To_Pointer is new
Ada.Unchecked_Conversion(System.Address, Access_Type);
     procedure One;
     procedure Two (Parameter : in Access_Type);
     --More procedures and functions
     end X;
     procedure One is
     Object_Ptr : Access_Type := null;
     begin
     for I in range 1 .. n loop

         Object_Ptr := Address_To_Pointer(Array_Object(I)'Address)
          procedure Two (Parameter => Object_Ptr);
     end loop;
     end One;

I am looking for the way that would provide the greatest speed of
execution.  I know that passing a pointer to an object of a record type
is more efficient that passing the record itself, but would that be
negated by repeated calls to Unchecked_Conversion.  Basically, are there
any advantages to using Unchecked_Conversion to get an access value to
an object to then pass it to a procedure.

Any assistance you could provide would be greatly appreciated.







^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1999-08-19  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-19  0:00 Access types vs. Record types as procedure parameters Andrew L Moore {66003}
1999-08-19  0:00 ` Robert Dewar
1999-08-19  0:00 ` Larry Kilgallen
1999-08-19  0:00 ` Matthew Heaney
1999-08-19  0:00   ` Tucker Taft

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