Forum > FPC development

[SOLVED] Using high instead of length in rtl/objpas/typinfo.pp

(1/2) > >>

lagprogramming:
rtl/objpas/typinfo.pp has
Function IndexOfEnumeratedAliases(aTypeInfo : PTypeInfo) : integer;
procedure RemoveEnumElementAliases(aTypeInfo: PTypeInfo);
function GetEnumeratedAliasValue(aTypeInfo: PTypeInfo; const aName: string): Integer;

In these routines values like Length(array)-1 are assigned to variables.
The following patch modifies the file to use High instead of Length.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---diff --git a/rtl/objpas/typinfo.pp b/rtl/objpas/typinfo.ppindex da6a889b08..2e6b903465 100644--- a/rtl/objpas/typinfo.pp+++ b/rtl/objpas/typinfo.pp@@ -3666,7 +3666,7 @@   TEnumeratedAliases = record Function IndexOfEnumeratedAliases(aTypeInfo : PTypeInfo) : integer;  begin-  Result:=Length(EnumeratedAliases)-1;+  Result:=High(EnumeratedAliases);   while (Result>=0) and (EnumeratedAliases[Result].TypeInfo<>aTypeInfo) do     Dec(Result); end;@@ -3709,7 +3709,7 @@ procedure RemoveEnumElementAliases(aTypeInfo: PTypeInfo);   A:=EnumeratedAliases[i];   A.Aliases:=Nil;   A.TypeInfo:=Nil;-  L:=Length(EnumeratedAliases)-1;+  L:=High(EnumeratedAliases);   EnumeratedAliases[i]:=EnumeratedAliases[L];   EnumeratedAliases[L]:=A;   SetLength(EnumeratedAliases,L);@@ -3775,7 +3775,7 @@ function GetEnumeratedAliasValue(aTypeInfo: PTypeInfo; const aName: string): Int   Aliases:=GetEnumeratedAliases(aTypeInfo);   if (Aliases=Nil) then     Exit;-  I:=Length(Aliases^)-1;+  I:=High(Aliases^);   While (Result=-1) and (I>=0) do     begin     if SameText(Aliases^[I].Alias, aName) then

PascalDragon:
Done in 5a0ace6d45c27f1629943b89e971d76dc08d9e38

BrunoK:
FPC 3.2.2 x86_64

What is the purpose of the change, is it to gain a few bytes of memory ?

It seems that Length(Array) - 1 generates inline assembly code without any call whereas high(Array) calls <fpc_dynarray_high>




lagprogramming:

--- Quote from: BrunoK on May 27, 2023, 02:14:53 pm ---FPC 3.2.2 x86_64

What is the purpose of the change, is it to gain a few bytes of memory ?

It seems that Length(Array) - 1 generates inline assembly code without any call whereas high(Array) calls <fpc_dynarray_high>

--- End quote ---

EDIT:
I've made a confusion between the assembly of the original code and the assembly of the modified code, most likely due to wrong editor tabs.
In the initial post I've presented the assembly of the original code as the assembly produced for the modified code. I'm updating the text.

