
Abraham Egnor
This is something I just noticed...
hello.hs: module Main(main) where
main = putStr "Hello world!\n"
hello.c: #include
int main(void) { printf("Hello world!\n"); return 0; }
[abe@shiva:~/src/test] $ ghc hello.hs -o hello_hs [abe@shiva:~/src/test] $ gcc hello.c -o hello_c [abe@shiva:~/src/test] $ ls -l hello_* -rwxr-xr-x 1 abe engy 13712 Jul 18 11:34 hello_c -rwxr-xr-x 1 abe engy 299900 Jul 18 11:33 hello_hs
Why is the binary made by ghc 20 times bigger than the one made by gcc?
I don't know for certain, but I've got a couple of guesses: 1. hello_hs is probably statically linked. hello_c is probably dynamically linked. 2. "Hello world!\n" in Haskell is boxed; in C it's un-boxed. Ditto for putStr vs. printf. The compiled code for the Haskell main appears to have a lot of code to deal with those complications; multiply that by however many functions are called by putStr.
Abe
Jon Cast

On Thursday 18 July 2002 21:09, Jon Cast wrote:
Abraham Egnor
wrote: This is something I just noticed...
hello.hs: module Main(main) where
main = putStr "Hello world!\n"
hello.c: #include
int main(void) { printf("Hello world!\n"); return 0; }
[abe@shiva:~/src/test] $ ghc hello.hs -o hello_hs [abe@shiva:~/src/test] $ gcc hello.c -o hello_c [abe@shiva:~/src/test] $ ls -l hello_* -rwxr-xr-x 1 abe engy 13712 Jul 18 11:34 hello_c -rwxr-xr-x 1 abe engy 299900 Jul 18 11:33 hello_hs
Why is the binary made by ghc 20 times bigger than the one made by gcc?
I don't know for certain, but I've got a couple of guesses:
1. hello_hs is probably statically linked. hello_c is probably dynamically linked.
Right. The ldd command gives the dependencies (on Linux): awo@asterix:~> ldd hello libc.so.6 => /lib/libc.so.6 (0x40022000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) If you compile hello.c as static binary, like gcc -static -o hello hello.c then the resulting exe is rather big, too ;-) awo@asterix:~> ls -l hello -rwxr-xr-x 1 awo users 1486299 Jul 18 21:40 hello So ghc isnt so bad, really :-) Greetings Andreas

1. hello_hs is probably statically linked. hello_c is probably dynamically linked.
Right. The ldd command gives the dependencies (on Linux):
awo@asterix:~> ldd hello libc.so.6 => /lib/libc.so.6 (0x40022000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
If you compile hello.c as static binary, like
gcc -static -o hello hello.c
then the resulting exe is rather big, too ;-)
awo@asterix:~> ls -l hello -rwxr-xr-x 1 awo users 1486299 Jul 18 21:40 hello
So ghc isnt so bad, really :-)
Okay, that makes sense. Is there some reason haskell binaries have to be statically linked? I can't seem to find a way to make them otherwise, at least with ghc. Abe

Is there some reason haskell binaries have to be statically linked? I can't seem to find a way to make them otherwise, at least with ghc.
The ELF dynamic linking format seems to be designed on the assumption of poor code reuse (e.g., C code) where calls from one module to another are rare and can therefore be expensive. Haskell code has very high levels of reuse and calls from one module to another (especially calls into Prelude, List, Monad, etc) are very common so dynamic linking imposes a very high overhead. This isn't a reason to not support dynamic linking but it's a reason to make it a low priority. Incidentally, it avoids confusion between the performance overhead of lazy evaluation and the performance overhead of dynamic linking of modular code. -- Alastair Reid alastair@reid-consulting-uk.ltd.uk Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/
participants (4)
-
Abraham Egnor
-
Alastair Reid
-
Andreas Wollschlaeger
-
Jon Cast