简体   繁体   English

GHC是否支持无GC编程?

[英]Does GHC support GC-less programming?

With -S option and a simple program: 使用-S选项和一个简单的程序:

main = do
   print "Hello"
   main

I can see that it produces some garbage: 我可以看到它产生了一些垃圾:

[...]
  1024232      4904     45992  0.000  0.000    0.428    0.530    0    0  (Gen:  1)
        0                      0.000  0.000

   1,242,080,056 bytes allocated in the heap
         271,656 bytes copied during GC
[...] 

But after removing print it apparently doesn't. 但在删除print它显然没有。 Is there a alloc-free subset of core libraries that could be used to write GC-less programs? 是否有可用于编写无GC程序的无库存核心库子集?

EDIT: There is also recent work regarding linear-types which seems to have potential to enable such feature. 编辑:最近有关于线性类型的工作似乎有可能实现这样的功能。

You can very occasionally produce small programs that perform little-to-no garbage collection. 您偶尔可以生成执行很少甚至没有垃圾收集的小程序。 For example, the following program: 例如,以下程序:

main = print $ sum [(1::Int)..1000000000]

if compiled with -O2 should run without allocating much or performing any GC worth mentioning. 如果用-O2编译应该运行而不分配太多或执行任何值得一提的GC。

However, this is generally restricted to programs that can be compiled into tight loops with "unboxed" data types (in this case, unboxed Int# values) without algebraic data structures (in this case, the list is fused out of existence). 但是,这通常仅限于可以编译为具有“未装箱”数据类型(在这种情况下,未装箱的Int#值)的紧密循环的程序,而没有代数数据结构(在这种情况下,列表融合不存在)。 There is a limited set of more complex data structures (eg, unboxed Vector s) that can also be manipulated without allocation, and if you are very careful, you might be able to write algorithms that operate on such structures without allocating much. 有一组有限的更复杂的数据结构(例如,未装箱的Vector ),也可以在没有分配的情况下进行操作。如果你非常小心,你可能能够编写在这种结构上运行的算法而不需要分配太多。

For example, the following program: 例如,以下程序:

import Control.Monad
import qualified Data.Vector.Unboxed.Mutable as V

main :: IO ()
main = do
  v <- V.new 1000000
  forM_ [0..999999] $ \i -> do
    V.write v i i
  replicateM_ 999 $
    forM_ [0..499999] $ \i -> do
      V.swap v i (999999 - i)
  print =<< V.read v 123

allocates a million-integer array and then runs through it 999 times reversing all the elements. 分配一个百万整数数组然后通过它运行999次反转所有元素。

Compiled with GHC version 8.4.3 and -O2 it allocates about 8 gigs at the beginning but doesn't do any additional allocation while running the list reversals. 使用GHC版本8.4.3和-O2编译它在开始时分配大约8个演出,但在运行列表反转时不进行任何额外分配。 My guess is that you could implement something useful, like an in-place quicksort, using similar techniques without doing any allocations and so skipping any GC. 我的猜测是你可以实现一些有用的东西,比如就地快速排序,使用类似的技术而不进行任何分配,因此跳过任何GC。

As a general rule, though, allocation is such a fundamental part of the way Haskell code compiled by GHC actually runs, that there's no reasonable subset of libraries or programming techniques that can guarantee GC-free programming. 但是,作为一般规则,分配是GHC编译的Haskell代码实际运行方式的基本部分,没有合理的库或编程技术子集可以保证无GC编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM