While - Wend code generation

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

While - Wend code generation

Post by luis »

I know it's something really microscopic, but anyway...


I saw the code generated for a loop like this

Code: Select all

 While k < 10    
  k + 1
 Wend
is this

Code: Select all

; While k < 10    
_While1:
  MOV    ebx,dword [v_k]
  CMP    ebx,10
  JGE   _Wend1
; k + 1
  INC    dword [v_k]
; Wend
  JMP   _While1
_Wend1:
wouldn't be better something like this ?

Code: Select all

  
  JMP	 _WhileTest1
_While1:
; k + 1
  INC    dword [v_k]
; Wend
_WhileTest1:
; While k < 10
  MOV    ebx,dword [v_k]
  CMP    ebx,10
  JL    _While1
This way instead of an absolute jump and a conditional one inside the loop, we have only one conditional jump making the work of both and only one executed for loop.

Is less efficient of the original code only if the loop is never executed, certainly the less common scenario.

Unless I'm missing something ...

Bye!


EDIT: well, it's seem it will stay this way... no changes in the new PB version, I checked :)
Last edited by luis on Thu Jan 21, 2010 12:49 am, edited 2 times in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: While - Wend code generation

Post by Kaeru Gaman »

this is an interesting idea, though it's absolutely not the philosophy of the While/Wend loop.

it's called a "Head-Conrolled-Loop", and this implies the condition is located at the head of the loop.

sure, you could say the final location of the condition is purely cosmetic, the functionality is the almost same with your approach.
but for me it just doesn't feel right to have the condition at the bottom of a Head-Conrolled-Loop.

additionally, what is faster, a conditional or an unconditional jump?
I think the unconditional should be faster, and this is the one used for the loop's iterations in the classic approach.
oh... and have a nice day.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: While - Wend code generation

Post by luis »

Hi KG.

It's EXACTLY the same thing. Instructions have been reordered in memory, but it's absolutely a while-wend loop. Try to follow the code and compare the order in which the instructions are executed. It's the same.

The only difference is that in the way I written it there is one less instruction executed for loop, so it should be more efficient (keeping the other instructions constant).

As I said, if I'm not missing something...


About your question: conditional or not have no impact (it's not like a if then goto instead of a goto). I believe the important thing to evaluate a jump is if it has been executed or not, and marginally if it's a near jump or a long one. It's always better to not jump and follow through instead, because jumping alter the efficiency of retrieving the code from the cache, anyway this is not relevant in the code above. And I don't want to talk about branch prediction either!

That's one of the reasons why writing the same code 3 times in sequence is usually faster than include the code in a for k =1 to 3 : next loop.

Anyway, it's a suggestion, Fred will do what he want with it. But frankly changing something so simple from time to time and make the code generated always better should be a good thing no ? Maybe someone else will spot something else in the future and so on... oh well. 8)

Let's see what happen with this first, no sense in looking for more if this will not be considered !
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply