Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making small haskell executables?

Are there any good ways to make small haskell executables? With ghc6 a simple hello world program seems to come to about 370kB (523kB before strip). Hello world in C is about 4kB (9kB before strip).

like image 556
Anthony Towns Avatar asked Mar 31 '09 03:03

Anthony Towns


2 Answers

With the development branch of GHC (anyone know exactly which version this was added in?):

$ ghc -o hello hello.hs
$ strip -p --strip-unneeded --remove-section=.comment -o hello-small hello
$ du hello hello-small
700 hello
476 hello-small

Add the -dynamic flag for a dynamically linked RTS:

$ ghc -dynamic -o hello hello.hs
$ strip -p --strip-unneeded --remove-section=.comment -o hello-small hello
$ du hello hello-small
24  hello
16  hello-small

See also: http://hackage.haskell.org/trac/ghc/wiki/SharedLibraries/PlatformSupport

For comparison with C:

$ gcc hello.c -o hello
$ strip -p --strip-unneeded --remove-section=.comment -o hello-small hello
$ du hello hello-small
12  hello
8   hello-small
like image 53
Caleb Case Avatar answered Nov 10 '22 12:11

Caleb Case


GHC is statically linking everything (except libraries used by runtime itself, which are linked dynamically).

In the old ages, GHC linked the whole (haskell) library in as soon as you've used something from it. Sometime ago, GHC started to link "per obj file", which drastically reduced the binary size. Judging from the size, you must've been using the newer GHC already.

On the plus side, you already have a lot of stuff in those 500K, like multithreaded core, garbage collector etc.

Add at least garbage collector to your C code, then compare them again :)

like image 21
ADEpt Avatar answered Nov 10 '22 11:11

ADEpt