Re: Milestones document




--
> From: Elliot Lee <sopwith@redhat.com>
> Date: Wed, 8 Dec 1999 14:36:59 -0500 (EST)
> To: Jim Gettys <jg@pa.dec.com>
> Cc: gnome-list@gnome.org
> Subject: Re: Milestones document
> -----
> On Wed, 8 Dec 1999, Jim Gettys wrote:
> 
> > For example, the next time you break library binary compatibility, you
> > go through all data structures and sort the structure elements small to
> > large, (often with subunits of information that ought to be local in a
> > cache line)
> 
> Small to large or large to small? (Or does it make a diff as long as it is
> sorted?)

Small to large: all C compilers in the world lay out memory in the order
of declaration, and they keep data naturally aligned.  This is universally
true.  While vital on a RISC, CISC's like an x86, while not causing a trap,
will take a significant performance hit on unaligned data, so compilers
have kept data naturally aligned by default for a long time (even a VAX
took a significant performance hit if the data wasn't aligned, and it has
gotten amazingrly worse as processors have gotten faster (and more RISC
like internally on CISC machines).

Example program length.c:
#include <stdio.h>

typedef struct _foo {
  char character4;
  char *pointer;
  short integer;
  char *pointer2;
  char character2;
} foostruct;

typedef struct _foo2 {
  char character;
  char character2;
  short integer;
  char *pointer;
  char *pointer2;
} foostruct2;

main()
{
  printf("size = %d, %d\n", sizeof(foostruct), sizeof(foostruct2));
  exit();
}

[jg@jg jg]$ ./length
size = 20, 12

And even more of a difference on a 64 bit system, potentially (the holes
are larger to keep the pointers naturally aligned.

> 
> This also may not help as much as theoretically possible, because of
the
> structure concatenation used for inheritance in Gtk+.
> 
> I was looking at gnome-libs, and while there are some nice places to
> reduce memory, a lot of the structure elements are pointers, for which no
> improvement is possible.

How much this can help varies wildly from one code to another, but since
it is so low cost, it is something to keep in mind anytime you write
a structure down.  In my contrived example, it is almost a factor of two
even on an x86.  Your milage will vary from zero to a theoretical almost
factor of two.

I just generally pack structures well when I write them down the
first time, but then again, I do wire protocols :-).

				- Jim



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]