Jump to content
Wil van Antwerpen

Binary size, how-to make it smaller?

Recommended Posts

Back in Delphi 5, Delphi 7 etc... I did built a few DLL's and I was amazed by how small they ended up.
Creating a nice control under 100kB was normal.

 

Nowadays, with Delphi 10.4 building any control under 2MB looks like it is a challenge.

Has anything changed here?
Is this because I am using VCL and is VCL pulling in 2MB for that? (If so I have to see if I can't eliminate the usage of VCL)

I don't see this happening if I built a component in C++ with Visual Studio.

 

Has anyone any tips on how-to reduce the size of the resulting binary DLL or ActiveX?

Looks to me that I am just missing something basic here.

 

Btw, just saying, I'm not interested in packers. I prefer to just cut the "bloat", not compress it.

 

Thanks for any ideas.

Share this post


Link to post
Quote

Turn off RTTI if you can.

That can save 400K 

 

For big savings remove Classes 1.5 M and SysUtils .2 M from the uses clause for a smaller lib.  

  • Like 1

Share this post


Link to post
1 hour ago, Pat Foley said:

That can save 400K 

 

For big savings remove Classes 1.5 M and SysUtils .2 M from the uses clause for a smaller lib.  

I can't really imagine writing any code > WriteLn('hello world'); without using SysUtils and Classes. And I doubt that many will go this way.

 

Eliminating the VCL is a different matter. That's possible if the lib doesn't need a UI. But I would probably not even try that nowadays, not to just save a few megabytes.

  • Like 1

Share this post


Link to post

Thanks.
I was afraid that it would be hard. I tried to get rid of some standard packages.. but didn't see a byte difference.
None of the suggested packages are easy to remove as that would trigger a lot of extra -hard to maintain- work.
Looks like I will have to learn to live with it for now.

Somehow I had expected that the compiler was smarter and not include parts of the packages I'm not using, but I can see how that's a bit of a pipe dream.


At least I now know what parts to pay attention to.

--
Wil

Share this post


Link to post

If you really need to do this then you probably need to switch to a different tool, or use an old version of Delphi.  Modern Delphi just produces huge executables.  Or you could just decide not to worry about a few MBs.

  • Like 1

Share this post


Link to post

Thanks David, yes that seems to be the case.
While other tools can be useful, quite often it is faster to build something in Delphi. Going back to using a very old version is most of the times not the best solution.
Much appreciate all the replies.

Share this post


Link to post
16 hours ago, dummzeuch said:

I can't really imagine writing any code > WriteLn('hello world'); without using SysUtils and Classes. 

I agree.  They are the bread and butter of most apps. 
Still, it makes you wonder why the linker cannot eliminate more of the unused stuff.

Share this post


Link to post
7 minutes ago, Lars Fosdal said:

I agree.  They are the bread and butter of most apps. 
Still, it makes you wonder why the linker cannot eliminate more of the unused stuff.

Because enhanced RTTI is now turned on for RTL/VCL with rather poor default (and of course, FMX, but FMX was never without it) and that RTTI prevents removing unused things with linker. So any class you touch or is touched by other code, will be almost completely included in executable. Unused classes are not included.

 

Default visibility for methods is public and published, for fields private, protected, public, published and for properties public, published. 

 

There is also WEAKLINKRTTI directive, that enables linker to eliminate unused symbols with RTTI, but it is not turned on in core Delphi frameworks. http://docwiki.embarcadero.com/RADStudio/Sydney/en/WEAKLINKRTTI_directive_(Delphi)

  • Like 2

Share this post


Link to post

I have x64 DLL that uses SysUtils and DateUtils weighting 590 Kb compiled with XE2. Unfortunately, RTL is not designed having little binary size in mind (bloated Classes unit that you have to include just when you need TStream).

Share this post


Link to post
1 hour ago, Fr0sT.Brutal said:

I have x64 DLL that uses SysUtils and DateUtils weighting 590 Kb compiled with XE2.

library rtltest;
//{$weaklinkrtti on}
uses
 system.classes;
begin
 TMemoryStream.Create.Free;
end.

Delphi XE2, x86_64, Release:

default - 547840 bytes

+ weaklinkrtti on - 505344 bytes

+ custom rtl build with minimal rtti - 256000 bytes

  • Like 1

Share this post


Link to post
31 minutes ago, Kazantsev Alexey said:

Delphi XE2, x86_64, Release:

default - 547840 bytes

+ weaklinkrtti on - 505344 bytes

+ custom rtl build with minimal rtti - 256000 bytes

Nice note. How did you produce custom RTL build? Just putting Classes and all its used units near the project or recompiled all units somehow?

Share this post


Link to post
1 hour ago, Fr0sT.Brutal said:

How did you produce custom RTL build?

For XE2 you need explicitly rebuild the three files (for latest versons need some more rtl files be rebuilded):
system.pas (with key -Y)
system.sysutils.pas (with key -V-)
system.variants.pas (with key -V-)
(search path should include rtl sources)

 

When project building seach path should include your own custom rtl dcus, and rtl sources after it.

Edited by Kazantsev Alexey
  • Like 1

Share this post


Link to post
4 hours ago, Kazantsev Alexey said:

library rtltest;
//{$weaklinkrtti on}
uses
 system.classes;
begin
 TMemoryStream.Create.Free;
end.

Delphi XE2, x86_64, Release:

default - 547840 bytes

+ weaklinkrtti on - 505344 bytes

+ custom rtl build with minimal rtti - 256000 bytes

The same example on 10.4.2:
1770kb, 1279kb, 456kb

Share this post


Link to post
On 6/22/2021 at 4:23 PM, Wil van Antwerpen said:

Somehow I had expected that the compiler was smarter and not include parts of the packages I'm not using

That would the BIGGEST INNOVATION in Delphi ever, giving Embarcadero a 500% boost to Delphi sales.

Share this post


Link to post
14 minutes ago, PeterPanettone said:

That would the BIGGEST INNOVATION in Delphi ever, giving Embarcadero a 500% boost to Delphi sales.

LOL, it should, but sadly nobody really cares about the size of a binary these days.
Having to roll (and maintain) your own version of a standard unit sadly isn't time best spent either.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×