Delphi code optimization tutorial:
String optimization
Delphi has three string types: short string (string[n], n=1..255) The storage area is statically allocated and the size is determined at compile time. This is inherited from bp for The type of dos; character array (pchar) is mainly for compatibility with various APIs. It has appeared in bp7 and is now more widely used in Delphi. Its storage area can be statically allocated with a character array or manually allocated with getmem; while long characters String (ansistring) is unique to Delphi. Its storage area is dynamically allocated at runtime, which is the most flexible and most susceptible to abuse.
Delphi's default string type AnsiString will be automatically initialized to empty without repeated initialization. The following code:
Delphi has three string types: short string (string[n], n=1..255) The storage area is statically allocated and the size is determined at compile time. This is inherited from bp for The type of dos; character array (pchar) is mainly for compatibility with various APIs. It has appeared in bp7 and is now more widely used in Delphi. Its storage area can be statically allocated with a character array or manually allocated with getmem; while long characters String (ansistring) is unique to Delphi. Its storage area is dynamically allocated at runtime, which is the most flexible and most susceptible to abuse.
vars:string;
begin
s:=;
…
end;
s:=; is unnecessary. But it's worth noting that this has no effect on the function return value result. Generally speaking, passing var arguments is faster than returning a string value.
Using SetLength to pre-allocate a long string (AnsiString) to dynamically allocate memory is a major advantage of AnsiString, but it can easily be self-defeating. A typical example is as follows:
s2:= ;
for i:=2 to length(s1) do s2:=s2+s1[i];
Not to mention that it can be replaced by delete. The main problem is that the memory area of s2 is repeatedly allocated in the loop of the above example, which is quite time-consuming. A simple and effective way is as follows:
setlength(s2,length(s1)-1);
for i:=2 to length(s1) do s2[i-1]:=s1[i];
In this way, s2 memory will only be reallocated once.
Thread Safety of Strings and Dynamic Arrays Before Delphi 5, these non-thread-safe calls for operations on dynamic arrays and long strings used reference counting to handle their critical issues. However, since Delphi 5, they have been changed directly to some operations. Prefix the critical instructions with the lock instruction to avoid this problem. Unfortunately, this modification is quite expensive, because the lock instruction in the Pentium II processor is quite time-consuming, and it takes about 28 additional instruction cycles to complete this operation, so the overall efficiency is reduced by at least half. There is only one way to solve this problem, and that is to modify the core code of delphi rtl. After backing up the original file, replace all locks in source/rtl/sys/system.pas with {lock}. Of course, the whole word must be replaced. This has not yet been fully optimized. The next step is to remove the xchg instruction that is also included in the delphi4 runtime library. Because this instruction has an implicit lock prefix, the xchg edx in the _lstrasg and _strlasg processes in system.pas must be removed. Replace [eax] with the following code:
mov ecx, [eax]
mov[eax],edx
mov edx,ecx
ok, you're done, just compile it and overwrite system.dcu. In this way, its execution efficiency will be 6 times higher than delphi5 and 2 times higher than delphi4.