For some reason the Delphi compiler (except some very simple cases, e.g. for Integer parameters) doesn't detect if the variable passed as an argument is being modified inside procedure/function. Even the newest Delphi XE2 behaves in the same way.
Using "const" is important even for non-reference-counted variables larger than 4 bytes - it prevents copying the arguments at the beginning of the procedure/function:
Code: Select all
type
TTestRec = record
X1 : Integer;
X2 : Integer;
X3 : Integer;
X4 : Integer;
end;
function Test1(Rec : TTestRec) : Integer;
begin
with Rec do
Result:=X1+X2+X3+X4;
end;
function Test2(const Rec : TTestRec) : Integer;
begin
with Rec do
Result:=X1+X2+X3+X4;
end;
Code: Select all
Test2 proc near
mov edx, [eax]
add edx, [eax+4]
add edx, [eax+8]
add edx, [eax+0Ch]
mov eax, edx
retn
Test2 endp
Code: Select all
Test1 proc near
var_RecCopy_X1 = dword ptr -18h
var_RecCopy_X2 = dword ptr -14h
var_RecCopy_X3 = dword ptr -10h
var_RecCopy_X4 = dword ptr -0Ch
push esi
push edi
add esp, 0FFFFFFF0h
mov esi, eax
lea edi, [esp+18h+var_RecCopy_X1]
mov ecx, 4
rep movsd ; COPYING
mov eax, [esp+18h+var_RecCopy_X1]
add eax, [esp+18h+var_RecCopy_X2]
add eax, [esp+18h+var_RecCopy_X3]
add eax, [esp+18h+var_RecCopy_X4]
add esp, 10h
pop edi
pop esi
retn
Test1 endp