Lazarus

Free Pascal => Windows => Topic started by: FlierMate on April 11, 2021, 02:16:54 pm

Title: How to reduce EXE program size?
Post by: FlierMate 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.
Title: Re: How to reduce EXE program size?
Post by: winni on April 11, 2021, 02:35:37 pm
Hi!

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

Winni
Title: Re: How to reduce EXE program size?
Post by: marcov 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.
Title: Re: How to reduce EXE program size?
Post by: Gustavo 'Gus' Carreno 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
Title: Re: How to reduce EXE program size?
Post by: Aidex 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)
Title: Re: How to reduce EXE program size?
Post by: FlierMate 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.   :-[
Title: Re: How to reduce EXE program size?
Post by: FlierMate 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..
Title: Re: How to reduce EXE program size?
Post by: marcov 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.
Title: Re: How to reduce EXE program size?
Post by: Aidex on April 11, 2021, 03:52:09 pm
FlierMate, the compiler option I meant is -Xg
Title: Re: How to reduce EXE program size?
Post by: GetMem 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. :)
Title: Re: How to reduce EXE program size?
Post by: Gustavo 'Gus' Carreno 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
Title: Re: How to reduce EXE program size?
Post by: lainz 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.
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 04:09:23 pm
Is the Code::Blocks binary standalone ?
Title: Re: How to reduce EXE program size?
Post by: lainz 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.
Title: Re: How to reduce EXE program size?
Post by: Fred vS 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
Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 04:21:42 pm
Yes, FPC is quite small. I tested with Rust x64 and I get a binary of 132 kb in release mode.

This is the Rust program I've used.
Code: Text  [Select][+][-]
  1. fn main() {
  2.    
  3. }

Edit: I know it has nothing to do with file size, but I also tried Kotlin native, and at least with their IDE it never ends compiling  :D FPC is quite fast as well compiling.
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 04:32:50 pm
Is the Code::Blocks binary standalone ?

Yes it's a C program, an exe that just requires the native dll from Windows.

So msvcrt is linked in statically?
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 04:36:58 pm
Yes, FPC is quite small.

Hum, not so small as GCC ad clang

Code: C  [Select][+][-]
  1. int main() {
  2.    
  3. }

with gcc-8 64 bit linux: 16.1 Kb.
with clang-7 64 bit linux: 14.0 Kb.

But yes, fpc does it small.

Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 04:49:33 pm
So msvcrt is linked in statically?

Good question =)

But I'm not sure, what I know is that it can use different compilers.

Quote
Multiple compiler support:

GCC (MingW / GNU GCC)
MSVC++
clang
Digital Mars
Borland C++ 5.5
Open Watcom
…and more

It must depend on the compiler you choose?

How I can detect that if I install it?
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 04:59:51 pm
Code: C  [Select][+][-]
  1. int main()
  2. {
  3. }
Is actually quite nothing. Include a printf() statement to add some beef.

MSVC by default compiles against msvcrt.dll. You can link statically with an option.

Can't compare binaries between Win/Linux, the executable format is quite different.

Regards,
Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 05:01:05 pm
Hey anyways, if I want to install Cyberpunk 2077 it uses 70 GB of disk space, and I can put it together with my less than 100kb dummy program  :D
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 05:02:40 pm
So msvcrt is linked in statically?

Good question =)

But I'm not sure, what I know is that it can use different compilers.

But you used only one to create that smaller file.

Quote
How I can detect that if I install it?

If you mean how you can detect how it works, check the DLLs that loads. Even if msvcrt comes with Windows, comparing a compiler with a runtime that is packaged with the OS to one that is not is only confusing the picture.
Title: Re: How to reduce EXE program size?
Post by: FlierMate on April 11, 2021, 05:04:05 pm
FlierMate, the compiler option I meant is -Xg

It made the EXE larger: 33,811 bytes. If I just type "FPC firstapp.pas" without the "-Xg" then it generates 33,280 bytes.
49KB EXE is a result compiling it in the IDE itself.

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.

Ah, I see. I'll do it if I have special needs. Thanks.
BTW, years ago, I remember the EXE was way smaller. Looks like the RTL has grown over the years.

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

True.

And I agree with you completely, there is always pros and cons. Usually we could not please everyone.

Hum, not so small as GCC

Code: C  [Select][+][-]
  1. int main() {
  2.    
  3. }

