comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: Run-time checking and speed
Date: 12 Jan 1995 15:11:22 GMT
Date: 1995-01-12T15:11:22+00:00	[thread overview]
Message-ID: <3f3gqq$kts@watnews1.watson.ibm.com> (raw)
In-Reply-To: 3ev16u$ojc@pong.lasc.lockheed.com

In article <3ev16u$ojc@pong.lasc.lockheed.com>, tony@deepthought.Sgi.COM
(Tony Leavitt) writes: 

|> -- CASE 1
|> -- This case seems to need all of the full run-time checking since
|> -- the variables x and y can be unlimited when accessing the array.
|>
|> XY_array : array (integer range 1..100, integer range 1..100) of float ;
|>
|> for x in 1..100 loop
|>   for y in 1..100 loop
|>      XY_array(x,y) := calc_something  ;
|>    end loop ;
|> end loop ;

No, the loop bounds are given by compile-time constants, so any compiler
that leaves the run-time checks in (at least with optimization turned on)
is a junk compiler.  (By the way, on each iteration of the inner loop, x
and y are CONSTANTS, so there is no possibility of "calc_something"
changing their values by a side effect.  x and y are well defined to
range over 1 .. 100.)

|> -- CASE 2
|> -- This case it seems that no runtime checking is needed when
|> -- accessing the array since x and y are, by definition, within
|> -- the array bounds (assuming memory doesn't go bad while running).
|>
|> subtype arraybounds is integer range 1..100 ;
|>
|> XY_array : array (arraybounds, arraybounds) of float ;
|>
|> for x in arraybounds loop
|>   for y in arraybounds loop
|>      XY_array(x,y) := calc_something  ;
|>    end loop ;
|> end loop ;

This example is precisely equivalent to the previous one.  The subtype
declaration essentially makes the identifier "arraybounds" a shorthand
for the subtype indication "integer range 1 .. 100" (and the iteration
scheme "for x in 1 .. 100" is a shorthand for "for x in integer range
1 .. 100").

|> -- CASE 3
|> -- This seems similar to CASE 2
|>
|> XY_array : array (integer range 1..100, integer range 1..100) of float ;
|>
|> for x in XY_array'Range(1) loop
|>   for y in XY_array'Range(2) loop
|>      XY_array(x,y) := calc_something  ;
|>    end loop ;
|> end loop ;

Again equivalent.  Given the declaration of XY_array, XY_array'Range(1)
is essentially a shorthand for "1 .. 100".

General rule: Trust your optimizing compiler to eliminate the checks that
are obviously unnecessary (or get a new compiler if this trust turns out
to be misplaced).  The only ones you have to worry about are the ones
that are unobviously unnecessary, and even then only if you have observed
a performance problem and determined that the run-time check is a
signficant contributor to it.

Here's a more interesting example: 

   type Index_Subtype is Integer range 1 .. 100;
   A   : array (Index_Subtype, Index_Subtype) of Float;
   Row : Index_Subtype;
   ...
   for Column in A'Range(2) loop
      A(Row, Column) := calc_something;
   end loop;

The value of Row is guaranteed to be in range PROVIDED THAT Row HAS BEEN
ASSIGNED A VALUE.  Ada 83 rules allowed compilers to omit the index check
on Row, on the grounds that if Row was not initialized execution was
"erroneous" and any behavior was allowed to ensue.  It was controversial
whether a compiler SHOULD omit this check.  In Ada 95, the check can
still be eliminated if the compiler can determine that Row has been
assigned a value before the loop is reached.  In any event, a good
compiler will move the check out of the loop so that it is only done
once.

--
Norman H. Cohen    ncohen@watson.ibm.com



  parent reply	other threads:[~1995-01-12 15:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-01-10 22:20 Run-time checking and speed Tony Leavitt
1995-01-12  1:14 ` Roger Labbe
1995-01-13 12:09   ` Philip Brashear
     [not found] ` <3f0prq$3bq@theopolis.orl.mmc.com>
1995-01-12 14:13   ` Robert Dewar
1995-01-13  1:49     ` Doug Smith
1995-01-13 15:29       ` Norman H. Cohen
1995-01-13 15:21     ` Norman H. Cohen
     [not found]     ` <3fa2pk$kbi@felix.seas.gwu.edu>
     [not found]       ` <EACHUS.95Jan17151835@spectre.mitre.org>
     [not found]         ` <3fjhrj$9b3@oahu.cs.ucla.edu>
1995-01-20  5:11           ` Robert Dewar
1995-01-23 16:43             ` Mats Weber
1995-01-24 19:25               ` Robert Dewar
1995-01-22 18:43         ` Michael Feldman
1995-01-23 23:38           ` Robert Dewar
1995-01-26 16:14             ` Kent Mitchell
1995-01-28  6:03               ` Robert Dewar
     [not found]             ` <3gbr4f$p4b@theopolis.orl.mmc.com>
1995-01-29 13:00               ` Robert Dewar
1995-01-30 19:21                 ` Garlington KE
1995-01-12 15:11 ` Norman H. Cohen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1995-01-12 15:54 Keith Arthurs
replies disabled

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