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.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7be1cfc685fd1ce2 X-Google-Attributes: gid103376,public From: Robert Dewar Subject: Re: Access types vs. Record types as procedure parameters Date: 1999/08/19 Message-ID: <7phv12$jee$1@nnrp1.deja.com>#1/1 X-Deja-AN: 514835558 References: <37BC4F12.E33D50EE@gbr.msd.ray.com> X-Http-Proxy: 1.0 x22.deja.com:80 (Squid/1.1.22) for client 205.232.38.14 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Thu Aug 19 22:04:27 1999 GMT X-MyDeja-Info: XMYDJUIDrobert_dewar Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.04 [en] (OS/2; I) Date: 1999-08-19T00:00:00+00:00 List-Id: In article <37BC4F12.E33D50EE@gbr.msd.ray.com>, Andrew L Moore {66003} 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.