with gcc 64 bit linux: 12.5 Kb.

If one coded using Assembler, a "Hello World" EXE as small as 1,024 bytes is possible. But of course it would be a lot more diificult to code in that language.
Title: Re: How to reduce EXE program size?
Post by: Gustavo 'Gus' Carreno on April 11, 2021, 05:16:48 pm
Hey FlierMate,

And I agree with you completely, there is always pros and cons. Usually we could not please everyone.

Quite right, you can never appease Roman and Trojan(Old Portuguese saying)

If one coded using Assembler, a "Hello World" EXE as small as 1,024 bytes is possible. But of course it would be a lot more diificult to code in that language.

I particularly avoided going here, because it's the low hanging fruit.

Nonetheless, from the old day of MS-DOS, there was a guaranteed way that real programmers did it the smallest:
Code: DOS  [Select][+][-]
  1. COPY CON PROGRAM.EXE

Mic drop... :D :P

And if they needed to distribute:
Code: DOS  [Select][+][-]
  1. COPY CON PROGRAM.ZIP

Cheers,
Gus
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 05:17:14 pm
If one coded using Assembler, a "Hello World" EXE as small as 1,024 bytes is possible. But of course it would be a lot more diificult to code in that language.

Of course with Assembler, but imho, Pascal is more comparable with C.

And about size, re-imho fpc size of executable are fairly reasonable.
It was not the case for libraries, -XX parameters was not enabled for them, but since fpc 3.0.0, it is fixed ( @marcov  ;) )

Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 05:18:28 pm
But you used only one to create that smaller file.
....
If you mean how you can detect how it works, check the DLLs that loads. Even if msvcrt comes with Windows, comparing a compiler with a runtime that is packaged with the OS to one that is not is only confusing the picture.

Hi, that's ok. Is like the C# executables are even smaller, but they require .NET installed. I understand.

But I did not compared the sizes, it was GetMem:

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. :)

I did just a Rust comparison on my post, not a C one:

Yes, FPC is quite small. I tested with Rust x64 and I get a binary of 132 kb in release mode.

This is the Rust program I've used.
Code: Text  [Select][+][-]
  1. fn main() {
  2.    
  3. }

Edit: I know it has nothing to do with file size, but I also tried Kotlin native, and at least with their IDE it never ends compiling  :D FPC is quite fast as well compiling.
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 05:38:08 pm
@lainz
Rust? Kotlin? Really?

IMHO, nothing can beat C in code compactness. It is made for that! Furthermore, in Linux GCC apps are linked dynamically against all libraries. It is not like MSVC just against a single msvcrt.dll. And, AFAIK, the FPC has the monolithic approach, i.e. to link everything statically.

You're comparing oranges with apples.

So, what was the intention of the guy who started the topic? I'm lost.

Regards,
Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 05:45:07 pm
@lainz
Rust? Kotlin? Really?

IMHO, nothing can beat C in code compactness. It is made for that! Furthermore, in Linux GCC apps are linked dynamically against all libraries. It is not like MSVC just against a single msvcrt.dll. And, AFAIK, the FPC has the monolithic approach, i.e. to link everything statically.

You're comparing oranges with apples.

So, what was the intention of the guy who started the topic? I'm lost.

Regards,

Hey why not?

Comparing a programming language resulting .exe with it's final size. That's it.
Title: Re: How to reduce EXE program size?
Post by: FlierMate on April 11, 2021, 05:46:15 pm
So, what was the intention of the guy who started the topic? I'm lost.

It all started when I tried to use "Optimization- Generate smaller code"(see screenshot) in compiler option as well as Smart-linking. But both do not seem to work. So this is what puzzled me.
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 05:49:42 pm
@lainz
Rust? Kotlin? Really?

IMHO, nothing can beat C in code compactness. It is made for that! Furthermore, in Linux GCC apps are linked dynamically against all libraries. It is not like MSVC just against a single msvcrt.dll. And, AFAIK, the FPC has the monolithic approach, i.e. to link everything statically.

You're comparing oranges with apples.

So, what was the intention of the guy who started the topic? I'm lost.

Regards,

Hello y.ivanov.

I think that it is the same for everybody starting with fpc (included me, of course).

The size of the executable that is bigger than with their comparable compilers concurrents.

