comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Community Input for the Maintenance and Revision of the Ada Programming Language
Date: Wed, 6 Sep 2017 16:21:32 -0500
Date: 2017-09-06T16:21:32-05:00	[thread overview]
Message-ID: <ooposs$5o1$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: e9e2556d-9b23-4e7f-9058-776dd506f73c@googlegroups.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3984 bytes --]

"Johan Söderlind Åström" <johaa1993@gmail.com> wrote in message 
news:e9e2556d-9b23-4e7f-9058-776dd506f73c@googlegroups.com...
>On Sunday, September 3, 2017 at 3:30:23 AM UTC+2, Randy Brukardt wrote:
>> "Johan Söderlind Åström" <johaa1993@gmail.com> wrote in message
>> news:338b355a-dee4-4c73-b00e-09d9a8430fb1@googlegroups.com...
>> ...
>> >procedure Algorithm (Item : Integer_Array) is
>> >  use Ada.Assertions;
>> >  X : Integer_Array (0 .. Item'Length - 1) with Address => Item'Address;
>>
>> The use of address clauses other than for interfacing is evil (and not
>> guaranteed to work in the case of overlaying Ada code). Don't do that.
>>
>> You can change the bounds of an array with a type conversion to a
>> constrained subtype, which is likely to be cheaper and not break if you
>> change compilers.

>What do you mean with cheaper? Faster execution?, Less memory footprint?

Both. The address clause adds an extra level of indirection that the type 
conversion doesn't necessarily add. (In Janus/Ada, the conversion would 
remove or replace the bounds descriptor, making the access cost identically 
to the original array accesses; an address clause always adds a level of 
indirection.) But I forgot about the cost to copy the array (usually 
insignificant, but not always).

>Which compiler will break my code?

Any compiler could; 13.3(16) says that it only required for 'Address to have 
a "useful result" if the prefix is aliased or by-reference (neither which is 
true for the parameter Item of type Integer_Array). Ergo, such a usage is 
not portable (even if many compilers allow it). Dunno if it will actually 
break in any implementation, but it is best to avoid these sorts of 
non-portable things.

As discussed,
   procedure Algorithm (Item : Integer_Array) is
        subtype Zero_Array is Integer_Array (0 .. Item'Length - 1);
        X : constant Zero_Array := Zero_Array (Item);

has the same effect (modulo probably making an extra copy of the data), is 
always going to work (assuming Item'Length is short enough to fit in the 
index subtype - the point of Stephen Leake), doesn't need any assertions, 
and is much easier to read.

>I do not know how Ada accesses a array element internally. But a zero
>indexed array must be cheaper than a custom indexed array, see:
>
>Accessing nth element from zero indexed array:
>(base_address + index*element_size).
>
>Accessing nth element from a custom indexed array:
>(base_address + (index - first)*element_size)

In most cases, that is not an issue. Whenever Item'First is static (that is 
known to the compiler), the compiler will fold it into the base address so 
the cost is the same. (And there is a cost to rebasing the array, no matter 
how you do it.)

Specifically, the general form:

(base_address + (index - first)*element_size)
is the same as:
(base_address - (first*element_size) + (index*element_size))

In the usual case, when both first and element_size are known to the 
compiler, the code generation will be

((base_address - (first*element_size)) + (index*element_size))

with the first part calculated by the compiler and generated as a single 
item. The only runtime operations are the + and second *, just as in the 
zero-based form.

Moral: Compilers can eliminate most of the supposedly extra costs; it pays 
only to worry about those that are known (via experiment) to be significant 
in the runtime of a program. (The 90-10 rule says that the performance of 
90% of the code has no effect on the result.)

Moral2: Premature optimization is the root of much evil. Only worry about 
the performance of code that is demonstrably too slow. Otherwise, 
readability and maintainability are the most important goals after 
correctness. YMMV.

                                Randy.



I do not think that accessing a element from a array is a complicated thing 
and starting from a zero indexed array is even simpler, then why would a 
compiler not guarantee such a simple thing to work? 


  parent reply	other threads:[~2017-09-06 21:21 UTC|newest]

