Why does RTTI optimization slow down?

I have an operation that repeats in a loop. With TRttiField:

if (field.name = '') or (field.Name[1] <> 'F') then
  continue;

Profiling shows that I spend a lot of time in UStrAsg and UStrClr because of this. Field.Name must make a virtual call to TRttiInstanceFieldEx.GetName, which must convert the UTF8-to-string to the base name of the RTTI structure. This happens twice per iteration of the loop.

I tried to cut it all out bypassing line conversions:

handle := PFieldExEntry(field.Handle);
if (handle.name = '') or (handle.Name[1] <> 'F') then
      continue;

I expected to see about 5% speed gain from this. Instead, the cycle takes a few seconds longer, about 20-25% slower! I checked the created ASM to make sure it did nothing, how to make string copies from RTTI structures to the local stack, but that is not the case. I see no reason why this should have been slower. Does anyone know what could be here?

+3
source share
1 answer

The field that your new code reads is declared as ShortString. Starting with Delphi 5, the compiler converts ShortStrings to long strings before generating code for most string operations. (At least the way it was with non-Unicode Delphi. Perhaps Unicode Delphi restores some ShortString-related optimizations.)

TRttiField , UTF-8 , , , ShortString-to-string, , , .

. :

handle := PFieldExEntry(field.Handle);
NameP := PAnsiChar(@handle.name);
if (NameP[0] = #0) or (NameP[1] <> 'F') then
  continue;

, ShortString, . 256 . , .

0

Source: https://habr.com/ru/post/1796175/


All Articles