[fpc-devel] Generics - a general question

Sven Barth pascaldragon at googlemail.com
Sat Nov 19 14:46:00 CET 2011


On 19.11.2011 09:33, AvP wrote:
>> Simply replacing "object" by "class" is not sufficient, because classes
>> need to be used differently than objects.
> Right, it actually needs a bit of modification in the destructor. ;-)

I haven't yet tried to replicate your example with classes, so I can't 
say what exactly needs to be changed.

>> objects aren't used by that many persons
> As I started / learned programming with TurboPascal, I habitually use objects,
> but is there a big difference for "the normal user", if advanced class
> features aren't used?
> Is there a difference in speed / memory consumption / code size between
> objects and classes?

The instance sizes themselves should be rather similar between classes 
and objects, but for every class type always a VMT is created, while for 
objects at least one virtual method needs to exist for that. Also 
classes should be more easy to use as they are implicit pointers. See here:

=== source begin ===

type
   TSomeObject = object
   end;
   PSomeObject = ^TSomeObject;

   TSomeClass = class
   end;

var
   o: PSomeObject;
   c: TSomeClass;
begin
   o := New(PSomeObject);
   Dispose(o);
   c := TSomeClass.Create;
   c.Free;
end.

=== source end ===

The advantage (if one needs that) of objects is that they can live on 
the stack (like records) and that you can declare constant objects (like 
records).

Speedwise classes and objects should be rather similar.

Classes can make use of Runtime Type Information. Thus you can query the 
name of the class by just using "c.ClassName" or for the class type by 
"c.ClassType". I don't know whether the "is" and "as" operators work for 
objects as well, but with them you can check whether a instance variable 
contains a certain subclass (is) or you can do a checked typecast (as) 
which raises an exception if the left side is not a subclass of the 
right side. Also classes can contain interfaces thus you can program to 
an interface instead of an implementation (which is sometimes seen as a 
cleaner way of object oriented programming).

>
> Back to generics:
> How much does a specialized generic class / object differ from a class /
> object, that is directly programmed (not as generic), in the binary code ?
>
> The reason for my question is that I often read that you shouldn't use
> generics if a programm heavily depends on speed (like 3D-games). So is this
> really an issue in FreePascal (like it seems to be in C-like languages) and
> how big is the difference in speed - if there is any?
>

I don't know how generics are implemented in C(++), but in FPC it 
basically works like this:

* compiler parses a generic, but instead of generating code it stores 
all tokens in a token buffer inside the PPU file
* compiler parses a specialization; for this it loads the token buffer 
from the PPU file and parses that again, but replaces the generic 
parameters (in most examples "T", in your example "_ResType") by the 
given type (e.g. LongInt, TObject, or your TType). Thus the code 
basically appears as if you had written the same class as the generic 
but with T replaced by your given type.

So in theory there should be no speed differences.

Regards,
Sven



More information about the fpc-devel mailing list