Code optimization

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Code optimization

Post by Danilo »

c4s wrote:@Danilo
I was looking for a more "integrated" solution. Replacing files of the compiler just doesn't feel right
Fred could integrate it into PB after writing the ASM file. PB always outputs the ASM file, so PB itself could do the (optional) re-writing of the ASM before calling FAsm.
Doable as an optional optimization pass over the ASM output. Alternatively allow 3rd party compiler plugins (as DLL/Shared lib) to work on source and output files.

pbcompiler.exe outputs ASM, and it is given to FAsm - so an external ASM output optimizer must reside between pbcompiler and FAsm.
Let me know if you have a better idea than replacing FAsm with the optimizer.

Now: pbcompiler.exe -> FAsm.exe
New: pbcompiler.exe -> ASM optimizer -> FAsm.exe
c4s wrote:plus the functionality cannot easily be enabled/disabled.
That could be easily added with a macro in a resident. I'm not on Windows now, but something like the following should work:

Code: Select all

Macro RemoveDeadProcedures
!define remove_dead_procedures
EndMacro
So you would just need to write "RemoveDeadProcedures" somewhere in your PB source.

In the DeadProcedureRemover.pb file, add a line to detect the "define remove_dead_procedures" in the ASM,
and re-write the output only if this line was found.
I did something similar before, it is the following commented part in the code:

Code: Select all

        ;ElseIf LCase(trimmedLine$) = "define show_optimize_info"
        ;    showResult = 1
        ;    Continue
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Code optimization

Post by Tenaja »

c4s wrote:@Danilo
I was looking for a more "integrated" solution. Replacing files of the compiler just doesn't feel right, plus the functionality cannot easily be enabled/disabled. Thanks for your suggestion though.
IIRC, I believe I read Fred suggest this same solution (rename/replace FASM) once, a few years ago. There is nothing wrong with it, short of the hassle of repeating the replacement after a fresh PB installation.

[edit] You could always ask Fred & Co. to add a field in the Preferences to run another file before running FASM.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Code optimization

Post by c4s »

Thanks both of you for the suggestions/workarounds.

I guess to easily integrate it into the IDE a new "Event to trigger the tool" would be needed. Currently there's only "Before Compile/Run" and "After Compile/Run". Obviously something in between is needed... Fred/Freak, what do you think?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Code optimization

Post by c4s »

I tried to get Danilo's tool working again because I remembered that back then it produced greatly size-optimized results.

Unfortunately PB 5.40's ASM output is a little different so that this method doesn't work anymore. I had a look on an older ASM code (from a similar discussion in 2009) and realized that it's because newer PB compilers don't use "procedure macros" (in the form of "MPXXX" calls). This mean that simply commenting them out won't work.

Does anyone know a working approach? Maybe this time the ASM level is too low and the kind of new --preprocess compiler option will do the trick?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Code optimization

Post by Tenaja »

c4s wrote:I tried to get Danilo's tool working again because I remembered that back then it produced greatly size-optimized results.

Unfortunately PB 5.40's ASM output is a little different so that this method doesn't work anymore. I had a look on an older ASM code (from a similar discussion in 2009) and realized that it's because newer PB compilers don't use "procedure macros" (in the form of "MPXXX" calls). This mean that simply commenting them out won't work.

Does anyone know a working approach? Maybe this time the ASM level is too low and the kind of new --preprocess compiler option will do the trick?
You will need to search for this:

Code: Select all

; Procedure ThisProcName()
_Procedure0:
and this:

Code: Select all

; EndProcedure
_EndProcedureZero1:
  XOR    eax,eax
_EndProcedure1:
  PUSH   dword [esp]
  CALL  _SYS_FreeString@4
  PUSH   eax
  PUSH   dword [esp+12]
  CALL  _SYS_FreeArray@4
  POP    eax
  ADD    esp,48
  POP    esi
  POP    edi
  POP    ebx
  POP    ebp
  RET
and comment-out or delete everything inclusively between them.

Likely a RegEx will do the job nicely, since proc arguments (parameters) and return values will change the asm and comments.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Code optimization

Post by c4s »

Thanks for the tip. Seems to be much more complicated (and error-prone) than just commenting out one line like it was possible previously.

I guess good procedure start/end markers for the ASM code (without comments) are:

Code: Select all

start$ = "_Procedure[NUMBERS]:"
code$ = "[ANYTHING]"
end$ = "_EndProcedureZero[NUMBERS]:[NEWLINE][ANYTHING]RET"
Where all [STUFF] should be replaced with the appropriate RegEx placeholders. Then if the procedure is redundant everything from start$ to code$ to end$ would have to be removed.

Later I'll check out the PREPROCESS output to see if that's easier to process. I simply don't want to put too much time into some hack that could again stop working with the next release of PB (and is pretty much something the PB team should implement anyway!).
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Code optimization

Post by User_Russian »

Please add code optimization and removal of unused procedures. When the program includes many modules, with thousands of procedures, the size of the executable file exceeds 1 MB, but not all procedures are actually used and deleting them will reduce the file size.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Code optimization

Post by Fluid Byte »

Who cares? Disk space today is measured in Terrabytes and Internet is very fast. I mean I commend you for trying to be efficient but what are you doing where 1 MB matters?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Code optimization

Post by luis »

Fluid Byte wrote:Who cares?
User_Russian.
Probably other people in this thread.
Myself.
Fluid Byte wrote: Disk space today is measured in Terrabytes and Internet is very fast. I mean I commend you for trying to be efficient but what are you doing where 1 MB matters?
A tech demo which must be under 64K to be admitted to a competition ?

