Sign in Join
Wiki copytemporary

File size and smartlinking: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11This article is a work in progress about executable size and smartlinking on Lazarus. Feel free to contribute.
22
33## Introduction
44
55In Free Pascal, "smartlinking" is removing unused code and variables from the final executable. This is done during the linking stage, when the executable is written using the object files created by the compiler earlier.
66
77## Case study 1 in Windows
88
99This study was conducted on the 8th of February 2006 because a Lazarus fully compiled with Smartlinking was Released (version 0.9.12). It intends to establish the relationship between the varying results below with different Lazarus and Free Pascal versions as well as with Smartlinking and without.
1010
1111The Variables being studied are:
1212
1313- Executable size after strip
1414- Executable size after strip and UPX
1515- Linking time
1616
1717Compile time isn´t considered here because it´s too similar on all configurations and much less significant than the link time.
1818
1919Executable size without strip isn´t included. Notice that strip was used always from command line with the command:
2020
2121 strip --strip-all magnifier.exe
2222
2323The program being compiled is the Virtual Magnifying Glass. The source and binaries for this program are freely available for download on: <http://magnifier.sourceforge.net>
2424
2525About the linking time please note that the utilized computer is 3.2GHz Pentium 4 with Intel motherboard and dual core processor and 512MB of RAM.
2626
2727**Results**
2828
2929The utilized OS is Windows XP and the 0.9.13 versions are from the same date when 0.9.12 was release. The comparison took place using the following software configurations:
3030
3131- Lazarus 0.9.12 available [here](http://sourceforge.net/project/showfiles.php?group_id=89339). Free Pascal 2.0.2 that comes with the installer. LCL and RTL are smartlinked. Refered from now on as simply 0.9.12.
3232
3333- Lazarus 0.9.13 downloaded from Subversion from the same date. Free Pascal 2.0.2 installed separately. The LCL is not smartlinked. Refered from now on as simply 0.9.13 + 2.0.2.
3434
3535- Lazarus 0.9.13 snapshot. Free Pascal 2.1 that comes with the installer. The LCL is not smartlinked. Refered from now on as simply 0.9.13 + 2.1.
3636
3737- Lazarus 0.9.13 snapshot. Free Pascal 2.1 that comes with the installer. The LCL is smartlinked. Refered from now on as simply 0.9.13 + 2.1 + SL.
3838
3939| | 0.9.12 | 0.9.13 + 2.0.2 | 0.9.13 + 2.1 + SL | 0.9.13 + 2.1 |
4040|----|----|----|----|----|
4141| File Size after strip (in bytes) | 1,108,480 | 1,587,712 | 1,425,408 | 1,649,152 |
4242| File Size after UPX (in bytes) | 318,976 | 438,272 | 388,608 | 454,144 |
4343| Linking time | 15 seconds | 5 seconds | 45 seconds | 10 seconds |
4444
45-<figure>
46-<img src="OuptutFileSizesComparisonChart.png" title="OuptutFileSizesComparisonChart.png" />
47-<figcaption>OuptutFileSizesComparisonChart.png</figcaption>
48-</figure>
45+![](/wiki-images/a/a1/OuptutFileSizesComparisonChart.png)
4946
5047**Conclusion**
5148
5249The 0.9.13 snapshot from the 8th of February 2006 features a unstable compiler from the 2.1 branch, which can cause the bigger executables and slower linking time as compared to the other versions.
5350
5451The 0.9.12 version has the best file size of all, both with UPX and without, showing that Smartlinking really can diminish the file size in Windows. This, however, does not come without a cost, and the cost is linking time, which is about 3 times higher then without smartlinking.
5552
5653The 0.9.12 version already comes fully configured for Smartlinking on Windows and no extra configuration is needed. This was not the case on previous releases.
5754
5855## Remove unused functions in a class
5956
6057Q: If a virtual method of a class is not used at all in the programm will the compiler remove it from the executable ?
6158
6259A: If you use FPC 2.3.1 with whole-program optimization and the compiler can prove that it is never callable: yes. See [[Whole Program Optimization]] for more information. In case this is in relation to the thread on the Lazarus list about the big executables: note that it has only a limited effect on Lazarus programs, because almost all linked LCL code can potentially be executed (due to the way the LCL is constructed). In fact, I think most savings there come from making a number of virtual method calls non-virtual, rather than from throwing away unreachable code.
6360
6461The internal linker can also do it (only throwing away virtual method calls, not turning virtual method calls into static ones), but only on Windows platforms. It is however not currently enabled in the compiler, because the changes break the external linker. It should therefore be turned into a command line option (along with a check that produces an error if you try to link a unit compiled with the option using the external linker), but that hasn't been done yet.
6562
6663Q: How can the compiler determine that a virtual method is unused at all,
6764
6865A: If you have a class hierarchy TBase-\>TDerived1-\>TDerived2 with a virtual method called "vmethod", and nowhere in the program there is a call to vmethod, then it is unused. Or if it is only called using TDerived2 instances, then if the linker does not find any direct references to TDerived1.vmethod or TBase.vmethod (e.g., via "inherited" calls from TDerived2 methods), it knows that the VMT entries for "vmethod" in TDerived1 and TBase can be set to nil.
6966
7067## Bytes Used
7168
7269Here's a layout by pascal units in bytes used for code. The size is based on Dwarf2 information generated. Doesn't include resources size used by a unit.
7370
7471The project is a single form application (no additional controls used). Lazarus used is a Trunk version of 23 Jan 2020.
7572
7673No optimization was used during compilation.
7774
7875| Size | Lib | Package | Category | File name |
7976|--------|------|----------|----------|--------------------------------|
8077| 238704 | lcl | base | | controls.pp |
8178| 144064 | lcl | base | | graphics.pp |
8279| 111280 | lcl | ws | | win32/win32int.pp |
8380| 106880 | lcl | base | | forms.pp |
8481| 98000 | rtl | rtl | | system.pp |
8582| 94432 | lcl | base | | intfgraphics.pas |
8683| 90352 | lcl | base | | comctrls.pp |
8784| 90224 | rtl | rtl | | classes.pp |
8885| 59552 | lcl | base | | stdctrls.pp |
8986| 51888 | lcl | base | | interfacebase.pp |
9087| 50336 | rtl | rtl | | ../win/sysutils.pp |
9188| 37728 | lcl | base | | imglist.pp |
9289| 35456 | rtl | rtl | | rtl-objpas/src/inc/variants.pp |
9390| 33232 | lcl | lazutils | | lazutf8.pas |
9491| 31456 | lcl | base | | extctrls.pp |
9592| 30912 | fcl | image | | fcl-image/src/fpreadtiff.pas |
9693| 30816 | lcl | ws | | win32/win32wscomctrls.pp |
9794| 30800 | lcl | ws | | win32/win32wsmenus.pp |
9895| 28832 | lcl | base | | menus.pp |
9996| 25984 | lcl | base | | maskedit.pp |
10097| 24480 | rtl | rtl | | rtl-objpas/src/win/varutils.pp |
10198| 24048 | lcl | base | | dialogs.pp |
10299| 23632 | fcl | json | | fcl-json/src/fpjson.pp |
103100| 23456 | lcl | ws | | win32/win32wsstdctrls.pp |
104101| 23408 | lcl | base | | lclintf.pas |
105102| 18448 | lcl | base | | lresources.pp |
106103| 17904 | lcl | lazutils | | textstrings.pas |
107104| 16736 | fcl | image | | fcl-image/src/fpcanvas.pp |
108105| 16544 | lcl | lazutils | | graphtype.pp |
109106| 13408 | lcl | base | | themes.pas |
110107| 13168 | lcl | ws | | win32/win32proc.pp |
111108| 13136 | fcl | image | | fcl-image/src/pixtools.pp |
112109| 13088 | lcl | lazutils | | lazloggerbase.pas |
113110| 12368 | lcl | lazutils | | laz_avl_tree.pp |
114111| 11872 | fcl | image | | fcl-image/src/fpimage.pp |
115112| 11776 | lcl | base | | actnlist.pas |
116113| 11488 | lcl | base | | buttons.pp |
117114| 10576 | rtl | debug | | ../inc/heaptrc.pp |
118115| 10544 | lcl | ws | | win32/win32wsbuttons.pp |
119116| 10464 | rtl | rtl | | ../objpas/typinfo.pp |
120117| 9744 | fcl | image | | fcl-image/src/fpreadpng.pp |
121118| 9696 | lcl | base | | graphmath.pp |
122119| 9536 | lcl | lazutils | | lazlogger.pas |
123120| 9424 | lcl | lazutils | | lazfileutils.pas |
124121| 9296 | lcl | base | | buttonpanel.pas |
125122| 9248 | fcl | image | | fcl-image/src/fpwritetiff.pas |
126123| 8864 | lcl | base | | clipbrd.pp |
127124| 8768 | lcl | ws | | win32/win32extra.pas |
128125| 8768 | fcl | jpeg | | pasjpeg/src/jquant2.pas |
129126| 8720 | fcl | jpeg | | pasjpeg/src/jdmarker.pas |
130127| 8160 | fcl | image | | fcl-image/src/fpwritepng.pp |
131128| 8016 | fcl | image | | fcl-image/src/fpwritebmp.pp |
132129| 7792 | fcl | zlib | | paszlib/src/trees.pas |
133130| 7648 | fcl | zlib | | paszlib/src/zdeflate.pas |
134131| 7536 | fcl | image | | fcl-image/src/ellipses.pp |
135132| 7472 | lcl | ws | | win32/win32wsforms.pp |
136133| 7456 | fcl | image | | fcl-image/src/fpreadbmp.pp |
137134| 7328 | lcl | ws | | win32/win32themes.pas |
138135| 6880 | fcl | jpeg | | pasjpeg/src/jdcoefct.pas |
139136| 6864 | lcl | base | | lclproc.pas |
140137| 6624 | lcl | lazutils | | lazutf16.pas |
141138| 6400 | lcl | ws | | win32/win32wscontrols.pp |
142139| 6240 | fcl | zlib | | paszlib/src/infblock.pas |
143140| 5920 | lcl | base | | lclrescache.pas |
144141| 5792 | fcl | image | | fcl-image/src/fpreadjpeg.pas |
145142| 5728 | fcl | image | | fcl-image/src/fppixlcanv.pp |
146143| 5680 | fcl | ws | | win32/win32wsspin.pp |
147144| 5648 | lcl | lazutils | | maps.pp |
148145| 5632 | lcl | ws | | win32/win32wsimglist.pp |
149146| 5568 | fcl | jpeg | | pasjpeg/src/jchuff.pas |
150147| 5520 | fcl | jpeg | | pasjpeg/src/jmemmgr.pas |
151148| 5392 | fcl | jpeg | | pasjpeg/src/jquant1.pas |
152149| 5264 | fcl | image | | fcl-image/src/fpreadgif.pas |
153150| 5232 | fcl | jpeg | | pasjpeg/src/jdphuff.pas |
154151| 5152 | lcl | base | | imagelistcache.pas |
155152| 5104 | lcl | base | | widgetset/wsstdctrls.pp |
156153| 5008 | fcl | json | | fcl-json/src/jsonscanner.pp |
157154| 4992 | lcl | base | | widgetset/wslclclasses.pp |
158155| 4880 | fcl | jpeg | | pasjpeg/src/jcphuff.pas |
159156| 4848 | fcl | utils | | fcl-base/src/contnrs.pp |
160157| 4816 | fcl | image | | fcl-image/src/fptiffcmn.pas |
161158| 4768 | rtl | debug | | ../inc/lnfodwrf.pp |
162159| 4640 | fcl | jpeg | | pasjpeg/src/jcmaster.pas |
163160| 4592 | fcl | jpeg | | pasjpeg/src/jdhuff.pas |
164161| 4512 | fcl | jpeg | | pasjpeg/src/jcparam.pas |
165162| 4256 | fcl | jpeg | | pasjpeg/src/jcsample.pas |
166163| 4208 | lcl | base | | widgetset/wscomctrls.pp |
167164| 4160 | fcl | zlib | | paszlib/src/infcodes.pas |
168165| 3936 | fcl | zlib | | paszlib/src/inftrees.pas |
169166| 3616 | fcl | jpeg | | pasjpeg/src/jcmarker.pas |
170167| 3584 | fcl | zlib | | paszlib/src/zstream.pp |
171168| 3584 | fcl | jpeg | | pasjpeg/src/jidctint.pas |
172169| 3488 | fcl | image | | fcl-image/src/fpwritepnm.pp |
173170| 3472 | fcl | image | | fcl-image/src/fpreadpnm.pp |
174171| 3360 | fcl | jpeg | | pasjpeg/src/jdmerge.pas |
175172| 3312 | lcl | base | | icnstypes.pas |
176173| 3248 | fcl | jpeg | | pasjpeg/src/jccoefct.pas |
177174| 3216 | fcl | jpeg | | pasjpeg/src/jdsample.pas |
178175| 3168 | fcl | jpeg | | pasjpeg/src/jdmainct.pas |
179176| 3152 | lcl | base | | widgetset/wsimglist.pp |
180177| 3120 | lcl | base | | spin.pp |
181178| 3120 | fcl | jpeg | | pasjpeg/src/jdmaster.pas |
182179| 3104 | rtl | winapi | | winunits-base/src/uxtheme.pp |
183180| 2992 | lcl | base | | widgetset/wscontrols.pp |
184181| 2944 | fcl | json | | fcl-json/src/jsonparser.pp |
185182| 2896 | fcl | jpeg | | pasjpeg/src/jccolor.pas |
186183| 2848 | fcl | zlib | | paszlib/src/zinflate.pas |
187184| 2832 | fcl | jpeg | | pasjpeg/src/jdcolor.pas |
188185| 2816 | fcl | jpeg | | pasjpeg/src/jcdctmgr.pas |
189186| 2752 | lcl | lazutils | | lazmethodlist.pas |
190187| 2736 | fcl | jpeg | | pasjpeg/src/jidctred.pas |
191188| 2704 | rtl | debug | | ../inc/exeinfo.pp |
192189| 2688 | fcl | jpeg | | pasjpeg/src/jidctfst.pas |
193190| 2576 | fcl | jpeg | | pasjpeg/src/jidctflt.pas |
194191| 2496 | rtl | rtl | | ../objpas/fgl.pp |
195192| 2464 | fcl | jpeg | | pasjpeg/src/jdinput.pas |
196193| 2448 | lcl | ws | | win32/win32wsfactory.pas |
197194| 2352 | fcl | image | | fcl-image/src/fpwritejpeg.pas |
198195| 2320 | fcl | jpeg | | pasjpeg/src/jcprepct.pas |
199196| 2208 | lcl | lazutils | | lclclasses.pp |
200197| 2032 | fcl | jpeg | | pasjpeg/src/jerror.pas |
201198| 2016 | rtl | winapi | | winunits-base/src/multimon.pp |
202199| 2016 | fcl | jpeg | | pasjpeg/src/jfdctint.pas |
203200| 1952 | rtl | winapi | | windows.pp |
204201| 1888 | fcl | jpeg | | pasjpeg/src/jdapimin.pas |
205202| 1872 | fcl | image | | fcl-image/src/clipping.pp |
206203| 1824 | fcl | zlib | | paszlib/src/inffast.pas |
207204| 1808 | lcl | base | | extendedstrings.pas |
208205| 1712 | lcl | base | | customtimer.pas |
209206| 1648 | lcl | base | | helpintfs.pas |
210207| 1584 | fcl | jpeg | | pasjpeg/src/jfdctfst.pas |
211208| 1584 | fcl | jpeg | | pasjpeg/src/jfdctflt.pas |
212209| 1584 | rtl | rtl | | ../objpas/math.pp |
213210| 1472 | fcl | jpeg | | pasjpeg/src/jdpostct.pas |
214211| 1360 | fcl | jpeg | | pasjpeg/src/jdapistd.pas |
215212| 1280 | fcl | utils | | fcl-base/src/custapp.pp |
216213| 1200 | lcl | lazutils | | lazfilecache.pas |
217214| 1152 | lcl | base | | widgetset/wsmenus.pp |
218215| 1152 | lcl | lazutils | | lazclasses.pas |
219216| 1120 | fcl | jpeg | | pasjpeg/src/jddctmgr.pas |
220217| 1056 | lcl | base | | widgetset/wsforms.pp |
221218| 1040 | lcl | lazutils | | integerlist.pas |
222219| 1024 | fcl | jpeg | | pasjpeg/src/jcapimin.pas |
223220| 992 | lcl | base | | lcltype.pp |
224221| 848 | lcl | lazutils | | lazstringutils.pas |
225222| 832 | fcl | image | | fcl-image/src/fpimgcanv.pp |
226223| 768 | lcl | base | | widgetset/wsproc.pp |
227224| 688 | rtl | rtl | | ../objpas/types.pp |
228225| 672 | lcl | ws | | win32/win32wsdialogs.pp |
229226| 672 | lcl | lazutils | | lazutf8classes.pas |
230227| 656 | fcl | jpeg | | pasjpeg/src/jcmainct.pas |
231228| 656 | rtl | rtl | | ../inc/fpintres.pp |
232229| 640 | lcl | lazutils | | laztracer.pas |
233230| 624 | fcl | jpeg | | pasjpeg/src/jdatasrc.pas |
234231| 560 | fcl | image | | fcl-image/src/fpimgcmn.pp |
235232| 528 | fcl | zlib | | paszlib/src/infutil.pas |
236233| 528 | fcl | utils | | fcl-base/src/syncobjs.pp |
237234| 512 | lcl | base | | lclmessageglue.pas |
238235| 496 | fcl | jpeg | | pasjpeg/src/jutils.pas |
239236| 496 | fcl | jpeg | | pasjpeg/src/jdatadst.pas |
240237| 480 | fcl | jpeg | | pasjpeg/src/jcapistd.pas |
241238| 464 | rtl | winapi | | winunits-base/src/commctrl.pp |
242239| 432 | lcl | base | | widgetset/wsextctrls.pp |
243240| 432 | fcl | zlib | | paszlib/src/zbase.pas |
244241| 384 | fcl | jpeg | | pasjpeg/src/jmemnobs.pas |
245242| 368 | fcl | jpeg | | pasjpeg/src/jcomapi.pas |
246243| 336 | rtl | rtl | | ../objpas/objpas.pp |
247244| 288 | lcl | base | | widgetset/wsdialogs.pp |
248245| 288 | fcl | xml | | fcl-xml/src/htmldefs.pp |
249246| 256 | fcl | jpeg | | pasjpeg/src/jinclude.pas |
250247| 256 | fcl | jpeg | | pasjpeg/src/jcinit.pas |
251248| 256 | lcl | lazutils | | lcltaskdialog.pas |
252249| 256 | lcl | lazutils | | laz2_xmlread.pas |
253250| 224 | lcl | base | | widgetset/wsspin.pp |
254251| 208 | lcl | base | | widgetset/wsbuttons.pp |
255252| 208 | fcl | zlib | | paszlib/src/adler.pas |
256253| 192 | rtl | rtl | | ../objpas/unicodedata.pas |
257254| 144 | lcl | lazutils | | avglvltree.pas |
258255| 128 | lcl | base | | widgetset/wsreferences.pp |
259256| 112 | lcl | lazutils | | stringhashlist.pas |
260257| 112 | main | app | | project1.lpr |
261258| 96 | rtl | winapi | | ../win/windirs.pp |
262259| 96 | rtl | winapi | | ../win/dos.pp |
263260| 80 | lcl | base | | lclversion.pas |
264261| 80 | lcl | base | | lclplatformdef.pas |
265262| 80 | lcl | base | | forms/calcform.pas |
266263| 80 | lcl | lazutils | | fileutil.pas |
267264| 64 | lcl | ws | | win32/interfaces.pp |
268265| 64 | lcl | lazutils | | lazutilities.pas |
269266| 64 | lcl | lazutils | | laz2_dom.pas |
270267| 64 | rtl | rtl | | ../inc/strings.pp |
271268| 48 | lcl | lazutils | | lconvencoding.pas |
272269| 48 | lcl | lazutils | | lazsysutils.pas |
273270| 48 | lcl | lazutils | | laz2_xmlutils.pas |
274271| 48 | lcl | lazutils | | fpcadds.pas |
275272| 48 | fcl | utils | | fcl-base/src/rttiutils.pp |
276273| 32 | fcl | utils | | fcl-base/src/gettext.pp |
277274
278275- Note, that zlib is used for PNG images reading and writting
279276
280277### by Lib
281278
282279| lib | code | percentage |
283280|-------|---------|------------|
284281| rtl | 340752 | 15.46% |
285282| lcl | 1491184 | 67.75% |
286283| fcl | 372176 | 16.88% |
287284| main | 122 | 0.01% |
288285| Total | 2204234 | 100% |
289286
290287## See Also
291288
292289- [[Size Matters]]
293290
294291---
295292*From the Free Pascal wiki, page "File size and smartlinking", as it stood on 2020-01-24 after a change by Skalogryz, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
296293Category: FPC
297294Category: Lazarus

Back to history