This question come back each year, like a wave, but the good news is that in practice, each new fpc release produces smaller exe.

And yes, fpc produces bigger exe than his concurrents, but fpc does not have any competitor for cross-compilations, for his huge numbers of targets, his human-pascal-language, his hot forum with mad people and much more.

Amen.

Fre;D


Title: Re: How to reduce EXE program size?
Post by: FlierMate on April 11, 2021, 06:04:13 pm
Below are some more findings. All are the size of console apps which have dependency.

C# .NET - 4,608 bytes EXE.
Generate .NET Framework dependent executable.
Code: [Select]
using System;

namespace N
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.Write("Hello World");
       }
   }
}

Visual C++ - 10,752 bytes EXE.
Generate CRT-dependent executable?

Code: [Select]
#include <iostream>

int main()
{
   std::cout << "Hello World\n";
}
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 06:13:50 pm
@ FlierMate

Yes, we know, fpc generates bigger exe than his comparable Visual C++ friend.

And, like marcov explained, it is due to the initialization of some units used by the RTL.

If you want to jump into it and make a "light RTL", I am very interested by your work.

Fre;D
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 06:17:44 pm
Hello y.ivanov.

I think that it is the same for everybody starting with fpc (included me, of course).

The size of the executable that is bigger than with their comparable compilers concurrents.

This question come back each year, like a wave, but the good news is that in practice, each new fpc release produces smaller exe.

And yes, fpc produces bigger exe than his concurrents, but fpc does not have any competitor for cross-compilations, for his huge numbers of targets, his human-pascal-language, his hot forum with mad people and much more.

Amen.

Fre;D

Thanx, Fred!
For reminding me that
Quote
... his hot forum with mad people and much more.
is a virtue.

I know well the reasons behind the FPC exe bigger sizes. I'm OK with that. The only thing I miss is the lightning compilation speed with Delphi.  :)

Regards,
 
Title: Re: How to reduce EXE program size?
Post by: GetMem on April 11, 2021, 06:21:21 pm
@marcov

Yes, it's a standalone application and yes msvcrt.dll is loaded, still the fpc binary is only 1.7x bigger.
Title: Re: How to reduce EXE program size?
Post by: FlierMate on April 11, 2021, 06:23:07 pm
If you want to jump into it and make a "light RTL", I am very interested by your work.

Yes, and thanks for the encouraging words. I started this topic when I was trying to compress the EXE compiled by FPC. I am wondering is there anyone who is working on a "light RTL" project?     
Title: Re: How to reduce EXE program size?
Post by: winni on April 11, 2021, 06:29:20 pm

Yes, and thanks for the encouraging words. I started this topic when I was trying to compress the EXE compiled by FPC. I am wondering is there anyone who is working on a "light RTL" project?   

As far as I know: No.

The reason is simple:
Today you can't buy a PC with "light RAM" anymore.

Winni
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 06:32:56 pm

Yes, and thanks for the encouraging words. I started this topic when I was trying to compress the EXE compiled by FPC. I am wondering is there anyone who is working on a "light RTL" project?   

As far as I know: No.

The reason is simple:
Today you can't buy a PC with "light RAM" anymore.

Winni

There are the Rpi and micro-friends that does some effort.
Title: Re: How to reduce EXE program size?
Post by: lainz on April 11, 2021, 06:36:32 pm

Yes, and thanks for the encouraging words. I started this topic when I was trying to compress the EXE compiled by FPC. I am wondering is there anyone who is working on a "light RTL" project?   

As far as I know: No.

The reason is simple:
Today you can't buy a PC with "light RAM" anymore.

Winni

Not PC but specialized hardware like printers with Linux OS. Limited hardware for specific use cases.

But some few KB's will do nothing. I mean the hardware I mentioned can run Electron without problems as well.
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 06:45:43 pm
But some few KB's will do nothing. I mean the hardware I mentioned can run Electron without problems as well.

Hum, for - 32 Kb (and more) for a simple console prog, I am interested by the "Light RTL" fork of FlierMate.
Title: Re: How to reduce EXE program size?
Post by: Gustavo 'Gus' Carreno on April 11, 2021, 06:46:27 pm
Hey all,

[snip] his hot forum with mad people and much more.

Mad person in the house. Present and accounted for ;)

