Recent

Author Topic: How to reduce EXE program size?  (Read 26975 times)

FlierMate

  • Guest
How to reduce EXE program size?
« on: April 11, 2021, 02:16:54 pm »
My simple "Hello World" program is 49,815 bytes. Hmm..., can we strip it of unused libraries / data?

The attached picture shows the code section of that size, in which I think the rest of EXE are unrelated to displaying "Hello World" text string on console screen?

I thought I want to attempt writing a compress tool for FPC but (1) the output EXE is far more complicated that I thought, (2) it is best if it is done by the developer of the FPC itself (i.e. not outsider like me).  :D

Thank you for your attention.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to reduce EXE program size?
« Reply #1 on: April 11, 2021, 02:35:37 pm »
Hi!

Assuming you got only 4GB Ram then your exe occupies 0.001 % of it.

Winni

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to reduce EXE program size?
« Reply #2 on: April 11, 2021, 02:43:59 pm »
And Windows EXEs are not even always fully loaded into memory

Anyway, if you want to obsess over size, you'll need to do it yourself. In the past research by people focussed on size has occasionally been merged back as improvement.

But most suggestions are microoptimizations that limit features. Fixes should be real saving of size (e.g. linker improvements), not just cutting the featureset to minimal. If you want that, you'll need to manage it yourself (e.g. maintain your own cut down RTL), since one RTL will never serve all users size requirements optimally

So the short answer is: maintain a custom rtl is the easiest for special size requirements. Of course work on improving the smart linking is always welcome.
« Last Edit: April 11, 2021, 03:30:02 pm by marcov »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1112
  • Professional amateur ;-P
Re: How to reduce EXE program size?
« Reply #3 on: April 11, 2021, 02:52:00 pm »
Hey FlierMate,

