[fpc-devel] Class field reordering

Nico Erfurth fpc at erfurth.eu
Sat Jul 14 20:29:27 CEST 2012


On 14.07.12 01:44, Jonas Maebe wrote:

> I've implemented an optimization that reorders the instance fields of
> Delphi-style classes (and only of Delphi-style classes) to minimise
> memory gaps caused by alignment differences and odd sizes. The effect is
> the same as when you would change the order of the fields in the source
> code to achieve this effect.
> 
> In general, I think this should be safe since unlike records and
> TP-style objects, a Delphi class is normally never blockwritten to disk
> or so. There is a switch to disable this transformation, but I'm
> wondering whether anyone sees a problem with enabling it by default when
> -O2 or higher is used.

As this might cause an overall gain by better cache utilization i would
keep it on by default. But I think it would be best that it can be
disabled per class-definition. If somebody tries to do manual cache
optimization it will usually only be for a couple of classes. Not the
whole project.

So basically, something like

class MyClass = class
{$OPTIMIZATION OFF}
  private
    FHeavilyUsedValue: Boolean;
    FAlsoHeavilyUsedValue: DWord;
{$OPTIMIZATION DEFAULT}
    NotOftenUsed: Byte;
end;

should leave the ordering of the first values alone, while optimizing
the rest. Or at least disable reordering for the whole class.

Nico



More information about the fpc-devel mailing list