We grew curious as to how efficient Delphi is at minimizing the size of executables and DLLs, especially when it comes to not linking in code that is not actually used. We decided we would put it through its paces by simply creating an empty DLL then including some units with a "Uses" statement but never actually utilize their code. We did all this with Delphi 4 and optimization turned on.
Our initial DLL with one dummy function was built at a very lean 4,096 bytes. You can't get any smaller than this. We then had the project source include a unit that in turn included some other Delphi units. Apparently even though neither unit uses the code within its logic, the "Uses" statement apparently causes the entire unit(s) to be linked in regardless. The byte increases are listed in the table below.
We then had our DLL project file include the same units with a "Uses" statement placed within the project file instead of referencing them through a secondary unit. In this case Delphi is far more effective at recognizing that the unit's code was not actually utilized. By the time we had include both the SysUtils, Classes, Forms, and ShellApi units the DLL size had jumped up to only 7,680 bytes.
So the moral of the story is that if you want to build a really small DLL or executable in Delphi you need to include all its code in your project source file so the linker will use its brain. Note that all the C++ compilers on the market behave similarly.
Unit | Bytes | Comments |
---|---|---|
SysUtils | 24,576 | |
Classes | 17,920 | |
Forms | 220,672 | |
Registry | 0 | Uses SysUtils, Classes |
Controls | 0 | Uses Forms |
ShellApi | 512 | A resource? |
Pure Win API units such as Windows, Messages, MMSystem, etc. | 0 |
Send any comments you have to [email protected].