Every once in a while, and lately more often than most(isn't this the 2nd post this last month), someone comes along with this notion that they need to produce a rather small binary.

In the current age where hardware minimums are in the 4GB for RAM and half a TERA for HDDs, I really don't understand this chasing of the windmills.

The only place where this should be brought up is in embedded environments, where you have very limited RAM/ROM/EPROM.

But even there, with the advent of the Arduino(AVR SoC) and the Raspberry Pie(ARM SoC) those limitations are getting smaller every iteration.

So, FlierMate, could you please explain to me this urgent quest to make the binary small? Please?

I profoundly thank you, in advance, for any light shed into this predicament.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Aidex

  • Jr. Member
  • **
  • Posts: 82
Re: How to reduce EXE program size?
« Reply #4 on: April 11, 2021, 03:31:24 pm »
Hi!
Have you tried the following?

Project Settings > Compiler Settings > Debugging > tick "Use external debug symbols file" (-Xg)
« Last Edit: April 11, 2021, 03:52:56 pm by Aidex »

FlierMate

  • Guest
Re: How to reduce EXE program size?
« Reply #5 on: April 11, 2021, 03:38:37 pm »
Hi!

Assuming you got only 4GB Ram then your exe occupies 0.001 % of it.

Winni

That is a different perspective to look at it.  Certainly it makes me less worried about the size of EXE.  :P

So the short answer is: maintain a custom rtl is the easiest for special size requirements. Of course work on improving the smart linking is always welcome.

FPC is a large project, it would take tremendous effort and time to study the code, let alone working on improving the linker. What I initially thought is a quick fix, but as I said, it is more complicated than that.
And thanks for the answer.


So, FlierMate, could you please explain to me this urgent quest to make the binary small? Please?

I profoundly thank you, in advance, for any light shed into this predicament.

I was used to participate in so called "size-optimizing coding contest", so I, for one, is quite obsessed about file size of binary executable.  People are always go for "smaller and faster" or "bigger and faster"(size, depending on the circumstance), or you have heard enough of "shortest path to a solution". All in all, clean code and small binary footprint are often correlated to efficiency.  Hopefully I have answered your question.   :-[

FlierMate

  • Guest
Re: How to reduce EXE program size?
« Reply #6 on: April 11, 2021, 03:42:36 pm »
Hi!
Have you tried the following?

Project Settings > Compiler Settings > Debugging > tick "Use external debug symbols file"

Hi there.  I think you are talking about Lazarus IDE? Hmm..

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to reduce EXE program size?
« Reply #7 on: April 11, 2021, 03:48:27 pm »
FPC is a large project, it would take tremendous effort and time to study the code, let alone working on improving the linker. What I initially thought is a quick fix, but as I said, it is more complicated than that.

That's the quick fix. Commenting some initializations in various standard units and maintain that as a custom RTL. Think about things like localisation, timezone support, thread support etc. Slightly harder is returning the string support back to the pre unicode level.

Note that embedded targets of FPC already do some of this. If you need embedded windows, you need to enable/backport such tricks to win32

Most low hanging has mostly been done.

Aidex

  • Jr. Member
  • **
  • Posts: 82
Re: How to reduce EXE program size?
« Reply #8 on: April 11, 2021, 03:52:09 pm »
FlierMate, the compiler option I meant is -Xg

balazsszekely

  • Guest
Re: How to reduce EXE program size?
« Reply #9 on: April 11, 2021, 03:57:55 pm »
Quick test: FPC 3.2.0 vs. CodeBlocks 20.3, 32 bit "Hello world" application, both in release mode:
1. FPC 3.2.0:  33,280 bytes
2. CodeBlocks: 18,944 bytes

Where is the big difference?  Yes, the binary generated by FPC is slightly larger, but I don't think this is problem, not for me at least. :)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1112
  • Professional amateur ;-P
Re: How to reduce EXE program size?
« Reply #10 on: April 11, 2021, 04:04:27 pm »
Hey FlierMate,

I was used to participate in so called "size-optimizing coding contest", so I, for one, is quite obsessed about file size of binary executable.  People are always go for "smaller and faster" or "bigger and faster"(size, depending on the circumstance), or you have heard enough of "shortest path to a solution". All in all, clean code and small binary footprint are often correlated to efficiency.  Hopefully I have answered your question.   :-[

Yes you have, and frankly I have to admit, and apologise, that I completely forgot about such type of contest that are just for the fun of it.

And on that subject, my counter answer is quite simple: In every case of trying to get it to the bone, there's always an effort in cutting away all the things that make it useful for the masses.

Or in other words, and already mentioned above, you would have to go and shave down a lot of the stuff in the RTL, FCL and LCL and probably add a lot of other very customized or hand crafted solutions for what you just shaved down due to everything being so intertwined.

So, to summarize, it's a rather big effort and it only pays out if you have deep knowledge of the ELF format and/or whatever it's called in Windows the exe format to then go into the RTL, FCL and/or LCL and shave the fat with confidence.

Not trivial and not for a one week-end project, I would say.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: How to reduce EXE program size?
« Reply #11 on: April 11, 2021, 04:06:10 pm »
Quote
Quick test: FPC 3.2.0 vs. CodeBlocks 20.3, 32 bit "Hello world" application, both in release mode:
1. FPC 3.2.0:  33,280 bytes
2. CodeBlocks: 18,944 bytes

Also (usually) CodeBlocks binary is detected as a virus, with all the C stuff we had problems at university and in our homes trying to install something that was free, small and don't get confused with a virus at every compilation. I don't know if it has changed over the years, just commenting my own experience.
« Last Edit: April 11, 2021, 04:10:37 pm by lainz »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to reduce EXE program size?
« Reply #12 on: April 11, 2021, 04:09:23 pm »
Is the Code::Blocks binary standalone ?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: How to reduce EXE program size?
« Reply #13 on: April 11, 2021, 04:11:43 pm »
Is the Code::Blocks binary standalone ?

Yes it's a C program, an exe that just requires the native dll from Windows. Nothing about Net or modern libraries.

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to reduce EXE program size?
« Reply #14 on: April 11, 2021, 04:15:21 pm »
Hello.

For info, this, compiled with fpc  -XX -Xs -CX parameters:

Code: Pascal  [Select][+][-]
  1. program myEmptyProg;
  2.  
  3. begin
  4. end.

gives:

with fpc 3.2.0 Windows 32 bit: 32.5 Kb
with fpc 3.2.0 Windows 64 bit: 43.0 Kb

with fpc 3.2.0 Linux 32 bit: 29.9 Kb
with fpc 3.2.0 Linux 64 bit: 34.0 Kb

 :-X
« Last Edit: April 11, 2021, 04:16:57 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018