comp.lang.ada
 help / color / mirror / Atom feed
* Performance of records with variant parts
@ 2021-03-22 17:02 John Perry
  2021-03-22 17:32 ` Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: John Perry @ 2021-03-22 17:02 UTC (permalink / raw)


Hello all

I encountered a significant penalty when using a record with variant parts in gnat community edition, on both MacOS and Linux. Suppose I have the following. (Kind of long, sorry.)

| type Object_Type is ( Sphere, Plane, None );
| type Surface_Type is ( Shiny, Checkerboard );
| type Thing_Type(kind: Object_Type := None; surface: Surface_Type := Shiny)
| is record
|    case kind is
|       when None =>
|          null;
|       when Sphere =>
|          radius2: Long_Float := 0.0;
|          center: Vector;
|       when Plane =>
|          offset: Long_Float := 0.0;
|          norm: Vector;
|    end case;
| end record;

Noticing that the fields are actually the same types, but different names, I also tried this:

| type Thing_Type is record
|    Kind: Object_Type := None;
|    Surface: Surface_Type := Shiny;
|    Size: Long_Float := 0.0;
|    Pos: Vector;
| end record;

This second version doesn't seem a good idea, but compiled output runs 15-30% faster. As far as I can tell***, it's related to this test which gets called ~3 million times:

|       case obj.kind is
|       when Sphere =>
|          -- do stuff
|       when Plane =>
|          -- do other stuff
|       when None =>
|          null;
|       end case;

Is there a reason that the record with variant parts runs so much slower? I can make the complete source available if need be.

john perry

***"as far as I can tell": gprof  stated at one point that this procedure sucks up most of the time; it's one of only two places where the discriminant is evaluated, and it's the only one that gets called A LOT; and this one change makes for the difference in performance.

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

end of thread, other threads:[~2021-04-02 16:07 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-22 17:02 Performance of records with variant parts John Perry
2021-03-22 17:32 ` Jeffrey R. Carter
2021-03-22 17:49   ` John Perry
2021-03-22 17:54     ` John Perry
2021-03-22 19:31     ` Jeffrey R. Carter
2021-03-22 22:11       ` John Perry
2021-03-23  9:31         ` Jeffrey R. Carter
2021-03-23 14:27           ` Simon Wright
2021-03-23 23:00           ` John Perry
2021-03-23 23:27             ` Jeffrey R. Carter
2021-03-26 15:38               ` Stephen Leake
2021-03-26 15:44                 ` John Perry
2021-03-30  7:12                   ` Emmanuel Briot
2021-04-01  0:03                     ` John Perry
2021-04-01  6:45                       ` Emmanuel Briot
2021-04-01 13:38                       ` Niklas Holsti
2021-04-02 16:07                         ` John Perry
2021-03-22 17:39 ` Dmitry A. Kazakov
2021-03-22 17:45   ` John Perry
2021-03-22 18:07     ` Dmitry A. Kazakov
2021-03-22 18:23       ` John Perry
2021-03-22 20:30         ` Dmitry A. Kazakov
2021-03-22 18:59 ` Niklas Holsti
2021-03-22 21:54   ` John Perry

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