8

When i import a (big) module into a Main module in one of the following ways:

import Mymodule
import qualified Mymodule as M
import Mymodule (MyDatatype)

the compiled binary grows the same huge amount compared to when i don't import that module. This happens regardless of whether i use anything inside that module or not in the Main module. Shouldn't the compiler (i am using GHC on Debian Testing) only add into the binary what is needed to run it?

In my specific case i have a huge Map in Mymodule which i don't use in the Main module. Selectively importing what i really need, did not change the growth of the compiled binary.

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
Josephine
  • 457
  • 2
  • 11

1 Answers1

17

As far as GHC is concerned, import lists are only there for readability and avoiding name clashes; they don't affect what's linked in at all.

Also, even if you did only import a few functions from a library, they might still depend on the bulk of the library internally, so you shouldn't necessarily expect to see a size decrease from only using some of an available interface in general.

By default, GHC links in entire libraries, rather than only the pieces you use; you could avoid this by building libraries with the -split-objs option to GHC (or put split-objs: True in your cabal-install configuration file (~/.cabal/config on Unix)), but it slows down compilation, and is seemingly not recommended by the GHC developers:

-split-objs

Tell the linker to split the single object file that would normally be generated into multiple object files, one per top-level Haskell function or type in the module. This only makes sense for libraries, where it means that executables linked against the library are smaller as they only link against the object files that they need. However, assembling all the sections separately is expensive, so this is slower than compiling normally. Additionally, the size of the library itself (the .a file) can be a factor of 2 to 2.5 larger. We use this feature for building GHC’s libraries.

The GHC manual

This will omit unused parts of libraries you use, regardless of what you import.

You might also be interested in using shared Haskell libraries.

chwarr
  • 6,777
  • 1
  • 30
  • 57
ehird
  • 40,602
  • 3
  • 180
  • 182
  • Quoting ehird: "As far as GHC is concerned, import lists are only there for readability and avoiding name clashes; they don't affect what's linked in at all." That cannot be true, since the growth in size happens even if i only put an "import Mymodule" in the Main module without using anything inside that module. – Josephine Feb 10 '12 at 10:34
  • 1
    "Import list" refers to the list of identifiers to import in parentheses after the module name, not the list of import statements at the top of a module. – ehird Feb 10 '12 at 13:49
  • Right, that makes sense. Thanks for the clarification and the very informative answer! – Josephine Feb 10 '12 at 20:48
  • The final link is dead – SwiftsNamesake Oct 04 '17 at 02:09