Save object, stop class hegemony!
By A.Bouchez on 2010, Friday August 6, 11:31 - Pascal Programing - Permalink
In a recent thread in the Embarcadero
Discussion Forums, Vedran Vuk posted some questions about object
keyword.
His purpose was to use object instead of class to improve speed and memory
consumption:
I do use classes. I just want to use objects on smaller classes that don't really need initialization or RTTI. Plus, I can directly operate on it like a record with no need for constructors and it can be sealed and has inheritance. Every byte matters in this case.
I have the same requirement sometimes, for example for our framework or for low-level units.The "object" type is deprecated. As was said, it mainly exists for compatibility with old Turbo Pascal. That is why it is not documented very well. It's use is not promoted.
Delphi has always had three kind of coders:
- high-level coders, using components and RAD, standard library and its classes;
- low-level coders, using pure code SW development, looking at the asm code generated and tracking down memory and CPU usage;
- and to be honest, all low-level coders use the high-level tools, because for a simple form, RAD is great, and standard library is to be used anyway.
For example, the whole KOL library relies and it, and performs a great job with it. The XMLRad framework uses it a lot for all basic types, in order to increase speed, and improve multi-threaded server response time.
- a memory mapped file, which I want to parse very quickly: a pointer to such an object is just great, and you still have methods at hand; I use this for TFileHeader or TFileInfo which map the .zip header, in SynZip.pas;
- a Win32 structure, as defined by a API call, in which I put handy methods;
- a temporary structure defined on the stack, just used during a procedure: I use this for TZStream in SynZip.pas, or for our RTTI related classes, which map the Delphi generated RTTI in an Object-Oriented way not as the TypeInfo which is function/procedure oriented. By mapping the RTTI memory content directly, our code is faster than using the new RTTI classes created on the heap. We don't instanciate any memory, which, for an ORM framework like ours, is good for its speed. We need a lot of RTTI info, but we need it quick, we need it directly.
See http://qc.embarcadero.com/wc/qcmain.aspx?d=79792
I've an Internal Error DT5830 under Delphi 2009 Update 3, but previous compiler versions (including Delphi 2009 Update 2) did accept a packed object declaration.
There is a simple workaround: define it as packed record... this works, but is not compatible with previous version of the compiler.
So here is how such a packed object must be coded, if you want your code to work on most Delphi compiler versions:
typeIt's a pity that Embarcadero seems not to a have a full regression test of all compiler/language features, and run it before every release, like Free Pascal compiler does.
{$A-} { force packed object (not allowed under Delphi 2009) }
/// the internal memory structure as expected by the ZLib library
TZStream = {$ifdef UNICODE}record{$else}object{$endif}
...
So here is my today's claim:
Don't forget low-level Delphi coders! Save our object type!