Big problem with Small programs
Sean Kelly
sean at f4.ca
Tue Jan 23 12:25:38 PST 2007
Sean Kelly wrote:
> Walter Bright wrote:
>> kris wrote:
>>> I have a helloworld.d program. I compile it on both Win32 and on
>>> linux. On linux, the executable is 72KB whereas on Win32 it is 141KB.
>>> Why is this?
>>>
>>> Turns out that D programs importing comprehensive Win32 D headers
>>> will wind up with executable space occupied for *all* constants and
>>> *all* struct initializers for the darned Win32 decls; where none of
>>> them are actually used.
>>>
>>> That amounts to ~70KB of junk in the executable -- almost a full 100%
>>> increase in size beyond what it should be.
>>>
>>> Does OptLink remove this? I've had no success with it. Walter? Can
>>> you help with this?
>>
>> Try using enums instead of const variables, they don't take up any
>> space in the object file.
>
> For reference, the "hello world" app test was compiled with Bud, which
> basically just recursively compiled all .d files and links the objects
> together. However, I just tried creating a library out of the Win32
> modules and renamed them to .di files to keep Bud from building them,
> and the resulting app was 90k instead of 144k. I don't suppose you can
> explain how linking a lib and linking individual object files can have
> such glaringly different effects? I'm going to try a manual build
> without Bud and see if the app is 144k now too.
I'm beginning to suspect a compiler/linker bug. Here is my first test
supplying all modules on one line:
--------------------------------------------------------------------------------
C:\code\src\d\test\appsize>dmd -inline -release -O hello.d
\code\import\tango\tango\io\Console.d
\code\import\tango\tango\sys\Common.d
\code\import\tango\tango\sys\win32\Types.d
\code\import\tango\tango\sys\win32\Common.d
\code\import\tango\tango\io\Buffer.d
\code\import\tango\tango\io\Exception.d
\code\import\tango\tango\io\model\IBuffer.d
\code\import\tango\tango\io\model\IConduit.d
\code\import\tango\tango\io\DeviceConduit.d
\code\import\tango\tango\io\Conduit.d
c:\bin\dmd\bin\..\..\dm\bin\link.exe
hello+Console+Common+Types+Common+Buffer+Ex
ception+IBuffer+IConduit+DeviceConduit+Conduit,,,user32+kernel32/noi;
C:\code\src\d\test>dir hello.exe
Volume in drive C is System
Volume Serial Number is 58B0-B8C5
Directory of C:\code\src\d\test\appsize
01/23/2007 12:05 PM 141,852 hello.exe
--------------------------------------------------------------------------------
And my second test compiling all modules separately and then linking
into an app:
--------------------------------------------------------------------------------
dmd -inline -release -O -c hello.d
dmd -inline -release -O -c \code\import\tango\tango\io\Console.d
dmd -inline -release -O -c \code\import\tango\tango\sys\Common.d
dmd -inline -release -O -c \code\import\tango\tango\sys\win32\Types.d
dmd -inline -release -O -c \code\import\tango\tango\sys\win32\Common.d
-ofCommonWin32.obj
dmd -inline -release -O -c \code\import\tango\tango\io\Buffer.d
dmd -inline -release -O -c \code\import\tango\tango\io\Exception.d
dmd -inline -release -O -c \code\import\tango\tango\io\model\IBuffer.d
dmd -inline -release -O -c \code\import\tango\tango\io\model\IConduit.d
dmd -inline -release -O -c \code\import\tango\tango\io\DeviceConduit.d
dmd -inline -release -O -c \code\import\tango\tango\io\Conduit.d
C:\code\src\d\test\appsize>dmd hello *.obj
c:\bin\dmd\bin\..\..\dm\bin\link.exe hello+*,,,user32+kernel32/noi;
C:\code\src\d\test\appsize>dir hello.exe
Volume in drive C is System
Volume Serial Number is 58B0-B8C5
Directory of C:\code\src\d\test\appsize
01/23/2007 12:13 PM 141,340 hello.exe
--------------------------------------------------------------------------------
And now my third test where tango/sys/win32/*.* is compiled into a
library (win32.lib) and the lib is linked instead of the module object
files:
--------------------------------------------------------------------------------
C:\code\src\d\test\appsize>lib -c -n Win32.lib CommonWin32.obj Types.obj
Digital Mars Librarian Version 8.00n
Copyright (C) Digital Mars 2000-2002 All Rights Reserved www.digitalmars.com
Digital Mars Librarian complete.
C:\code\src\d\test\appsize>del CommonWin32.obj Types.obj
C:\code\src\d\test\appsize>dmd hello *.obj Win32.lib
c:\bin\dmd\bin\..\..\dm\bin\link.exe
hello+*,,,Win32.lib+user32+kernel32/noi;
C:\code\src\d\test\appsize>dir hello.exe
Volume in drive C is System
Volume Serial Number is 58B0-B8C5
Directory of C:\code\src\d\test\appsize
01/23/2007 12:20 PM 90,140 hello.exe
--------------------------------------------------------------------------------
As you can see, simply putting the Win32 object files into a library
prior to linking reduced the application size by 51,100 bytes, but
separate compilation had the same (bad) result as compiling all modules
on one line. I grant that 51k may not be a huge issue for the average
Windows app, but it could be a killer for handheld (WinCE) applications.
Could you please explain the size difference if this is indeed not a
bug? I don't mind making these files .di files and adding a pragma(lib,
"Win32.lib"), but this seems like something that must be addressed in a
more general manner at some point.
Sean
More information about the Digitalmars-d
mailing list