comp.lang.ada
 help / color / mirror / Atom feed
* Anonymous access types are evil, why?
@ 2013-08-28 11:49 ake.ragnar.dahlgren
  2013-08-28 16:10 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: ake.ragnar.dahlgren @ 2013-08-28 11:49 UTC (permalink / raw)


Consider the following application that uses anonymous access types and allocates Controlled objects on the heap using two different ways. One way takes 60 times longer than the other:

C:\huge_performance_problem>C:\huge_performance_problem\obj\main
Duration:  3.306827264
Duration:  0.056302128

C:\huge_performance_problem>

Please review the code:

-- main.adb:

with Ada.Text_IO,
     Models.A,
     Models.B,
     Ada.Real_Time;

procedure Main is

   Number_Of_Times : Positive := 40000;

   type Method_Pointer_Type is access procedure (B : in out Models.B.B_Type);
   
   procedure Measure_Time (Method : Method_Pointer_Type)
   is
      B : Models.B.B_Type;
      
      Start_Time_Stamp : Ada.Real_Time.Time;
      End_Time_Stamp   : Ada.Real_Time.Time;
   begin
      Start_Time_Stamp := Ada.Real_Time.Clock;
      for I in 1 .. Number_Of_Times loop
         Method(B);
      end loop;
      End_Time_Stamp := Ada.Real_Time.Clock;
   
      declare
         use type Ada.Real_Time.Time;
         
         Total_Time : Duration := Ada.Real_Time.To_Duration (End_Time_Stamp - Start_Time_Stamp);
      begin
         Ada.Text_IO.Put_Line ("Duration: " & Total_Time'Img);
      end;
   end Measure_Time;
      
begin
   Measure_Time(Method => Models.B.Direct_Assignment'Access);
   Measure_Time(Method => Models.B.Indirect_Assignment'Access);
end Main;



-- models.ads:

package Models is
      
end Models;



-- models.a.ads:

with Ada.Finalization;

package Models.A is

   type A_Type is new Ada.Finalization.Controlled with
      record
         Asdf : Integer;
         qwer : String(1..8000);
      end record;
   
   type A_Access_Type is access all A_Type;
   
end Models.A;



-- models.b.ads:

with Ada.Finalization,
     Models.A;

package Models.B is

   type B_Type is new Ada.Finalization.Controlled with
      record
         A : access Models.A.A_Type;
      end record;
   
   procedure Direct_Assignment (B : in out B_Type);
   procedure Indirect_Assignment (B : in out B_Type);
   
   type B_Access_Type is access all B_Type;
   
end Models.B;



-- models.b.adb:

with Ada.Text_IO;

package body Models.B is

   procedure Direct_Assignment(B : in out B_Type) is
   begin
      B.A := new Models.A.A_Type;
   end Direct_Assignment;
   
   procedure Indirect_Assignment (B : in out B_Type) is
   begin
      declare
         A : Models.A.A_Access_Type := new Models.A.A_Type;
      begin
         B.A := A;            
      end;
   end Indirect_Assignment;
      
end Models.B;



Now consider the case when the declaration of B_Type is changed to a named access type:
   type B_Type is new Ada.Finalization.Controlled with
      record
         A : Models.A.A_Access_Type;
      end record;

The execution times are now roughly the same:

C:\huge_performance_problem>C:\huge_performance_problem\obj\main
Duration:  0.055985048
Duration:  0.058163538

C:\huge_performance_problem>

What are the conclusions we can draw?
1. Perhaps one conclusion would be that when using anonymous access types then indirect assignment should be preferred over direct assignment. (see Models.B.Direct_Assignment and Models.B.Indirect_Assignment).
2. Avoid anonymous access types. Prefer named access types and 'Unchecked_Access.

Is there anybody who can explain why direct assignment takes approximately 60 times longer than indirect assignment?

Best regards,
Åke Ragnar Dahlgren

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

end of thread, other threads:[~2013-08-30 17:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-28 11:49 Anonymous access types are evil, why? ake.ragnar.dahlgren
2013-08-28 16:10 ` Adam Beneschan
2013-08-28 21:10   ` Randy Brukardt
2013-08-30  7:29   ` ake.ragnar.dahlgren
2013-08-30 15:17     ` Adam Beneschan
2013-08-30 17:04       ` Robert A Duff
2013-08-28 20:16 ` sbelmont700
2013-08-28 21:10   ` Shark8
2013-08-30 16:16 ` Gerhard Rummel

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