Following your post, using a recent FPC development version I've looked at the code produced for the ORIGINAL procedure RemoveEnumElementAliases(aTypeInfo: PTypeInfo);

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---00000000004F5ED0 53                       push rbx00000000004F5ED1 4154                     push r1200000000004F5ED3 488D642488               lea rsp,[rsp-$78]00000000004F5ED8 4889FB                   mov rbx,rdi00000000004F5EDB 4889E7                   mov rdi,rsp00000000004F5EDE 488D35D3193400           lea rsi,[rip+$003419D3]00000000004F5EE5 E8B6C6F3FF               call -$000C394A    # $00000000004325A0 fpc_initialize00000000004F5EEA 488D542410               lea rdx,[rsp+$10]00000000004F5EEF 488D742428               lea rsi,[rsp+$28]00000000004F5EF4 BF01000000               mov edi,$0000000100000000004F5EF9 E882A2F3FF               call -$000C5D7E    # $0000000000430180 fpc_pushexceptaddr00000000004F5EFE 4889C7                   mov rdi,rax00000000004F5F01 E80A81F2FF               call -$000D7EF6    # $000000000041E010 fpc_setjmp00000000004F5F06 89442468                 mov [rsp+$68],eax00000000004F5F0A 85C0                     test eax,eax00000000004F5F0C 0F85FE000000             jnz +$000000FE    # $00000000004F6010 TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+32000000000004F5F12 4889DF                   mov rdi,rbx00000000004F5F15 E8C6FEFFFF               call -$0000013A    # $00000000004F5DE0 TYPINFO_$$_INDEXOFENUMERATEDALIASES$PTYPEINFO$$LONGINT00000000004F5F1A 89C3                     mov ebx,eax00000000004F5F1C 83F8FF                   cmp eax,$FF00000000004F5F1F 0F840B010000             jz +$0000010B    # $00000000004F6030 TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+35200000000004F5F25 4889E6                   mov rsi,rsp00000000004F5F28 488D05D14E6300           lea rax,[rip+$00634ED1]00000000004F5F2F 488B10                   mov rdx,[rax]00000000004F5F32 4863C3                   movsxd rax,ebx00000000004F5F35 48C1E004                 shl rax,$0400000000004F5F39 488D3C02                 lea rdi,[rax+rdx]00000000004F5F3D 488D1574193400           lea rdx,[rip+$00341974]00000000004F5F44 E8D7C9F3FF               call -$000C3629    # $0000000000432920 FPC_COPY00000000004F5F49 48C744247000000000       mov qword ptr [rsp+$70],$0000000000000000004F5F52 488D4C2470               lea rcx,[rsp+$70]00000000004F5F57 488D3512193400           lea rsi,[rip+$00341912]00000000004F5F5E 488D7C2408               lea rdi,[rsp+$08]00000000004F5F63 BA01000000               mov edx,$0000000100000000004F5F68 E87371F3FF               call -$000C8E8D    # $000000000042D0E0 fpc_dynarray_setlength00000000004F5F6D 48C7042400000000         mov qword ptr [rsp],$0000000000000000004F5F75 488D05844E6300           lea rax,[rip+$00634E84]00000000004F5F7C 4C8B20                   mov r12,[rax]00000000004F5F7F 4D85E4                   test r12,r1200000000004F5F82 7409                     jz +$09    # $00000000004F5F8D TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+18900000000004F5F84 4D8B6424F8               mov r12,[r12-$08]00000000004F5F89 4983C401                 add r12,$0100000000004F5F8D 4983EC01                 sub r12,$0100000000004F5F91 488D05684E6300           lea rax,[rip+$00634E68]00000000004F5F98 488B00                   mov rax,[rax]00000000004F5F9B 4863DB                   movsxd rbx,ebx00000000004F5F9E 48C1E304                 shl rbx,$0400000000004F5FA2 488D3418                 lea rsi,[rbx+rax]00000000004F5FA6 488D05534E6300           lea rax,[rip+$00634E53]00000000004F5FAD 488B10                   mov rdx,[rax]00000000004F5FB0 4963C4                   movsxd rax,r12d00000000004F5FB3 48C1E004                 shl rax,$0400000000004F5FB7 488D3C02                 lea rdi,[rax+rdx]00000000004F5FBB 488D15F6183400           lea rdx,[rip+$003418F6]00000000004F5FC2 E859C9F3FF               call -$000C36A7    # $0000000000432920 FPC_COPY00000000004F5FC7 488D05324E6300           lea rax,[rip+$00634E32]00000000004F5FCE 488B10                   mov rdx,[rax]00000000004F5FD1 4963C4                   movsxd rax,r12d00000000004F5FD4 48C1E004                 shl rax,$0400000000004F5FD8 488D3402                 lea rsi,[rax+rdx]00000000004F5FDC 4889E7                   mov rdi,rsp00000000004F5FDF 488D15D2183400           lea rdx,[rip+$003418D2]00000000004F5FE6 E835C9F3FF               call -$000C36CB    # $0000000000432920 FPC_COPY00000000004F5FEB 4D63E4                   movsxd r12,r12d00000000004F5FEE 4C89642470               mov [rsp+$70],r1200000000004F5FF3 488D4C2470               lea rcx,[rsp+$70]00000000004F5FF8 488D3511193400           lea rsi,[rip+$00341911]00000000004F5FFF 488D3DFA4D6300           lea rdi,[rip+$00634DFA]00000000004F6006 BA01000000               mov edx,$0000000100000000004F600B E8D070F3FF               call -$000C8F30    # $000000000042D0E0 fpc_dynarray_setlength00000000004F6010 E8ABA4F3FF               call -$000C5B55    # $00000000004304C0 fpc_popaddrstack00000000004F6015 4889E7                   mov rdi,rsp00000000004F6018 488D3599183400           lea rsi,[rip+$00341899]00000000004F601F E86CC6F3FF               call -$000C3994    # $0000000000432690 FPC_FINALIZE00000000004F6024 837C246800               cmp dword ptr [rsp+$68],$0000000000004F6029 740F                     jz +$0F    # $00000000004F603A TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+36200000000004F602B E840A6F3FF               call -$000C59C0    # $0000000000430670 FPC_RERAISE00000000004F6030 C744246800000000         mov [rsp+$68],$0000000000000000004F6038 EBD6                     jmp -$2A    # $00000000004F6010 TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+32000000000004F603A 488D642478               lea rsp,[rsp+$78]00000000004F603F 415C                     pop r1200000000004F6041 5B                       pop rbx00000000004F6042 C3                       ret The number of call instructions didn't changed but I've noticed that the modified code is smaller. Looking for the most likely problem I've noticed the following lines in the ORIGINAL assembly code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---00000000004F5F75 488D05844E6300           lea rax,[rip+$00634E84]00000000004F5F7C 4C8B20                   mov r12,[rax]00000000004F5F7F 4D85E4                   test r12,r1200000000004F5F82 7409                     jz +$09    # $00000000004F5F8D TYPINFO_$$_REMOVEENUMELEMENTALIASES$PTYPEINFO+18900000000004F5F84 4D8B6424F8               mov r12,[r12-$08]00000000004F5F89 4983C401                 add r12,$0100000000004F5F8D 4983EC01                 sub r12,$0100000000004F5F91 488D05684E6300           lea rax,[rip+$00634E68]Those add & sub instructions look strange. I don't know what's the best way to continue, a bugtracker entry!?

runewalsh:
high generates better code because internally, dynamic array lengths in dynamic array headers store high value directly, not length. Both involve a branch anyway, to check for empty array. (And at least in trunk, seems as if none of them ever call fpc_dynarray_high on i386 and x86_64.)

Navigation

[0] Message Index

[#] Next page

Go to full version