BTW: the unicode only builds already went against that making things more difficult. But that's history now.
Probably it's not easy to find more compelling reasons to desire small executables from PB, but who knows. Maybe for a small linux embedded system ?
I'm also curious about the possible reasons other people here may have for wanting this.
A good reason would be to make ROM-mable code, but this is true only for other languages and since PB does not support it, it's not a valid excuse. :)
The only other reason I can think of is that if something it's not needed, it's more appealing to me to not include it even if it's easier to just say "who cares".
It's the joy you find in a job well done, versus a more sloppy one which is good enough (but you know what's underneath it).
I'm sure many others can relate to this.
You don't have any control here. It's this way and everything spectacularly space-optimized you do ends up on top of some stuff you are forced to bring with you.
I like when a problem is just mine and I can work a little harder if I want to overcome it, not when I inherit it.
So I wholeheartedly support the request just in principle, but nurturing very low hopes. :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Code optimization

Post by Fluid Byte »

luis wrote:User_Russian.
Probably other people in this thread.
Myself.
So two people without a reason which equals no one. As I said, no one cares.
luis wrote:A tech demo which must be under 64K to be admitted to a competition?
A tech demo? What the fuck is this? 1985?
luis wrote:Probably it's not easy to find more compelling reasons to desire small executables from PB, but who knows.
It's not easy to find because there are no reasons. Again, huge disk space, fast Internet.
luis wrote:A good reason would be to make ROM-mable code, but this is true only for other languages and since PB does not support it, it's not a valid excuse. :)
Why are you even listing this if it's not supported by PB? You seem so desperate to prove me wrong that you are pulling stuff out of your ass.
luis wrote:The only other reason I can think of is that if something it's not needed, it's more appealing to me to not include it even if it's easier to just say "who cares".
What in gods name are you babbling about you illiterate twat.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Code optimization

Post by luis »

Fluid Byte wrote: So two people without a reason which equals no one. As I said, no one cares.
As I said, I care. The thread exist and people are talking about ways to implement that in some way.
So they care too.
You can repeat "no one cares" until you run out of saliva though, the real world would still be there after you finished.
Fluid Byte wrote: A tech demo? What the fuck is this? 1985?
You are not up to speed to what's happening in the programming world.
These things are still going strong with a lot of capable people working on them, just using different technologies compared to 30 years ago.

https://en.wikipedia.org/wiki/64K_intro
http://awards.scene.org/nominees.php?cat=9

... but you are going to simply ignore this anyway.
Fluid Byte wrote:It's not easy to find because there are no reasons. Again, huge disk space, fast Internet.
I mentioned at least one, I expect to be others I didn't think about.
Fluid Byte wrote: Why are you even listing this if it's not supported by PB?
I decide it was worth mentioning, for reasons I don't feel compelled to explain to you.
Fluid Byte wrote: You seem so desperate to prove me wrong that you are pulling stuff out of your ass.
No, I'm not. Have someone read my post to you.
You'll see I'm wondering about possible reasons for wanting this, beyond the couple I mentioned.

... but you are going to ignore this too.

BTW: why do you have the urge to use "ass", "fuck", etc. in your sentences ?
Fluid Byte wrote: What in gods name are you babbling about you illiterate twat.
Which gods ?
At this point I would like the opportunity to bash your face in I must say.
Probably you would understand that at least.
No idea why you have become such a prick, I remember you differently.
You can do the same by reading your old posts and remember a better self.
It was latent I have to admit it, but now it exploded.

Maybe it has to do with Emma Watson not giving you the time of the day.

You seem unable to learn from recent events, at any rate.

EDIT: now, if you really have to reply back, please count to 100 while eating a bar of chocolate.

EDIT: you didn't count... too bad.
Last edited by luis on Thu Jul 13, 2017 1:31 pm, edited 4 times in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Code optimization

Post by skywalk »

Suddenly, Fluid Byte has become our forum troll.
I write many big apps and lots of support tools from shared libraries. Why in the world would I choose to carry 1MB exe's of dead code and suffer the delays in backup, antivirus scans, compressing, loading, etc? I have already said in another thread I was able to shave 30 to 50% exe size by manual omission of dead library procedures. So, count me in for an automated method.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Code optimization

Post by Little John »

skywalk wrote:Suddenly, Fluid Byte has become our forum troll.
Yes, indeed.

He has lost contact to reality and confuses the contents of his narrow mind with the real world. And of course, he is the center of his own "world". So instead of saying "I do not care.", he says
Fluid Byte wrote:no one cares.
(which is wrong, obviously).
This is because he is unable even to imagine that in the real world there are people who are not as narrow minded as he is. And because in his "world" he is not only the judge but also the police, everything and everyone that irritates him because there is no place for it in his tiny brain, will be attacked. What a sorry figure!

The best thing we can do (as always in cases like this): Do not feed the troll!
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Code optimization

Post by normeus »

I would also want to see smaller execs. I enjoy having a tiny 1meg PureBasic program doing what my massive C# programs do.
So count me in with the ones who care but, it does not change my life if code is not optimized.


:D Have a nice day :D
Surf is UP!!! ( July 12 2017 - 10:50AM Pacific time )
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
the.weavster
Addict
Addict
Posts: 1536
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Code optimization

Post by the.weavster »

luis wrote:You can repeat "no one cares" until you run out of saliva though, the real world would still be there after you finished.
:lol:
Locked