Even trivially small Haskell programs turn into gigantic executables.
I’ve written a small program, that was compiled (with GHC) to the binary with the size extending 7 MB!
What can cause even a small Haskell program to be compiled to the huge binary?
What, if anything, can I do to reduce this?
Let’s see what’s going on, try
You see from the
lddoutput that GHC has produced a dynamically linked executable, but only the C libraries are dynamically linked! All the Haskell libraries are copied in verbatim.Aside: since this is a graphics-intensive app, I’d definitely compile with
ghc -O2There’s two things you can do.
Stripping symbols
An easy solution: strip the binary:
Strip discards symbols from the object file. They are generally only needed for debugging.
Dynamically linked Haskell libraries
More recently, GHC has gained support for dynamic linking of both C and Haskell libraries. Most distros now distribute a version of GHC built to support dynamic linking of Haskell libraries. Shared Haskell libraries may be shared amongst many Haskell programs, without copying them into the executable each time.
At the time of writing Linux and Windows are supported.
To allow the Haskell libraries to be dynamically linked, you need to compile them with
-dynamic, like so:Also, any libraries you want to be shared should be built with
--enabled-shared:And you’ll end up with a much smaller executable, that has both C and Haskell dependencies dynamically resolved.
And, voilà!
which you can strip to make even smaller:
An eensy weensy executable, built up from many dynamically linked C and Haskell pieces:
One final point: even on systems with static linking only, you can use -split-objs, to get one .o file per top level function, which can further reduce the size of statically linked libraries. It needs GHC to be built with -split-objs on, which some systems forget to do.