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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.140.27.229 with SMTP id 92mr15621404qgx.9.1465266194814; Mon, 06 Jun 2016 19:23:14 -0700 (PDT) X-Received: by 10.157.1.174 with SMTP id e43mr61522ote.9.1465266194772; Mon, 06 Jun 2016 19:23:14 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!p34no3580495qgp.1!news-out.google.com!107ni61qgx.1!nntp.google.com!q32no4600223qgq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 6 Jun 2016 19:23:14 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=71.171.109.232; posting-account=vk6_JwoAAABkMyHO1YfdP69Hm3CpbdGR NNTP-Posting-Host: 71.171.109.232 References: <8af002bb-271a-4a76-b0db-097a3724f0b3@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7263ac4b-945c-4702-b998-e87c1de1f4a7@googlegroups.com> Subject: Re: Avoiding dispatching in procedure's with classwide types From: Jeremiah Injection-Date: Tue, 07 Jun 2016 02:23:14 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 7456 X-Received-Body-CRC: 2009270700 Xref: news.eternal-september.org comp.lang.ada:30642 Date: 2016-06-06T19:23:14-07:00 List-Id: On Sunday, June 5, 2016 at 11:12:13 PM UTC-4, riea...@comcast.net wrote: > On Saturday, May 28, 2016 at 3:01:37 PM UTC-4, Jeremiah wrote: > > I have a procedure that uses one tagged type (the primitive type) and a= classwide type. >=20 > 1) I have no clue as to what you are trying to do here. > 2) I have no idea why you think that this would improve performance. Here's a small example I cooked up. It's a useless example, but hopefully = illustrates the problem better. Say a package declares two types as so: ************************************************** package Test_Pkg_A is type Base_Class_A is tagged limited private; procedure Set (Object : in out Base_Class_A; Value : in Integer); function Get (Object : in Base_Class_A) return Integer; =20 type Base_Class_B is tagged limited private; procedure Set (Object : in out Base_Class_B; Value : in Integer); function Get (Object : in Base_Class_B) return Integer; =20 procedure Copy_Version_1 (Target : in out Base_Class_A; Source : in Base_Class_B'Class); procedure Copy_Version_2 (Target : in out Base_Class_A; Source : in Base_Class_B'Class); =20 private =20 type Base_Class_A is tagged limited record Value : Integer :=3D 0; end record; =20 type Base_Class_B is tagged limited record Value : Integer :=3D 0; end record; end Test_Pkg_A; ************************************************** The body for these packages is: ************************************************** package body Test_Pkg_A is =20 procedure Set (Object : in out Base_Class_A; Value : in Integer) is begin Object.Value :=3D Value; end Set; =20 function Get (Object : in Base_Class_A) return Integer is begin return Object.Value; end Get; =20 procedure Set (Object : in out Base_Class_B; Value : in Integer) is begin Object.Value :=3D Value; end Set; =20 function Get (Object : in Base_Class_B) return Integer is begin return Object.Value; end Get; =20 procedure Copy_Version_1 (Target : in out Base_Class_A; Source : in Base_Class_B'Class) is begin -- This seems a bit dangerous since it bypasses dispatching on Sourc= e??? Target.Value :=3D Source.Value; end Copy_Version_1; =20 procedure Copy_Version_2 (Target : in out Base_Class_A; Source : in Base_Class_B'Class) is begin -- This seems safer. Target.Value :=3D Get(Source); end Copy_Version_2; =20 =20 =20 end Test_Pkg_A; ************************************************** Notice how both Copy_Version_1 and Copy_Version_2 both take in a class wide= source. However, Copy_Version_1 directly copies the parameter while Copy_= Version_2 uses a call to a dispatching Get() function. If the Source passed in overrides Get, then both of the Copy Functions coul= d give very different answers. Say for example: ************************************************** with Test_Pkg_A; package Test_Pkg_B is type Derived_Class_A is new Test_Pkg_A.Base_Class_B with null record; =20 overriding=20 function Get (Object : in Derived_Class_A) return Integer; end Test_Pkg_B; ************************************************** with body: ************************************************** package body Test_Pkg_B is overriding=20 function Get (Object : in Derived_Class_A) return Integer is Value : Integer :=3D Test_Pkg_A.Base_Class_B(Object).Get; begin if Value > 2 then return Value; else return 0; end if; end Get; end Test_Pkg_B; ************************************************** Here for whatever reason, the Get function for Derived_Class_A bounds the v= alue from returning as a 1 (this is just a random example here). Now if you have the following main: ************************************************** with Test_Pkg_A; with Test_Pkg_B; with Ada.Text_IO; procedure Ada_Main is Target_A : Test_Pkg_A.Base_Class_A; Target_B : Test_Pkg_A.Base_Class_A; =20 Source_A : Test_Pkg_B.Derived_Class_A; Source_B : Test_Pkg_B.Derived_Class_A; begin =20 Source_A.Set(1); Source_B.Set(1); =20 Test_Pkg_A.Copy_Version_1 (Target =3D> Target_A, Source =3D> Source_A); Test_Pkg_A.Copy_Version_2 (Target =3D> Target_B, Source =3D> Source_B); =20 Ada.Text_IO.Put_Line ("Target_A: "=20 & Integer'Image(Target_A.Get)); Ada.Text_IO.Put_Line ("Target_B: "=20 & Integer'Image(Target_B.Get)); =20 =20 end Ada_Main; ************************************************** you get the output: ************************************************** D:\__workspaces\Ada\Test_Ada\obj\ada_main Target_A: 1 Target_B: 0 [2016-06-06 22:03:01] process terminated successfully, elapsed time: 02.38s ************************************************** So my question is if the method used in Copy_Value_1 considered a no-no or = since it bypasses dispatching in favor of directly copying the values in a = procedure that takes a class wide type? Or is it ok since there is no guar= antee to what the internals of the Copy_Value_1 procedure are? My gut is i= t is bad form, but I wanted to know if I was just over thinking it. As for why I would think it would improve performance: GNAT doesn't tend t= o optimize out dispatching calls (at least not in any code I have seen yet)= and I do have a couple of more complex types that would have to call 3 or = 4 dispatching calls in a procedure with a class wide type passed in. These= calls would be operated on large arrays of objects based on those types. = Taking out the dispatching could reduce the time iterating through those ar= rays. But it might also leave the procedure open to bugs on derived childr= en?