Cheers,
Gus
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 07:21:20 pm
But some few KB's will do nothing. I mean the hardware I mentioned can run Electron without problems as well.

Hum, for - 32 Kb (and more) for a simple console prog, 

32KB for a *windows* console prog.   If you want to compare to embedded solutions, your testing must also be for FPC embedded targets like arduino or some Arm Cortext M*.

Unless you are still nursing one of those notorious Windows NT4 based printers :-)  (and FPC doesn't support <XP anymore)
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 07:31:22 pm
But some few KB's will do nothing. I mean the hardware I mentioned can run Electron without problems as well.

Hum, for - 32 Kb (and more) for a simple console prog, 

For a *windows* console prog.   If you want to compare to embedded solutions, your testing must also be for FPC embedded targets like arduino or some Arm Cortext M*.

Ooops, sorry, the topic is about Windows and his tons of RAM needed, then of course 32 KB is nothing.

No, I was not thinking about Windows but embedded solutions that have few memory, mainly using Linux.
Title: Re: How to reduce EXE program size?
Post by: winni on April 11, 2021, 07:48:11 pm
Hi!

I recognized that some people don't have a feeling what a kilobyte is related to a gigabyte. For those a graphic is always helpfull.

In the attachment.

Winni
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 07:50:48 pm
Sorry to come back with the first post, but if there something to compare on Windows, what are the result in size with fpc vs Delphi?
Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 08:03:10 pm
Hi!

I recognized that some people don't have a feeling what a kilobyte is related to a gigabyte. For those a graphic is always helpfull.

In the attachment.

Winni

Here with arduino 32 Kb ram + 2 KB flash.
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 08:07:57 pm
Completely empty Win32 console program:

Delphi7: 14 848 bytes
FPC 3.1.1: 32 768 bytes

No debug info, no RT checking
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 08:21:35 pm
Completely empty Win32 console program:

Delphi7: 14 848 bytes

NOT UNICODE

Quote
FPC 3.1.1: 32 768 bytes

No debug info, no RT checking

UNICODE.

So to make a fair comparison at least use D2009. We all know that Turbo Pascal 3 also made smaller binaries, but that is not of much use now (unless you compare to the 16-bit dos port)
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 08:22:34 pm
No, I was not thinking about Windows but embedded solutions that have few memory, mainly using Linux.

The point is that embedded solutions don't use full target RTLs, so comparing with that is useless.
Title: Re: How to reduce EXE program size?
Post by: PascalDragon on April 11, 2021, 08:24:16 pm
Unless you are still nursing one of those notorious Windows NT4 based printers :-)  (and FPC doesn't support <XP anymore)

Though that's not a problem of the compiler, but more of the RTL. So when throwing out features to decrease the RTL size one might possible get back support for NT4 or 9x back as well :P
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 08:29:34 pm
@marcov
Sorry, that is what I have  :(
Title: Re: How to reduce EXE program size?
Post by: marcov on April 11, 2021, 08:36:54 pm
@marcov
Sorry, that is what I have  :(

Well, then you just have to believe me  that for small binaries, Delphi versions that support unicode are roughly twice as big.
Title: Re: How to reduce EXE program size?
Post by: FPK on April 11, 2021, 08:38:18 pm
Hi!

I recognized that some people don't have a feeling what a kilobyte is related to a gigabyte. For those a graphic is always helpfull.

In the attachment.

Winni

Here with arduino 32 Kb ram + 2 KB flash.

% fpc11avr -Wpattiny10 empty.pp
Free Pascal Compiler version 3.3.1 [2021/04/11] for avr
Copyright (c) 1993-2021 by Florian Klaempfl and others
(1002) Target OS: Embedded
(3104) Compiling empty.pp
(9009) Assembling program
(9015) Linking empty
(1008) 2 lines compiled, 0.0 sec, 94 bytes code, 0 bytes data
Title: Re: How to reduce EXE program size?
Post by: PascalDragon on April 11, 2021, 08:48:22 pm
@marcov
Sorry, that is what I have  :(

Well, then you just have to believe me  that for small binaries, Delphi versions that support unicode are roughly twice as big.

Empty console program compiled with Delphi 10.2 for Win32:

41 984 Byte

With WEAKLINKRTTI ON:

36 352 Byte

So marcov's estimation of roughly twice as big is correct. ;)
Title: Re: How to reduce EXE program size?
Post by: FPK on April 11, 2021, 08:50:23 pm
And "Hello world" on Amiga 244 Bytes: https://github.com/chainq/amiga-tiny-hello-p
Title: Re: How to reduce EXE program size?
Post by: y.ivanov on April 11, 2021, 09:07:15 pm
@marcov
Sorry, that is what I have  :(

Well, then you just have to believe me  that for small binaries, Delphi versions that support unicode are roughly twice as big.

I truly believe. I have not disputed it. Just tried to supply some figures.  ::)
Title: Re: How to reduce EXE program size?
Post by: ASBzone on April 11, 2021, 11:43: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.

A couple months back, I performed some informal testing across the systems on my network with varying executable sizes from a couple of applications, and came away with the following conclusions:

-- There is no substantial difference between the performance of one executable and another unoptimized version of the same, until there is at least a 250K size difference.
-- The newer the machine, the larger the difference in size needs to be, in order to make it noticeable -- even with logging
-- 10 year old systems running Windows are only a few dozen milliseconds slower in starting up than newer systems
-- The greatest contributing factor in startup performance is what AV or other end-point protection you are running  (I could literally shave 15-20 ms off the startup time based on which AV app was used)

It is such a non-factor that you will likely never reclaim the time you spent trying to optimize the performance via size.


Title: Re: How to reduce EXE program size?
Post by: Fred vS on April 11, 2021, 11:50:18 pm
Hi!

I recognized that some people don't have a feeling what a kilobyte is related to a gigabyte. For those a graphic is always helpfull.

In the attachment.

Winni

Here with arduino 32 Kb ram + 2 KB flash.

% fpc11avr -Wpattiny10 empty.pp
Free Pascal Compiler version 3.3.1 [2021/04/11] for avr
Copyright (c) 1993-2021 by Florian Klaempfl and others
(1002) Target OS: Embedded
(3104) Compiling empty.pp
(9009) Assembling program
(9015) Linking empty
(1008) 2 lines compiled, 0.0 sec, 94 bytes code, 0 bytes data

Thats the way, héhé, héhé, I like it.

 ;D
Title: Re: How to reduce EXE program size?
Post by: ASBzone on April 12, 2021, 05:14:27 am
My simple "Hello World" program is 49,815 bytes. Hmm..., can we strip it of unused libraries / data?

One other point I intended to mention, but forgot.

Does it really matter what size "Hello World" compiles to, if "Hello World" is not your final product?    Meaning, you might go through all the work to strip down the RTL (or something else) to provide the barest of minimum sized executable, when that isn't your actual use case, and then you find out that all the things you stripped out are very much needed by the actual code you want to produce.

If you're even going to go through this exercise at all, shouldn't you at least do it with meaningful code that you actually need to run, and want to end up with?

Because it makes little sense to optimize anything else.  ;) ;)
Title: Re: How to reduce EXE program size?
Post by: Dodfr on June 30, 2021, 01:06:05 am
Hi,
 
If your dev skills are pascal oriented and you still have a "good old" Delphi7 you may use KOL/MCK libs that permit to create very small apps including with visual components (limited to those from MCK libs that reproduce a lot of standard VCL component palette).

Simple helloworld inside a Form is 23KB, console version is smaller, adding visual components (MCK-KOL not VCL) do not increase size a lot.

A simple helloworld console mode is 8KB if I remember well.

Also to make EXE even smaller you can use ASM inline version of KOL libs instead of Delphi coded version.

Other trick with kol you can replace the standard sysutils with the one optimized with KOL.

So if you have some size coding contest and you are more comfortable with Pascal language than C/C++ then go for Delphi7+KOL/MCK, and whenever KOL/MCK development stopped I still use it time to time if I want small exe sized apps.

Appart from KOL/MCK if you want to reduce size of an EXE you can use strip.exe and EXE compressor like UPX (I think UPX do strip itself).

Regards.
Title: Re: How to reduce EXE program size?
Post by: avra on June 30, 2021, 02:41:37 pm
KOL is not Delphi only. It works with our favorite compiler as well...
https://wiki.freepascal.org/KOL
Title: Re: How to reduce EXE program size?
Post by: Dodfr on June 30, 2021, 04:27:01 pm
KOL is not Delphi only. It works with our favorite compiler as well...
https://wiki.freepascal.org/KOL

Excellent :-) I have an old Ressource Meter for years made with on D7+KOL and I wanted to make it x64


Title: Re: How to reduce EXE program size?
Post by: marcov on June 30, 2021, 04:48:48 pm
64-bit is always larger, so if you want to keep it small....
Title: Re: How to reduce EXE program size?
Post by: Dodfr on June 30, 2021, 06:17:41 pm
64-bit is always larger, so if you want to keep it small....

The thing is that I also check for some infos of running process it is to retrieve start command line that I can get from 32bits to 32bits processes but not from 32bits to 64bits (the ressource meter also show Top most CPU process Top RAM Top DiskActivity but for this I use Perfomance Counters) original size is 450KB and once compressed with UPX it is 150KB standalone fully portable exe zero install zero DLL.
Title: Re: How to reduce EXE program size?
Post by: Seenkao on June 30, 2021, 11:11:04 pm
Для уменьшения исполняемого файла надо перебрать файл system.pp для данной системы и исключить всё что вам не нужно или "минимизировать" код. Это было уже сделано Владимиром Кладовым, но на ассемблере для Delphi и для 32-х битной системы. Возможно тот код можно использовать для FPC/Lazarus (что-то уже переводилось для FPC/Lazarus).

Всю информацию и связаться с Владимиром Кладовым вы можете на сайте http://f0460945.xsph.ru/rindex.htm

google translate:
To reduce the executable file, you need to iterate over the system.pp file for the given system and exclude everything that you do not need or "minify" the code. This was already done by Vladimir Kladov, but in assembler for Delphi and for a 32-bit system. Maybe that code can be used for FPC/Lazarus (something has already been translated for FPC/Lazarus).

All information and contact with Vladimir Kladov you can visit the website http://f0460945.xsph.ru/rindex.htm
Title: Re: How to reduce EXE program size?
Post by: WackaMold on July 19, 2021, 10:12:01 pm
UPX Exe-packer from https://upx.github.io shrinks the empty program after compiling+linking from 44,032 bytes to 19,968 ... and a demo GUI About program from 2,853,376 bytes to 826,880 (68% shrinkage).
Title: Re: How to reduce EXE program size?
Post by: marcov on July 19, 2021, 10:13:49 pm
UPX Exe-packer from https://upx.github.io shrinks the empty program after compiling+linking from 44,032 bytes to 19,968 ... and a demo GUI About program from 2,853,376 bytes to 826,880 (68% shrinkage).

UPX is known, the FPC distribution even used to use it, but it was removed because it caused more problems (read: antivirus problems) than it solved.
Title: Re: How to reduce EXE program size?
Post by: Mr.Madguy on October 26, 2021, 10:59:08 am
Sorry for necroposting, but, I guess, my post will be useful. I don't see, that anybody has mentioned obvious way to decrease EXE size - runtime packages. It's one of two biggest EXE size offenders. Second is RTTI. Real reason, why C++ programs are so small - they use C++ Runtime Redist. I.e. all that msvcrt.dll stuff. Problem with Pascal - is that all that stuff is always built-in into every EXE and DLL, it produces. But! Funny thing, but Delphi allows this too. And this mechanism is called "Runtime packages". Problem is - Delphi Redist isn't as well-known, as C++ Redist. Not many developers know about it and provide it with their applications. They usually prefer to provide bpls themselves and this kills whole advantage of using runtime packages. Because if they're would be installed system-wide, they would be shared between all apps.

For example for my app (total size, including plug-ins):
Built-in 32bit: 9.75Mb
Built-in 64bit: 15.00Mb
Runtime 32bit: 4.77Mb (-51%) + 6.00Mb runtime
Runtime 64bit: 7.16Mb (-53%) + 8.62Mb runtime

But size change per plug-in is more dramatic:
Built-in 32bit: ~375Kb
Built-in 64bit: ~610Kb
Runtime 32bit: ~75Kb (-80%)
Runtime 64bit: ~130Kb (-79%)

So, more plug-ins - bigger advantage.
Title: Re: How to reduce EXE program size?
Post by: AlexTP on October 26, 2021, 11:33:36 am
Runtime packages are ok only on Windows, imagine what problems will have users on Unixes.
Title: Re: How to reduce EXE program size?
Post by: Mr.Madguy on October 26, 2021, 11:53:25 am
Runtime packages are ok only on Windows, imagine what problems will have users on Unixes.
I don't think so. Unixes also have something like libc.so. It's just RTL, that is separated to DLL. We can do the same with .so libraries too. For example compile RTL into something like libfpcrtl.so and distribute all this libraries as separate LazRTL package.
Title: Re: How to reduce EXE program size?
Post by: Handoko on October 26, 2021, 12:08:00 pm
I am no sure but I think Linuxes are not good in handling shared libraries. Many Linux programs (GIMP, Blender3D, OpenShot, etc) I downloaded couldn't start just because library version issue. I learned from mistakes, always install the program from Software Center or correct repository, or use the version that match my Linux version.
Title: Re: How to reduce EXE program size?
Post by: jamestien on October 26, 2021, 12:09:30 pm
You are talk about EXE, so I assume that you are on Windows machine.
If you want to get minimal EXE size of your program regardless of all the hassles, then you could just code your program using pure Windows API call like C++ style and handle all the window messages in your main program loop.
 
I used to do this before when I was using Delphi 3 & 7SE to get smallest & fastest program possible. but... man, for all the benefit of those component goodies today, I found it is just impossible to write a program without unnecessary package compiled into EXE. Just live with it...
Title: Re: How to reduce EXE program size?
Post by: Mr.Madguy on October 26, 2021, 05:16:11 pm
I am no sure but I think Linuxes are not good in handling shared libraries. Many Linux programs (GIMP, Blender3D, OpenShot, etc) I downloaded couldn't start just because library version issue. I learned from mistakes, always install the program from Software Center or correct repository, or use the version that match my Linux version.
This problem is fixed in Snap and Flatpack. Even if distributing packages with main application partially kills all benefits, unless your program has lots of plug-ins. But overall it's not problem with concept itself - it's problem with it's implementation in Linux.
Title: Re: How to reduce EXE program size?
Post by: marcov on October 26, 2021, 11:06:49 pm
I am no sure but I think Linuxes are not good in handling shared libraries. Many Linux programs (GIMP, Blender3D, OpenShot, etc) I downloaded couldn't start just because library version issue. I learned from mistakes, always install the program from Software Center or correct repository, or use the version that match my Linux version.
This problem is fixed in Snap and Flatpack.

"Fix" is a bit too big a word for those.  Their workaround is pretty much making copies of it in specific containers, is like saying Windows doesn't have DLL hell, because you can place the copies of all dlls in the same directory as the each app. 

Otherwise the whole file size (and memory!)  sharing bit goes out of the window.

I also think the Delphi package system's goal is primarily dynamic loading of code (iow plugins) and using it quite transparently and easy.

There are however also downsides:
Quote
Even if distributing packages with main application partially kills all benefits, unless your program has lots of plug-ins. But overall it's not problem with concept itself - it's problem with it's implementation in Linux.

Yes, plugins' as in functionality, (not size savings) are the primary goal of packages I think.

As for the Linux problem, it is bigger. Linux simply doesn't value binary interfaces at all beyond the current distribution version iteration. And for non core packages often not even that.

Title: Re: How to reduce EXE program size?
Post by: PascalDragon on October 27, 2021, 10:37:28 am
Runtime packages are ok only on Windows, imagine what problems will have users on Unixes.

This won't be a problem, cause like with all good *nix citizens the runtime package libraries will be versioned:


Using dynamic packages at compile time is in principle already possible for the Windows targets (excluding WinCE), the macOS targets as well as x86_64-linux (for all of them it's disabled inside the compiler and there's also the fact that we don't yet have a way to generate packages from fpmake).
Title: Re: How to reduce EXE program size?
Post by: marcov on October 27, 2021, 11:12:05 am
I revised my post a bit after reading Pascaldragon's, which reminded me that the exact package division in Delphi is not guaranteed within a future FPC/Lazarus package system.

Finer divided packages reduce overhead for programs that only use a few, but increase overhead for larger ones (e.g. lazarus LCL programs)
Title: Re: How to reduce EXE program size?
Post by: FPK on October 27, 2021, 08:12:22 pm
Yes, plugins' as in functionality, (not size savings) are the primary goal of packages I think.

I would expect that plugins work across different releases of a software but this is not going to happen if they are compiled by different versions of FPC.
TinyPortal © 2005-2018