Weird bug causing slowdown with asm compiler

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Weird bug causing slowdown with asm compiler

Post by novablue »

Hi, i have a weird bug that i can not really reproduce in a small code, i can only try to explain what happens and how i "fixed" it, but maybe it can still be of help.

I basically have this code where i call a procedure using the string output from another procedure. When i call DoSomething() in the following pseudo code the procedure for some reason runs very slow taking 10 seconds to finish. It also only happens with the ASM compiler, i switched to the c compiler and the bug was gone.

Code: Select all

Procedure.s GetString(ID.i)
    Select ID
        Case 1
            ProcedureReturn "Hello"
        Case 2
            ProcedureReturn "World"
    EndSelect
EndProcedure

Procedure DoSomething(String$)
    ;Processing really slow
    Debug String$
EndProcedure

DoSomething(GetString(1))
Then i changed the code to where i first put the result from GetString() into a temporary variable and then call DoSomething(). Now it finishes the exact same task in less than 1 second with the asm compiler.

Code: Select all

Procedure.s GetString(ID.i)
    Select ID
        Case 1
            ProcedureReturn "Hello"
        Case 2
            ProcedureReturn "World"
    EndSelect
EndProcedure

Procedure DoSomething(String$)
    ;Processing fast
    Debug String$
EndProcedure

Result$ = GetString(1)
DoSomething(Result$)
So yeah again those example codes don't reproduce the problem when they are run, it just me trying to represent the way it happens in my large project. i assume it is some weird bug in the asm compiler related on how return values get passed on since it works fast with both variants using the c backend for me. :?

Tested with PB 5.73 x64 and PB 6.00 x64
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Weird bug causing slowdown with asm compiler

Post by mk-soft »

Both run equally fast here.
Window 10 (21H2).

But I know the problem and already have the solution. Take Purbasic into the exclusion list of virus scanners.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
juergenkulow
Enthusiast
Enthusiast
Posts: 556
Joined: Wed Sep 25, 2019 10:18 am

Re: Weird bug causing slowdown with asm compiler

Post by juergenkulow »

Code: Select all

;RDTSC—Read Time-Stamp Counter
Procedure.s GetString(ID.i)
  Select ID
    Case 1
      ProcedureReturn "Hello"
    Case 2
      ProcedureReturn "World"
  EndSelect
EndProcedure

Procedure DoSomething(String$)
  ;Processing fast
  Debug String$
EndProcedure

CompilerIf Defined(PB_Compiler_Backend,#PB_Constant)
  CompilerIf #PB_Compiler_Backend=#PB_Backend_C
    Procedure GetRDTSC()
      Protected t.q
      !__asm__ __volatile__ (".intel_syntax noprefix;"
      !"RDTSC;"
      !"shl rdx, 32;"
      !"Or rax, rdx;"
      !"MOV %0, rax;"
      
      !".att_syntax"
      !
      !:"=r" (v_t)
      !:
      !: "rax"
      !);  
      ProcedureReturn t
    EndProcedure
  CompilerElse
    Procedure GetRDTSC()
      EnableASM:DisableDebugger
      rdtsc
      shl rdx, 32
      Or rax, rdx
      DisableASM : EnableDebugger
      ProcedureReturn
    EndProcedure  
  CompilerEndIf
CompilerElse
  Procedure GetRDTSC()
    EnableASM:DisableDebugger
    rdtsc
    shl rdx, 32
    Or rax, rdx
    DisableASM : EnableDebugger
    ProcedureReturn
  EndProcedure
CompilerEndIf

Define t1,t2,t3
t1=GetRDTSC()
Result$ = GetString(1)
DoSomething(Result$)
t2=GetRDTSC()
DoSomething(GetString(1))
t3=GetRDTSC()

Debug "Result$ = GetString(1) : DoSomething(Result$)  CPU time cycles:"+Str(t2-t1)
Debug "DoSomething(GetString(1)) CPU time cycles:"+Str(t3-t2)
; ASM Linux x64
; Hello
; Hello
; Result$ = GetString(1) : DoSomething(Result$)  CPU time cycles:66659
; DoSomething(GetString(1)) CPU time cycles:31268

; C Backend Optimize Linux x64
; Hello
; Hello
; Result$ = GetString(1) : DoSomething(Result$)  CPU time cycles:75224
; DoSomething(GetString(1)) CPU time cycles:39431
I hope you find the 10 seconds with it.
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Weird bug causing slowdown with asm compiler

Post by Fred »

Unfortunately we can't investigate without more info
Post Reply