[comp.lang.c] data alignment

chris@trantor.umd.edu (Chris Torek) (02/17/88)

>In article <28200092@ccvaxa> aglew@ccvaxa.UUCP writes:
>>I have often wanted an optimizing compiler that could [rearrange]
>>struct { long a; int b; long c; char d; }
>>... and make it into AAAACCCCBBDx.

In article <496@ecrcvax.UUCP> johng@ecrcvax.UUCP (John Gregor) writes:
>I would like that also, but don't call it a struct.  Rearranging the order
>breaks lots of code and violates K&R p. 196.

Yes.  In fact, I have the perfect name for it, stolen from Craig
Stanfill's thesis hacking days here at U of MD.

If you take a bunch of data items and arrange them in an order,
you have a structured collection of data: a `struct'.  But if
you throw the data items in a carry-sack and shake well, you
have an unordered tangle of data.  Hence the proper name for
an unordered aggregate type: a `bag'.

Next week: the `blender' datatype :-) .
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable

pardo@june.cs.washington.edu (David Keppel) (02/18/88)

[ Compiler rearranges structs for better mem use and/or no holes ]

In article <2309@umd5.umd.edu> chris@trantor.umd.edu (Chris Torek) writes:
>you throw the data items in a carry-sack and shake well, you
>have an unordered tangle of data.  Hence the proper name for
>an unordered aggregate type: a `bag'.

"Bag" is already defined (at least in Smalltalk ;-) as a set type that
allows replicated elements.  This also allegedly appears occasionally
in mathematics.

In a set, each element appears at most once.	{ Arf Barf Bork }
In a bag, elements can appear several times.	{ Arf Arf Barf Bork Bork }

>Next week: the `blender' datatype :-) .
Now _this_ I want to see!  ;-)

	;-D on  (Blender bender spender ender - no tab)  Pardo

ok@quintus.UUCP (Richard A. O'Keefe) (02/18/88)

In article <2309@umd5.umd.edu>, chris@trantor.umd.edu (Chris Torek) writes:
> >In article <28200092@ccvaxa> aglew@ccvaxa.UUCP writes:
> >>I have often wanted an optimizing compiler that could [rearrange]
> >>struct { long a; int b; long c; char d; }
> >>... and make it into AAAACCCCBBDx.
> Yes.  In fact, I have the perfect name for it, stolen from Craig
> Stanfill's thesis hacking days here at U of MD. ... Hence the
> proper name for an unordered aggregate type: a `bag'.
> 
Please don't do that.  "bag" is a very common name for the "multiset"
data type.  (That is, a homogeneous collection like a set, except that
items can be present more than once.)

There is a trivial solution, and a non-trivial solution.
If you put
	all doubles, arrays of doubles, or structs with double elements first,
then	all floats, arrays of floats, or structs with float elements,
then	all pointers, arrays of pointers, or structs with pointer elements,
then	all longs, arrays of longs, or structs with long elements,
then	all ints, arrays of ints, or structs with int elements,
then	all shorts, arrays of shorts, or structs with short elements,
then	all chars, arrays of chars, or structs with char elements
the resulting 'struct' will be reasonably packed on a wide range of machines.
That is the trivial solution.

The non-trivial solution is to write your structure declaration in two
steps.  Suppose you have a collection of typedefs and macros in a file
header.h, and a collection of struct declarations you would like to
optimise.  Now write another program which does includes header.h and
uses the sizeof() information to determinate an appropriate order for
each struct, and have that program write out a structs.h file with the
optimised declarations.

chris@trantor.umd.edu (Chris Torek) (02/19/88)

>In article <2309@umd5.umd.edu> I said
>>an unordered aggregate type: a `bag'.

In article <661@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard
A. O'Keefe) writes:
>Please don't do that.  "bag" is a very common name for the "multiset"
>data type.  (That is, a homogeneous collection like a set, except that
>items can be present more than once.)

[which Dave Pardo also mentioned]

Yes; and indeed, that is what Craig's `bags' were.  I was largely kidding
(as I intended to imply with the remark about a `blender' datatype).

>There is a trivial solution, and a non-trivial solution.

[deleted; both involve ordering structures from largest-to-smallest
types]

It seems not unreasonable, though, to have a language keyword to
tell the compiler to do this for you.  (Please do not suggest
this seriously to the X3J11 committee.  The dpANS describes a
language sufficiently different from C already.)
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable