comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Record aggregates assignments and the black-box and else
Date: Mon, 10 May 2010 08:20:08 -0700 (PDT)
Date: 2010-05-10T08:20:08-07:00	[thread overview]
Message-ID: <314c8794-dbce-4fe1-b8d8-1258c782fbfe@u3g2000prl.googlegroups.com> (raw)
In-Reply-To: op.vcctpljtxmjfy8@garhos

On May 7, 2:23 pm, Yannick Duchêne (Hibou57)
<yannick_duch...@yahoo.fr> wrote:

> If I do :
>
>     R := (A => R.A, B => 5);
>
> what is the most likely expected impact on the executable code efficiency,  
> providing this kind of construct oftenly appears in multiple places of the  
> source ? (the record type is obviously private and just accessed in the  
> implementation).
>
> Do you think most compilers will use the presence of an R's self component  
> as an hint for some kind of optimization when applicable ?

This isn't as simple as it looks.  The language semantics demand that
for an aggregate, the program creates a temporary object of the type
whose value is the aggregage; then it assigns R to that temporary
object.  One consequence is that if an exception is raised while
building the temporary object, R will be unaffected.  (There are
exceptions.  If controlled types are involved, I think that compilers
are allowed or required to generate code that builds the aggregate
directly in R instead of using a temporary object.  Also, if
Record_Type were limited, you couldn't do the assignment, but an
aggregate in some other contexts would *not* cause a temporary object
to be built.  I don't feel like going into details right now.)

I'm not sure what happens in a case like this:

   R := (A => R.A, B => 5);

where R was previously uninitialized.  Even though R.A is supposed to
have subtype "Natural", if it's uninitialized and thus garbage, it
could have a negative value.  Thus, it's possible that this
assignment, which is conceptually part of the assignment that creates
the temporary object:

   Temp_Object.A := R.A;

could raise an exception if R.A happened to have a garbage negative
value.  However, I don't think compilers are required to generate code
that checks this.  Since both sides are supposed to be "Natural", the
compiler can do the assignment without any checking, and is not
required to check that the value on the right side is valid.

Anyway, assuming that the compiler determines that R.A will not raise
an exception, and that B => 5 cannot raise an exception either, and
there aren't any other reasons why R has to be preserved while the
temporary object is being created, it might decide that there's no
reason to create a temporary object.  This is permissible since the
semantics would be exactly the same in all cases.  Then it's
conceivable that the compiler could generate code that looks like

   R.A := R.A;
   R.B := 5;

and then, in its optimization process, notice that the first
assignment is useless.  So it's possible that a compiler may optimize
the code in the way you were suggesting.  However, the compiler does
need to do a fair amount of checking to determine that a temp object
is unneeded, and there may be compilers that don't do all that work.

So I guess the answer to your question is probably that some compilers
will optimize the way you want, and others won't.

                                   -- Adam



  parent reply	other threads:[~2010-05-10 15:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-07 21:23 Record aggregates assignments and the black-box and else Yannick Duchêne (Hibou57)
2010-05-09  0:25 ` Randy Brukardt
2010-05-10 15:20 ` Adam Beneschan [this message]
2010-05-10 19:03   ` Yannick Duchêne (Hibou57)
replies disabled

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