[bug_fixed] Insert "const" for ref-counted paramet
Posted: 11.04.2006, 00:47
The Delphi Compiler will generate faster code if you use the "const" modifier in parameters of the types "string", "variant" and dynamic/open arrays. Also records with such fields can be optimized with a simple "const". (best example: TZVariant)
Example:
This generates:
And now with "const"
You see the difference? So using "const" will improve the speed.
I could do this for you, but that meight be a very large patch.
Example:
Code: Select all
function ArrayToStrings(Value: array of string): TStrings;
var
I: Integer;
begin
Result := TStringList.Create;
for I := Low(Value) to High(Value) do
Result.Add(Value[I]);
end;
Code: Select all
FrmMain.pas.209: begin
00629748 55 push ebp
00629749 8BEC mov ebp,esp
0062974B 83C4F8 add esp,-$08
0062974E 53 push ebx
0062974F 56 push esi
00629750 57 push edi
00629751 8BCA mov ecx,edx
00629753 85C9 test ecx,ecx
00629755 7807 js $0062975e
00629757 8B1C88 mov ebx,[eax+ecx*4]
0062975A 49 dec ecx
0062975B 53 push ebx
0062975C 79F9 jns $00629757
0062975E 8BC4 mov eax,esp
00629760 8955F8 mov [ebp-$08],edx
00629763 8945FC mov [ebp-$04],eax
00629766 8B4DF8 mov ecx,[ebp-$08]
00629769 41 inc ecx
0062976A 8B45FC mov eax,[ebp-$04]
0062976D 8B15F8104000 mov edx,[$004010f8]
00629773 E8E4C5DDFF call @AddRefArray
00629778 33C0 xor eax,eax
0062977A 55 push ebp
0062977B 68CE976200 push $006297ce
00629780 64FF30 push dword ptr fs:[eax]
00629783 648920 mov fs:[eax],esp
FrmMain.pas.210: Result := TStringList.Create;
00629786 B201 mov dl,$01
00629788 A10CE74100 mov eax,[$0041e70c]
0062978D E88AA6DDFF call TObject.Create
00629792 8BF8 mov edi,eax
FrmMain.pas.211: for I := Low(Value) to High(Value) do
00629794 8B75F8 mov esi,[ebp-$08]
00629797 85F6 test esi,esi
00629799 7C13 jl $006297ae
0062979B 46 inc esi
0062979C 8B5DFC mov ebx,[ebp-$04]
FrmMain.pas.212: Result.Add(Value[I]);
0062979F 8B13 mov edx,[ebx]
006297A1 8BC7 mov eax,edi
006297A3 8B08 mov ecx,[eax]
006297A5 FF5138 call dword ptr [ecx+$38]
006297A8 83C304 add ebx,$04
FrmMain.pas.211: for I := Low(Value) to High(Value) do
006297AB 4E dec esi
006297AC 75F1 jnz $0062979f
FrmMain.pas.213: end;
006297AE 33C0 xor eax,eax
006297B0 5A pop edx
006297B1 59 pop ecx
006297B2 59 pop ecx
006297B3 648910 mov fs:[eax],edx
006297B6 68D5976200 push $006297d5
006297BB 8B45FC mov eax,[ebp-$04]
006297BE 8B4DF8 mov ecx,[ebp-$08]
006297C1 41 inc ecx
006297C2 8B15F8104000 mov edx,[$004010f8]
006297C8 E84FC4DDFF call @FinalizeArray
006297CD C3 ret
006297CE E94DAEDDFF jmp @HandleFinally
006297D3 EBE6 jmp $006297bb
006297D5 8BC7 mov eax,edi
006297D7 8B7DEC mov edi,[ebp-$14]
006297DA 8B75F0 mov esi,[ebp-$10]
006297DD 8B5DF4 mov ebx,[ebp-$0c]
006297E0 8BE5 mov esp,ebp
006297E2 5D pop ebp
006297E3 C3 ret
And now with "const"
Code: Select all
function ArrayToStrings({>>} const {<<}Value: array of string): TStrings;
var
I: Integer;
begin
Result := TStringList.Create;
for I := Low(Value) to High(Value) do
Result.Add(Value[I]);
end;
Code: Select all
FrmMain.pas.209: begin
00629748 53 push ebx
00629749 56 push esi
0062974A 57 push edi
0062974B 8BF2 mov esi,edx
0062974D 8BD8 mov ebx,eax
FrmMain.pas.210: Result := TStringList.Create;
0062974F B201 mov dl,$01
00629751 A10CE74100 mov eax,[$0041e70c]
00629756 E8C1A6DDFF call TObject.Create
0062975B 8BF8 mov edi,eax
FrmMain.pas.211: for I := Low(Value) to High(Value) do
0062975D 85F6 test esi,esi
0062975F 7C10 jl $00629771
00629761 46 inc esi
FrmMain.pas.212: Result.Add(Value[I]);
00629762 8B13 mov edx,[ebx]
00629764 8BC7 mov eax,edi
00629766 8B08 mov ecx,[eax]
00629768 FF5138 call dword ptr [ecx+$38]
0062976B 83C304 add ebx,$04
FrmMain.pas.211: for I := Low(Value) to High(Value) do
0062976E 4E dec esi
0062976F 75F1 jnz $00629762
FrmMain.pas.213: end;
00629771 8BC7 mov eax,edi
00629773 5F pop edi
00629774 5E pop esi
00629775 5B pop ebx
00629776 C3 ret
You see the difference? So using "const" will improve the speed.
I could do this for you, but that meight be a very large patch.