Optimizations

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Optimizations

Post by Armoured »

A more optimized code!
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Optimizations

Post by Psychophanta »

Seconded.
Armoured wrote:A more optimized code!
Yes, more than C please. 8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

+1 although I don't know how optimized the output already is...
Windows 7 & PureBasic 4.4
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Optimizations

Post by freak »

Psychophanta wrote:Yes, more than C please. 8)
lol! :lol:

You do realize that companies developing industrial grade C compilers spend
years with large teams on their code optimisation ?
I wonder how you expect us to compete with that...
quidquid Latine dictum sit altum videtur
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Just use some magic
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

and your fantaisie. :D

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Just add a redundant syntax that you can add just before a procedure: OptimiseProcedureBetterThanCorC++

Or even better a little checkbox in the compiler options that says the same thing...

It doesn't have to do anything at all :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Foz wrote:Just add a redundant syntax that you can add just before a procedure: OptimiseProcedureBetterThanCorC++

Or even better a little checkbox in the compiler options that says the same thing...

It doesn't have to do anything at all :)
Excellent idea! However, I think it's already patented and in use by Microsoft, see here: http://www.purebasic.fr/english/viewtopic.php?t=29526

But we don't care about software patents anyways. :twisted:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I found the checkbox!!!

Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Here's a real-world sample of what can be done (with a LOT of time):

PB code:

Code: Select all

Structure SFrameMap
  StructureUnion
    Alloc.b[1048576/8]
    BitsL.l[0]
  EndStructureUnion
EndStructure

Global Frames.SFrameMap

Procedure UnlockFrame(Frame)
  Index = Frame / 32
  Offset = Frame % 32
  Frames\BitsL[Index] &~ (1 << Offset)
EndProcedure
Currently generated code:

Code: Select all

_Procedure0:
  PUSH   ebp
  PUSH   ebx
  PUSH   edi
  PS0=24
  XOR    eax,eax
  PUSH   eax
  PUSH   eax                                                                                                                                                                                    
; Index = Frame / 32
  MOV    ebx,dword [esp+PS0+0]
  MOV    eax,ebx
  MOV    ebx,32
  CDQ
  IDIV   ebx
  MOV    ebx, eax
  MOV    dword [esp],ebx
; Offset = Frame % 32
  MOV    ebx,dword [esp+PS0+0]
  AND    ebx,31
  MOV    dword [esp+4],ebx
; Frames\BitsL[Index] &~ (1 << Offset)
  LEA    ebp,[v_Frames]
  PUSH   ebp
  MOV    eax,dword [esp+4]
  SAL    eax,2
  POP    ebp
  ADD    ebp,eax
  MOV    ebx,dword [ebp]
  MOV    edi,dword [esp+4]
  MOV    ecx,edi
  MOV    eax,1
  SAL    eax,cl
  MOV    edi,eax
  NOT    edi
  AND    ebx,edi
  PUSH   ebx
  LEA    ebp,[v_Frames]
  PUSH   ebp
  MOV    eax,dword [esp+8]
  SAL    eax,2
  POP    ebp
  ADD    ebp,eax
  POP    eax
  MOV    dword [ebp],eax
; EndProcedure
  XOR    eax,eax
_EndProcedure1:
  ADD    esp,8
  POP    edi
  POP    ebx
  POP    ebp
  RET    4
Optimized code:

Code: Select all

_Procedure0:
MOV     eax, [esp+4]
MOV     ecx, eax
SHR     eax, 5
AND     ecx, 31
MOV     edx, 1
SHL     edx, cl
NOT     edx
AND     [eax*4+frames], edx
RET     4
Out of curiousity, I'm going to see what GCC does as well.

Edit: gcc 3.4.4 with -O3:

Code: Select all

_UnlockFrame@4:
	push	ebp
	mov	eax, -2
	mov	ebp, esp
	mov	ecx, DWORD PTR [ebp+8]
	pop	ebp
	mov	edx, ecx
	and	ecx, 31
	shr	edx, 5
	rol	eax, cl
	and	DWORD PTR _Frames[0+edx*4], eax
	ret	4
That's pretty amazingly fast, but the debugging part of the code is actually WRONG because it creates a stack frame which seems to be destroyed too early.
Also, GCC got a very unfair advantage by having only all variables specified as unsigned. Let's see what it does when everything is signed...

Code: Select all

_UnlockFrame@4:
	push	ebp
	mov	ebp, esp
	mov	ecx, DWORD PTR [ebp+8]
	test	ecx, ecx
	mov	eax, ecx
	js	L5
	pop	ebp
	mov	edx, eax
	sar	edx, 5
	mov	eax, edx
	sal	eax, 5
	sub	ecx, eax
	mov	eax, -2
	rol	eax, cl
	and	DWORD PTR _Frames[0+edx*4], eax
	ret	4
	.p2align 4,,7
L5:
	pop	ebp
	lea	eax, [ecx+31]
	mov	edx, eax
	sar	edx, 5
	mov	eax, edx
	sal	eax, 5
	sub	ecx, eax
	mov	eax, -2
	rol	eax, cl
	and	DWORD PTR _Frames[0+edx*4], eax
	ret	4
Wow, that was big!
It still pops the stack frame too early, so debugging data will report a crash in the wrong function if something goes wrong. The PB code is at least correct.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Re: Optimizations

Post by va!n »

Armoured wrote:A more optimized code!
+1 :!: :wink:
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Re: Optimizations

Post by dell_jockey »

freak wrote: You do realize that companies developing industrial grade C compilers spend years with large teams on their code optimisation ?
I wonder how you expect us to compete with that...
of course, nobody expects Fantaisie to compete with these large outfits. There's a way out though: don't use assembly as an intermediate language but have PB create C, so you can leverage the "years that large teams spend on their code optimisation".
A few of those that elected to go this route: Eiffel, SmartEiffel, GLBasic, BCX, ....
Last edited by dell_jockey on Thu Feb 21, 2008 2:18 pm, edited 1 time in total.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

A few of those that elected to go this route: Eiffel, SmartEiffel, GLBasic, BCX, ....
Compiling a small project in GLBasic took a whopping 21 seconds.
BriceManuel
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Nov 29, 2007 8:23 am

Post by BriceManuel »

Trond wrote:Compiling a small project in GLBasic took a whopping 21 seconds.
Wait until you get something over 10,000 lines.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Trond wrote:Compiling a small project in GLBasic took a whopping 21 seconds.
of course there are disadvantages as well.... but if you're a small outfit and want good code optimisation nonetheless it might be an approach to take.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply