为什么Haskell / GHC可执行文件的文件大小如此之大? [英] Why are Haskell/GHC executables so large in filesize?

查看:119
本文介绍了为什么Haskell / GHC可执行文件的文件大小如此之大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

最近我注意到Haskell可执行文件有多大。下面的所有内容均在GHC 7.4.1上使用 -O2 在Linux上进行编译。

Recently I noticed how large Haskell executables are. Everything below was compiled on GHC 7.4.1 with -O2 on Linux.


  1. Hello World( main = putStrLn Hello World! )超过800 KiB。在其上运行 strip 会将文件大小减小到500 KiB;甚至在编译中添加 -dynamic 也无济于事,这给我留下了400 KiB左右的可执行文件。

  1. Hello World (main = putStrLn "Hello World!") is over 800 KiB. Running strip over it reduces the filesize to 500 KiB; even adding -dynamic to the compilation doesn't help much, leaving me with a stripped executable around 400 KiB.

编译一个涉及Parsec的非常原始的示例将产生一个1.7 MiB文件。

Compiling a very primitive example involving Parsec yields a 1.7 MiB file.

-- File: test.hs
import qualified Text.ParserCombinators.Parsec as P
import Data.Either (either)

-- Parses a string of type "x y" to the tuple (x,y).
testParser :: P.Parser (Char, Char)
testParser = do
    a <- P.anyChar
    P.char ' '
    b <- P.anyChar
    return (a, b)

-- Parse, print result.
str = "1 2"
main = print $ either (error . show) id . P.parse    testParser "" $ str
-- Output: ('1','2')

Parsec可能是一个更大的库,但是我只使用了其中的一小部分,实际上由上面生成的优化核心代码比可执行文件小得多。

Parsec may be a larger library, but I'm only using a tiny subset of it, and indeed the optimized core code generated by the above is dramatically smaller than the executable:

$ ghc -O2 -ddump-simpl -fforce-recomp test.hs | wc -c
49190 (bytes)

因此,并非如此Parsec的代码实际上是在程序中找到的,这是我最初的假设。

Therefore, it's not the case that a huge amount of Parsec is actually found in the program, which was my initial assumption.

为什么如此庞大的可执行文件尺寸?我能做些什么(动态链接除外)?

Why are the executables of such an enormous size? Is there something I can do about it (except dynamic linking)?

推荐答案

有效减少Glasgow Haskell生成的可执行文件的大小编译器您必须关注

To effectively reduce size of the executable produced by Glasgow Haskell Compiler you have to focus on


  • 使用动态链接并通过 -dynamic 选项到ghc,这样模块代码就不会通过使用共享的(动态)库被捆绑到最终的可执行文件中。需要在系统中存在这些GHC库的共享版本!

  • 删除最终可执行文件的调试信息(通过GNU binutils的剥离工具进行fE操作)

  • 删除未使用模块的导入(不要指望动态链接会获得收益)

  • use of dynamic linking with -dynamic option passed to ghc so modules code won't get bundled into the final executable by utilizing of shared(dynamic) libraries. The existence of shared versions of these GHC's libraries in the system is required !
  • removing debugging informations of the final executable (f.E. by strip tool of GNU's binutils)
  • removing imports of unused modules (don't expect gains at dynamic linking)

简单的hello world示例具有最终大小9 KiB和Parsec测试了大约28 KiB(都是64位Linux可执行文件),我发现它很小,对于这种高级语言实现来说是可以接受的。

The simple hello world example has the final size 9 KiB and Parsec test about 28 KiB (both 64 bit Linux executables) which I find quite small and acceptable for such a high level language implementation.

这篇关于为什么Haskell / GHC可执行文件的文件大小如此之大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