comp.lang.ada
 help / color / mirror / Atom feed
* Discriminants constraining unconstrained array types
@ 2001-10-20 15:49 Matthew Woodcraft
  2001-10-20 17:39 ` James Rogers
  0 siblings, 1 reply; 5+ messages in thread
From: Matthew Woodcraft @ 2001-10-20 15:49 UTC (permalink / raw)



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?

-M-



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

* Re: Discriminants constraining unconstrained array types
  2001-10-20 15:49 Discriminants constraining unconstrained array types Matthew Woodcraft
@ 2001-10-20 17:39 ` James Rogers
  2001-10-20 19:04   ` Damien Carbonne
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: James Rogers @ 2001-10-20 17:39 UTC (permalink / raw)


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



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

* Re: Discriminants constraining unconstrained array types
  2001-10-20 17:39 ` James Rogers
@ 2001-10-20 19:04   ` Damien Carbonne
  2001-10-20 20:55   ` Larry Kilgallen
  2001-10-20 21:43   ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Damien Carbonne @ 2001-10-20 19:04 UTC (permalink / raw)


James Rogers a �crit :

> 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;
>

I think what you explain further is right only if the declaration was:

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

Note: "1" could be replaced by any other valid value.
The issue is with default values and declarations without constraint.






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

* Re: Discriminants constraining unconstrained array types
  2001-10-20 17:39 ` James Rogers
  2001-10-20 19:04   ` Damien Carbonne
@ 2001-10-20 20:55   ` Larry Kilgallen
  2001-10-20 21:43   ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Larry Kilgallen @ 2001-10-20 20:55 UTC (permalink / raw)


In article <3BD1B6F3.F90883D7@worldnet.att.net>, James Rogers <jimmaureenrogers@worldnet.att.net> writes:

> 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.

Why is that the only reasonable option ?



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

* Re: Discriminants constraining unconstrained array types
  2001-10-20 17:39 ` James Rogers
  2001-10-20 19:04   ` Damien Carbonne
  2001-10-20 20:55   ` Larry Kilgallen
@ 2001-10-20 21:43   ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2001-10-20 21:43 UTC (permalink / raw)


James Rogers wrote:
> 
> 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.)
> >
> 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;

I am not aware of any compilers that generate unreasonable code for such
a construct. They all size objects of such a type based on the actual
subtype. A memory problem frequently occurs if the type has a default
discriminant and unconstrained objects are created, but the rule goes
beyond excluding such types/objects.

-- 
Jeff Carter
"Perfidious English mouse-dropping hoarders."
Monty Python & the Holy Grail



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

end of thread, other threads:[~2001-10-20 21:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-20 15:49 Discriminants constraining unconstrained array types Matthew Woodcraft
2001-10-20 17:39 ` James Rogers
2001-10-20 19:04   ` Damien Carbonne
2001-10-20 20:55   ` Larry Kilgallen
2001-10-20 21:43   ` Jeffrey Carter

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