Delphi code optimization (character processing)

xiaoxiao2021-03-05  18

Delphi has three string types: short string (string [n], n = 1..255) storage area is static allocation, the size is determined when compiling, which is inherited in the type of BP for DOS; character array (PCHAR) It is mainly to be compatible with various types of APIs, which have appeared in BP7, and now more applications in Delphi, and the storage area can be staticly allocated with a character array, or it can be manually assigned by getMem; while long strings (ANSISTRING) is Delphi unique The stored area is dynamically allocated at runtime, the most flexible is also most easily abused. Do not repeat the initialization of Delphi default string type ANSISTRING automatically initializes empty. As follows: var s: string; begin s: = ''; ... End; s: = ''; there are more than one. But it is worth noting that this is invalid if the function return value Result is invalid. In general, the use of VAR-in-arguments is more faster than returning string values. Using SETLENGTH pre-allocation of long strings (ANSISTRING) dynamic allocation of memory is a large length of Ansistring, but it is easy to get into a typical example: S2: = '; for i: = 2 to Length (S1) DO S2: = S2 S1 [I]; and not to say that DELETE can be replaced, the main problem is that the memory area of ​​S2 in the loop in the previous example is constantly repeated, and it is quite a fee. A simple and effective approach is as follows: setLength (S2, Length (S1) -1); for i: = 2 to length (S1) DO S2 [I-1]: = S1 [i]; this S2 memory will only be redistributed once. String and dynamic array thread security (Thread Safety) Dynamic array in Delphi 5 This non-threaded security call is handled by the reference count to handle its critical problems, and it is changed directly from Delphi5. Critical instructions plus the LOCK instruction prefix to avoid this problem. Unfortunately, this modification is quite expensive, because at the PentiumII processor, the Lock instruction is quite cost, and the extra 28 instruction cycles is taken to complete this operation, so the overall efficiency is at least half. The way to solve this problem is only one, that is, modify the Delphi RTL core code. After backing up the original file, replace all LOCK in Source Tlsyssystem.PAS is {Lock}, of course, must be an internal change. The next step is not fully optimized, the next step is to remove the XCHG instructions in the Delphi4 running library, because the instruction has an implicit Lock prefix, so you must use the system.pas _lstrasg and _Strlasg xchg EDX, [EAX] Replace with the following code: MOV ECX, [EAX] MOV [EAX], EDX MOV EDX, ECX OK is very successful, compiled, override the system.dcu. So, the performance efficiency will be increased by 6 times higher than Delphi5, 2 times higher than Delphi4. Avoid using short strings Since a lot of string operations will convert short strings to a long string, slowing down the execution speed, so it is still less short-character strings. Avoid using a COPY function This is also related to abuse of memory management. A typical situation is as follows: if Copy (S1, 23, 64) = COPY (S2, 15, 64) Then ... thus caused two temporary memory, thus reducing efficiency.

转载请注明原文地址:https://www.9cbs.com/read-38412.html

New Post(0)