comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Discriminants constraining unconstrained array types
Date: Sat, 20 Oct 2001 17:39:43 GMT
Date: 2001-10-20T17:39:43+00:00	[thread overview]
Message-ID: <3BD1B6F3.F90883D7@worldnet.att.net> (raw)
In-Reply-To: 87g08e9urn.fsf@chiark.greenend.org.uk

Matthew Woodcraft wrote:
> 
> The following rule appears in the GNAT coding style guide:
> 
> | Do not declare discriminated record types where the discriminant is
> | used for constraining an unconstrained array type. (Discriminated
> | records for a variant part are allowed.)
> 
> Does anyone know a reason to avoid this feature of the language in
> general, or is it more likely that there's just some issue with the
> runtime or bootstrap behaviour which makes it inadvisable to use in the
> compiler itself?

I was not involved in the team that wrote this guideline.

I can explain some of the problems with declaring discriminated
record types used for constraining unconstrained array types.

The first problems is memory usage. Take the following example:

type Unconstrained_Type is array(Positive range <>) of Integer;

type Inadvised_Record_Type (Max : Positive) is record
   Buffer : Unconstrained_Type(1..Max);
end record;

The compiler must identify the size of Inadvised_Record_Type.
The problem arises because the array constraints are determined
at run-time and not at compile-time. The only vaguely reasonable
option for the compiler is to set Buffer'Size equal to
Positive'Last * Integer'Size. This means that you will be wasting
large amounts of memory for every instance of Inadvised_Record_Type.

This also causes problems when you want to write an instance of
Inadvised_Record_Type to a stream. The size written to the stream
will be the entire buffer size, not simply Buffer'Length
Integer, plus another Integer for the Max discriminant. If you are
sending this stream representation across a communication link
you will get very poor throughput due to the excessive number of
extra Integer data elements sent corresponding to the wasted
buffer space.

The point is that individual data elements in a record type should
be of a fixed or constrained size.

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-10-20 17:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-20 15:49 Discriminants constraining unconstrained array types Matthew Woodcraft
2001-10-20 17:39 ` James Rogers [this message]
2001-10-20 19:04   ` Damien Carbonne
2001-10-20 20:55   ` Larry Kilgallen
2001-10-20 21:43   ` Jeffrey Carter
replies disabled

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