One very basic question

Just starting out? Need help? Post your questions and find answers here.
hagibaba
Enthusiast
Enthusiast
Posts: 170
Joined: Fri Mar 05, 2004 2:55 am
Location: UK
Contact:

Post by hagibaba »

This is a good example of how when a procedure is not called it changes the size of the exe. The resulting file sizes are 12kb and 11.5kb, but the only difference in the code is that one is called and the other is not. Just thought I'd add this to clarify.

Code: Select all


Procedure.d Called()
 Protected a.d,b.d,c.d,d.d,e.d,f.d,g.d,h.d,i.d,j.d
 a=1 : b=2 : c=3 : d=4 : e=5 : f=6 : g=7 : h=8 : i=9 : j=10
 a+a : b+b : c+c : d+d : e+e : f+f : g+g : h+h : i+i : j+j
 ProcedureReturn a
EndProcedure

If OpenWindow(0, 100, 200, 195, 260, "PB Window")
 
 MessageRequester("Info", "test1="+StrD(Called()), 0)
 
 Repeat
  EventID = WaitWindowEvent()
  If EventID = #PB_Event_CloseWindow
   Quit = 1
  EndIf
 Until Quit = 1
 
EndIf
End

Code: Select all


Procedure.d NotCalled()
 Protected a.d,b.d,c.d,d.d,e.d,f.d,g.d,h.d,i.d,j.d
 a=1 : b=2 : c=3 : d=4 : e=5 : f=6 : g=7 : h=8 : i=9 : j=10
 a+a : b+b : c+c : d+d : e+e : f+f : g+g : h+h : i+i : j+j
 ProcedureReturn a
EndProcedure

If OpenWindow(0, 100, 200, 195, 260, "PB Window")

 MessageRequester("Info", "test2="+StrD(0), 0)
 
 Repeat
  EventID = WaitWindowEvent()
  If EventID = #PB_Event_CloseWindow
   Quit = 1
  EndIf
 Until Quit = 1
 
EndIf
End

SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

My latest tests show that :
All functions/procedures are included in the final exe.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Functions are included only if they are referenced somewhere (even in a non referenced function). If there is no use at all, it will be skipped.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

But that is at odds with my DeleteFile example, where the Procedure is never
referenced, but the API command is still included as part of the executable.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Yes, only the code body of the function isn't included, not the commands used in it. It should be changed somewhen.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

That's what I thought happened, I knew that as soon as you used any command for the forst time then it was added to the exe. I just thought that first time used commands in unreferenced procedures were not added either, so I wasn't completely wrong. :wink:
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

However, most of the procedures are calling one another.

For example, in an included PB,

Code: Select all

Procedure  GetFormLayout()
EndProcedure  
Procedure  GetDisplayItem()
  GetFormLayout()
EndProcedure  
Procedure  DisplayToScreen()
  GetDisplayItem()
EndProcedure  
When DisplayToScreen() is not used, all the referenced procedures should not be referenced although they are referenced by others, isn't it?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

SkyManager wrote:When DisplayToScreen() is not used, all the referenced procedures should not be referenced although they are referenced by others, isn't it?
yay, what a sentence... 8)

I think it will be quite complex if not even impossible, to implement such in a one-pass-compiler, no?
oh... and have a nice day.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I think it will be quite complex if not even impossible, to implement such
> in a one-pass-compiler, no?

It's not possible with a one-pass compiler.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It's possible, but it would be a lot of work.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

I don't see how it's possible in one pass, even with declares. Care to elaborate?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Fasm's macro feature must be used, so it's not fully one-pass, but the point is that when the progam is compiled, each thing (procedure, string, etc...) has a list of which scopes it is used. At the end, the compiler can look at the information to know what to do. For example:

Code: Select all

Procedure.l IsUsed(Proc)
  If UsedFromMain(Proc)
    ProcedureReturn 1
  Else
    For Each Procedure where Proc is used -> UsedFrom
      If IsUsed(UsedFrom)
        ProcedureReturn 1
      EndIf
    Next
  Endif
  ProcedureReturn 0
EndProcedure

For Each Procedure -> Proc
  If IsUsed(Proc)
    EmitMacroNumber(Proc)
  EndIf
Next
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

> when the progam is compiled, each thing (procedure, string, etc...) has a list of which scopes it is used.

to create this list is one additional pass before the one you call "when it's compiled"
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Kaeru Gaman wrote:> when the progam is compiled, each thing (procedure, string, etc...) has a list of which scopes it is used.

to create this list is one additional pass before the one you call "when it's compiled"
No, this info can be created in the same pass as the compilation. Then the procedures are placed in macros, and the macro is used once at the bottom of the source if the procedure is used.
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post by Thalius »

Dunno if am right here but cant you use ( depending on the Situation ) CompilerIF's to include only the functions you would liek to use?

eg.

Code: Select all

CompilerIf Defined(MYLIB_COMPILER_SERVERMODE,#PB_Constant)
  --> Server Only Functions ...
CompilerElse
  --> Client Functions ...
CompilerEndIf
ofc you had to do it also for teh Declares -- its not too nice but my tests have shown a big difference in sizes. Atm am working on a Game Networklib.

Cheers, Thalius
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
Post Reply