Thread overview: 228+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-03  5:45 Community Input for the Maintenance and Revision of the Ada Programming Language Randy Brukardt
2017-08-03 12:06 ` Lucretia
2017-08-03 12:52   ` Lucretia
2017-08-09 21:08     ` Luke A. Guest
2017-08-09 21:12       ` Luke A. Guest
2017-08-10 14:43         ` Lucretia
2017-08-10 15:14           ` Dmitry A. Kazakov
2017-08-11  1:27             ` Randy Brukardt
2017-08-11  1:24           ` Randy Brukardt
2017-08-11  5:54             ` G.B.
2017-08-10  5:41       ` G.B.
2017-08-10 14:41         ` Lucretia
2017-08-10 15:25           ` J-P. Rosen
2017-08-10 15:42             ` Luke A. Guest
2017-08-10 15:44             ` Dmitry A. Kazakov
2017-08-11  0:56           ` Randy Brukardt
2017-08-11  1:10           ` Randy Brukardt
2017-08-11 22:43             ` Shark8
2017-08-12  2:40               ` Luke A. Guest
2017-08-11 14:42           ` Justin Sq
2017-08-11 14:48             ` jsquirek
2017-08-11 17:10               ` Luke A. Guest
2017-08-11 17:24                 ` Justin Sq
2017-08-11 20:09                   ` Dmitry A. Kazakov
2017-08-11 21:13                     ` Randy Brukardt
2017-08-18 21:06                       ` Robert Eachus
2017-08-31  0:49                         ` Randy Brukardt
2017-08-31  7:36                         ` Jacob Sparre Andersen
2017-08-31  9:52                           ` Dmitry A. Kazakov
2017-08-31 12:49                             ` Jacob Sparre Andersen
2017-08-31 13:16                               ` Dmitry A. Kazakov
2017-08-31 14:09                                 ` Jacob Sparre Andersen
2017-08-31 14:41                                   ` Dmitry A. Kazakov
2017-08-31 15:45                                     ` Simon Wright
2017-08-31 16:08                                       ` Dmitry A. Kazakov
2017-08-31 18:51                                     ` Georg Bauhaus
2017-08-31 23:54                               ` Randy Brukardt
2017-09-01  3:51                                 ` Justin Sq
2017-09-01  7:24                                   ` Dmitry A. Kazakov
2017-09-01 12:23                                     ` jsquirek
2017-09-03  1:17                                   ` Randy Brukardt
2017-08-18 20:33               ` Robert Eachus
2017-08-19  1:24                 ` Shark8
2017-08-19  8:55                   ` Dmitry A. Kazakov
2017-08-19 16:38                     ` Shark8
2017-08-19 17:09                       ` Dmitry A. Kazakov
2017-08-22  6:02                         ` Robert Eachus
2017-08-22  7:34                           ` Dmitry A. Kazakov
2017-08-26  4:41                             ` Robert Eachus
2017-08-26  6:29                               ` Dmitry A. Kazakov
2017-08-26  7:16                                 ` Jacob Sparre Andersen
2017-08-26  7:48                                   ` Dmitry A. Kazakov
2017-08-27 12:03                                     ` Robert Eachus
2017-08-27 12:35                                       ` Dmitry A. Kazakov
2017-08-27 14:44                                         ` Dennis Lee Bieber
2017-08-27 16:32                                           ` Dmitry A. Kazakov
2017-08-19  8:47                 ` Dmitry A. Kazakov
2017-08-10 12:18       ` J-P. Rosen
2017-08-10 12:40         ` Lucretia
2017-08-10 15:17           ` J-P. Rosen
2017-08-11  8:33         ` AdaMagica
2017-08-10 16:28       ` Pascal Obry
2017-08-10 16:52         ` Dmitry A. Kazakov
2017-08-10 17:08           ` Luke A. Guest
2017-08-10 17:25             ` Dmitry A. Kazakov
2017-08-11  1:02               ` Lucretia
2017-08-11  6:08                 ` Dmitry A. Kazakov
2017-08-11 20:51                 ` Randy Brukardt
2017-08-10 17:06         ` Luke A. Guest
2017-08-11  1:31           ` Randy Brukardt
2017-08-11  6:24             ` G.B.
2017-08-11  7:57               ` Stefan.Lucks
2017-08-11  8:34                 ` Dmitry A. Kazakov
2017-08-11  8:53                   ` Stefan.Lucks
2017-08-11  9:12                     ` Dmitry A. Kazakov
2017-08-11  9:22                   ` AdaMagica
2017-08-12 18:29                   ` Jacob Sparre Andersen
2017-08-29 16:01               ` Alejandro R. Mosteo
2017-08-09 15:24 ` Johan Söderlind Åström
2017-08-09 17:23   ` Shark8
2017-08-09 18:49     ` Dmitry A. Kazakov
2017-08-31  6:59   ` Jacob Sparre Andersen
2017-08-28 23:49 ` faryumg
2017-08-29  8:17   ` Simon Wright
2017-08-31 13:35   ` Johan Söderlind Åström
2017-09-02 10:49   ` Vincent DIEMUNSCH
2017-09-02 11:04     ` Dmitry A. Kazakov
2017-09-02 11:21     ` Johan Söderlind Åström
2017-09-02 15:22       ` Vincent DIEMUNSCH
2017-09-02 17:19       ` Jeffrey R. Carter
2017-09-04 20:52         ` Johan Söderlind Åström
2017-09-03  1:30       ` Randy Brukardt
2017-09-06 15:02         ` Johan Söderlind Åström
2017-09-06 15:57           ` Jacob Sparre Andersen
2017-09-06 18:56             ` Johan Söderlind Åström
2017-09-06 21:21           ` Randy Brukardt [this message]
2017-09-08 21:04             ` Johan Söderlind Åström
2017-09-08 21:46               ` Dmitry A. Kazakov
2017-09-08 23:55                 ` Johan Söderlind Åström
2017-09-16 12:49                 ` Johan Söderlind Åström
2017-09-16 13:10                   ` Dmitry A. Kazakov
2017-09-17 11:05                     ` AdaMagica
2017-09-17 12:51                       ` Dmitry A. Kazakov
2017-09-08 22:13               ` Jeffrey R. Carter
2017-09-10 21:09                 ` Johan Söderlind Åström
     [not found]                 ` <9d4274d7-5ad1-42d0-8ec3-de822e28ec6c@googlegroups.com>
2017-10-02 19:01                   ` Randy Brukardt
2017-09-08 23:07               ` Randy Brukardt
2017-09-09 20:35                 ` Johan Söderlind Åström
2017-10-02 18:57                   ` Randy Brukardt
2017-09-09 14:48           ` G.B.
2017-09-03  1:35       ` Randy Brukardt
2017-09-03 10:32         ` Alejandro R. Mosteo
2017-09-03 12:59           ` Dmitry A. Kazakov
2017-09-05 22:25             ` Randy Brukardt
2017-09-05 22:29           ` Randy Brukardt
2017-09-02 16:49     ` Jeffrey R. Carter
2017-09-03 10:34       ` Alejandro R. Mosteo
2017-09-02 17:08     ` Jeffrey R. Carter
2017-09-06 16:38       ` Vincent DIEMUNSCH
2017-09-06 20:52         ` Egil H H
2017-09-06 21:30         ` Randy Brukardt
2017-09-08 15:49         ` Jeffrey R. Carter
2017-09-03  1:28     ` Randy Brukardt
2017-09-06  6:15   ` Shark8
2017-09-01 14:11 ` Björn Lundin
2017-09-01 14:47   ` J-P. Rosen
2017-09-01 16:02     ` Dmitry A. Kazakov
2017-09-01 17:53   ` Jacob Sparre Andersen
2017-09-01 18:06     ` J-P. Rosen
2017-09-01 19:02     ` Dmitry A. Kazakov
2017-09-03  1:22   ` Randy Brukardt
2017-09-11 18:49 ` Tarjei Jensen
2017-09-11 19:33   ` Dmitry A. Kazakov
2017-09-12  6:18     ` Tarjei Jensen
2017-09-12  6:38       ` gautier_niouzes
2017-09-12  7:02         ` Tarjei Jensen
2017-09-12  7:15           ` Dmitry A. Kazakov
2017-09-12  7:35             ` Simon Wright
2017-09-12  9:23             ` J-P. Rosen
2017-09-12 10:07               ` Dmitry A. Kazakov
2017-09-12 16:30                 ` Shark8
2017-09-12 16:58                   ` Dmitry A. Kazakov
2017-09-12 19:30                 ` J-P. Rosen
2017-09-12 19:53                   ` Dmitry A. Kazakov
2017-09-12 10:44           ` gautier_niouzes
2017-09-12  6:39       ` Egil H H
2017-09-12  7:02         ` Tarjei Jensen
2017-09-12  7:42           ` Egil H H
2017-09-12  8:24             ` Tarjei Jensen
2017-09-12  8:35             ` Dmitry A. Kazakov
2017-09-12  9:21               ` Egil H H
2017-09-12 10:22                 ` Dmitry A. Kazakov
2017-09-12 10:48                   ` Egil H H
2017-09-12 12:09                     ` Dmitry A. Kazakov
2017-09-12 12:56                       ` Egil H H
2017-09-12 13:14                         ` Dmitry A. Kazakov
2017-09-12 13:25                           ` Egil H H
2017-09-12 13:43                             ` Dmitry A. Kazakov
2017-09-12 14:07                               ` Egil H H
2017-09-12 15:59                                 ` Dmitry A. Kazakov
2017-09-12 14:36                               ` Egil H H
2017-09-12 15:55                                 ` Dmitry A. Kazakov
2017-09-12 16:15                                   ` Egil H H
2017-09-12 16:40                                     ` Dmitry A. Kazakov
2017-09-12 16:47                                       ` Egil H H
2017-09-12 16:59                                         ` Dmitry A. Kazakov
2017-09-12 17:03                                           ` Egil H H
2017-09-12 17:17                                             ` Dmitry A. Kazakov
2017-09-12 17:29                                               ` Egil H H
2017-09-12 19:14                                                 ` Dmitry A. Kazakov
2017-09-12 19:58                                                   ` Egil H H
2017-09-13  7:24                                                     ` Dmitry A. Kazakov
2017-09-13  8:13                                                       ` Egil H H
2017-09-13  8:57                                                         ` Dmitry A. Kazakov
2017-09-13  9:00                                                           ` Egil H H
2017-09-12 18:55                                       ` Simon Wright
2017-09-12 17:43                                 ` Jeffrey R. Carter
2017-09-13 13:07                                   ` Alejandro R. Mosteo
2017-09-13 16:34                                     ` Jeffrey R. Carter
2017-09-13 18:34                                       ` Egil H H
2017-09-13 19:41                                         ` Jeffrey R. Carter
2017-09-14 12:30                                       ` Alejandro R. Mosteo
2017-09-14 13:36                                         ` J-P. Rosen
2017-09-14 18:08                                         ` Jeffrey R. Carter
2017-10-02 19:53                             ` Randy Brukardt
2017-09-12 14:55                           ` Simon Wright
2017-09-12 16:04                             ` Dmitry A. Kazakov
2017-10-02 19:51                           ` Randy Brukardt
2017-10-03  7:48                             ` Simon Wright
2017-10-03  9:34                               ` G.B.
2017-10-03 10:11                                 ` Dmitry A. Kazakov
2017-10-03 13:25                                   ` G.B.
2017-10-03 13:56                                     ` Dmitry A. Kazakov
2017-10-03 20:24                                   ` Randy Brukardt
2017-10-03 20:20                                 ` Randy Brukardt
2017-10-02 19:45                     ` Randy Brukardt
2017-09-12 17:41       ` Jeffrey R. Carter
2017-09-12 21:04   ` G.B.
2017-09-13  7:30     ` Dmitry A. Kazakov
2017-09-13 17:39       ` G.B.
2017-09-13 19:34         ` Dmitry A. Kazakov
2017-09-14  6:57           ` G.B.
2017-09-14  7:33             ` Dmitry A. Kazakov
2017-10-02 10:06 ` reinert
2017-10-02 13:54   ` Jeffrey R. Carter
2017-10-02 14:56     ` reinert
2017-10-02 14:56   ` Dennis Lee Bieber
2017-10-02 15:02   ` G.B.
2017-10-02 16:23     ` reinert
2017-10-02 16:38       ` Dmitry A. Kazakov
2017-10-02 17:19         ` reinert
2017-10-02 17:39           ` Dmitry A. Kazakov
2017-10-02 19:10       ` Jeffrey R. Carter
2017-10-02 19:14       ` Jeffrey R. Carter
2017-10-03  3:30         ` reinert
2017-10-03  6:36           ` G.B.
2017-10-04 19:21             ` reinert
2017-10-04 19:33               ` Dmitry A. Kazakov
2017-10-04 21:45               ` G.B.
2017-10-04 22:04                 ` Dmitry A. Kazakov
2017-10-05  6:50                   ` G.B.
2017-10-05  7:21                     ` Dmitry A. Kazakov
2017-10-04 12:38 ` Daniel Norte Moraes
  -- strict thread matches above, loose matches on Subject: below --
2017-08-03  5:53 Randy Brukardt
2017-08-03  9:42 ` Dirk Craeynest
2017-08-03  9:52   ` Dirk Craeynest
2017-08-05  0:10   ` Randy Brukardt
2017-08-03 12:52 laguest9000
replies disabled

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