Delphi language optimization
1) Return value of record or array type
C++ programmers won't do this because the return value will be pushed onto the stack, resulting in both time and space efficiency. Delphi does not have this problem. The return value is equivalent to the Out parameter, and the caller passes the memory unit pointer. Therefore, you can use record or array return values to increase readability and convenience in writing expressions.
2) Public expression
Delphi only extracts common expressions within a statement line. If a common expression contains a function call or property access, it is not extracted, i.e., it is evaluated twice, because extraction may result in changed semantics.
3) const parameters
When the function parameters are not changed, if the parameter length exceeds 4 bytes or is an interface, string, or dynamic array type, using const parameters allows the compiler to pass it in address mode without modifying the reference technology.
4) Austerity
Delphi will allocate as small a space as possible when allocating storage units. If it is small enough and can fit in a register, direct operation with the register is generally used. For example, when assigning records or data with a length of 4 bytes, the compiler will automatically use 32-bit registers according to DWord.
5) Operator
Dividing by 2 or 4 will be optimized to right shift, and multiplying by 2 or 4 will be optimized to left shift, so there is no need to specifically use shift operations, and more readable code can be written. But multiplication and division by 8 and other multiples of 2 will not have this optimization.
i := i + 1; and Inc(i); generally produce the same code.
6) Floating point
Delphi does not optimize floating point calculations, even adding zero operations will not be optimized, and many FWAIT instructions will be added. Therefore, when writing key floating-point operations, you must optimize the expressions yourself and extract the public expressions yourself. Use intermediate variables as little as possible, because floating-point variables will not be optimized and will be written back to memory even if they are not used later. Use assembly if necessary, or consider using C. Nonetheless, in general, there is not much difference in performance between equivalent Delphi and C floating-point programs.
7) with statement
with can generate implicit intermediate variables to avoid repeated calculations
8) Collection
i in [0..31] is faster than (i >= 0) and (i <= 31)
9) Bit width
When compression is not used, use integer and cardinal, because SmallInt, ShortInt, Byte, etc. will also occupy 4 or 8 bytes when the byte is aligned. It is still 32 bits in calculation and must be expanded to 32 bits first.