Smaller, cleaner RTTI coming?

One of the biggest complaints about the extended RTTI introduced in Delphi 2010 is the way it adds so much to the size of your EXE.  Well, in a recent StackOverflow answer, Barry Kelly hinted that the format of the basic RTTI structures in TypInfo.pas are “more likely to change from version to version now that it has a much higher level abstraction in the Rtti unit.”

I mentioned in a comment that I wouldn’t like to see much change in the RTTI data structures because it would make dynamic class creation code difficult to maintain, and I’ve been doing some serious work in that area recently. Barry responded with the rationale:

It’s less a case of completely changing, than having more data appended on the end. It’s a real PITA to jam in extra data without breaking code. As it is, the RTTI format contributes a fairly substantial size penalty largely because its encoding can’t be optimized much. It would be nice to consider something like e.g. .NET metadata tables, which use a conciser representation. Instead, Delphi RTTI is left with every xref taking up a minimum of 4 bytes, and frequently with redundant strings for names that could be pooled. But that can’t happen within the current framework.

He mentions two things here: changing the way references work to make the RTTI take up less space, and making it less difficult to update the RTTI structures with new information.  I could get behind something like that, especially since the update would almost by necessity involve cleaning up the type definitions in the process.  It’s a real pain to work with some of the existing RTTI structures, because you get stuff like this:

{ vmtFieldTable entry in VMT }
  PVmtFieldTable = ^TVmtFieldTable;
  TVmtFieldTable = packed record
    Count: Word; // Published fields
    ClassTab: PVmtFieldClassTab;
   {Entry: array[1..Count] of TVmtFieldEntry;
    ExCount: Word;
    ExEntry: array[1..ExCount] of TVmtFieldExEntry;}


Why are those last three fields commented out?  Because they’re declared as static arrays, but the upper bound isn’t known at compile time and changes from one instance to another.  Basically, they contain dynamic arrays inserted inline into the record, which is something that Delphi’s type system doesn’t support.  And since the type system doesn’t support it and the compiler can’t locate the members of the records, extracting data from a TVmtFieldTable (or several other structures that do similar things) is a real pain.

RTTI that’s easier to work with and takes up less space in the EXE?  If they can pull it off, I’d love to see it!

One Comment

Leave a Reply