如果我定义 Kolakoski Sequence作为
kolakoski :: () -> [Int]
kolakoski () = 1 : 2 : helper ()
where
helper () = 2 : concat (zipWith replicate (helper ()) (cycle [1, 2]))
并找到第 500,000,000 项
kolakoski () !! 500000000
我发现当使用 ghc -O 编译时,这会很快消耗大量内存。但是在关闭优化的情况下,它几乎没有使用任何东西。哪个优化导致了这个问题,我该如何关闭它?
最佳答案
让我们比较实际数字。如果在没有优化的情况下运行,您的 kolakoski 版本大约使用 70k:
$ ghc --make Kolakoski-Unit && ./Kolakoski-Unit +RTS -s
2
288,002,359,096 bytes allocated in the heap
1,343,933,816 bytes copied during GC
67,576 bytes maximum residency (422 sample(s))
52,128 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 551615 colls, 0 par 1.89s 2.30s 0.0000s 0.0001s
Gen 1 422 colls, 0 par 0.02s 0.02s 0.0001s 0.0001s
INIT time 0.00s ( 0.00s elapsed)
MUT time 37.34s ( 37.25s elapsed)
GC time 1.91s ( 2.33s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 39.25s ( 39.58s elapsed)
%GC time 4.9% (5.9% elapsed)
Alloc rate 7,712,197,063 bytes per MUT second
Productivity 95.1% of total user, 94.3% of total elapsed
With optimization, it uses about ~4GB (althhough the actual memory usage in the task manager goes up to ~6GB).
$ ghc --make Kolakoski-Unit -O && ./Kolakoski-Unit +RTS -s
2
64,000,066,608 bytes allocated in the heap
27,971,527,816 bytes copied during GC
3,899,031,480 bytes maximum residency (34 sample(s))
91,679,728 bytes maximum slop
9549 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 122806 colls, 0 par 8.67s 9.48s 0.0001s 0.0148s
Gen 1 34 colls, 0 par 11.55s 69.78s 2.0524s 56.2970s
INIT time 0.00s ( 0.00s elapsed)
MUT time 8.80s ( 8.43s elapsed)
GC time 20.22s ( 79.26s elapsed)
EXIT time 0.03s ( 0.89s elapsed)
Total time 29.05s ( 88.58s elapsed)
%GC time 69.6% (89.5% elapsed)
Alloc rate 7,275,318,406 bytes per MUT second
Productivity 30.4% of total user, 10.0% of total elapsed
If we use a list based version and no optimization, the memory consumption is very similar to the one with optimizations enabled:
kolakoskiList :: [Int]
kolakoskiList = 1 : 2 : helper
where
helper = 2 : concat (zipWith replicate helper (cycle [1, 2]))
$ ghc --make Kolakoski-List && ./Kolakoski-List +RTS -s
2
96,000,143,328 bytes allocated in the heap
26,615,974,536 bytes copied during GC
3,565,429,808 bytes maximum residency (34 sample(s))
83,610,688 bytes maximum slop
8732 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 184252 colls, 0 par 9.98s 10.16s 0.0001s 0.0006s
Gen 1 34 colls, 0 par 10.45s 21.61s 0.6357s 12.0792s
INIT time 0.00s ( 0.00s elapsed)
MUT time 12.02s ( 11.88s elapsed)
GC time 20.44s ( 31.77s elapsed)
EXIT time 0.05s ( 0.69s elapsed)
Total time 32.50s ( 44.34s elapsed)
%GC time 62.9% (71.7% elapsed)
Alloc rate 7,989,608,807 bytes per MUT second
Productivity 37.1% of total user, 27.2% of total elapsed
Now, we can check the GHC flag reference for the flags that get automatically set on -O. Since the list version seems to do the same thing as the optimized one, one might think that GHC transforms kolakoski to kolakoskiList:
kolakoskiOptimized _ = kolakoskiList
让我们用 -ddump-simpl -dsupress-all 在核心中检查一下:
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 45, types: 30, coercions: 0}
kolakoski
kolakoski =
\ ds_d10r ->
case ds_d10r of _ { () ->
: (I# 1)
(: (I# 2)
(letrec {
helper_aNo
helper_aNo =
\ ds1_d10s ->
case ds1_d10s of _ { () ->
: (I# 2)
(concat
(zipWith
(replicate) (helper_aNo ()) (cycle (: (I# 1) (: (I# 2) ([]))))))
}; } in
helper_aNo ()))
}
main
main = print $fShowInt (!! (kolakoski ()) (I# 500000000))
main
main = runMainIO main
即使您不熟悉 GHC 的核心,您也可以看到 kolakoski 与您的原始版本基本相同。现在将其与 -O -ddump-simpl -dsupress-all 进行比较:
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 125, types: 102, coercions: 9}
kolakoski6
kolakoski6 = I# 1
kolakoski5
kolakoski5 = I# 2
Rec {
go_r1NG
go_r1NG =
\ ds_a14B _ys_a14C ->
case ds_a14B of _ {
[] -> [];
: ipv_a14H ipv1_a14I ->
case _ys_a14C of _ {
[] -> [];
: ipv2_a14O ipv3_a14P ->
case ipv_a14H of _ { I# n#_a13J ->
case tagToEnum# (<=# n#_a13J 0) of _ {
False ->
let {
lvl2_s1N3
lvl2_s1N3 = : ipv2_a14O ([]) } in
letrec {
xs_a1LH
xs_a1LH =
\ m_a1LO ->
case tagToEnum# (<=# m_a1LO 1) of _ {
False -> : ipv2_a14O (xs_a1LH (-# m_a1LO 1));
True -> lvl2_s1N3
}; } in
++ (xs_a1LH n#_a13J) (go_r1NG ipv1_a14I ipv3_a14P);
True -> ++ ([]) (go_r1NG ipv1_a14I ipv3_a14P)
}
}
}
}
end Rec }
lvl_r1NH
lvl_r1NH = : kolakoski5 ([])
lvl1_r1NI
lvl1_r1NI = : kolakoski6 lvl_r1NH
Rec {
xs'_r1NJ
xs'_r1NJ = ++ lvl1_r1NI xs'_r1NJ
end Rec }
Rec {
kolakoski3
kolakoski3 = : kolakoski5 kolakoski4
kolakoski4
kolakoski4 = go_r1NG kolakoski3 xs'_r1NJ
end Rec }
kolakoski2
kolakoski2 = : kolakoski5 kolakoski3
kolakoski1
kolakoski1 = : kolakoski6 kolakoski2
kolakoski
kolakoski = \ ds_d13p -> case ds_d13p of _ { () -> kolakoski1 }
这个版本包含几个顶级CAFs ,它们将在程序的生命周期内保留。因此,您确实会生成第 500,000,000 个值的列表并保存它。
现在,那里发生了什么?函数内部的东西向外 float 。让我们再次检查标志引用。 -O 暗示了一个有希望的标志:
-ffull-lazinessTurn on full laziness (floating bindings outwards). Implied by-O.
这就是导致您的问题的标志。实际上,您可以使用 ghc --make -O -fno-full-laziness Kolakoski-Unit.hs 来获取原始内存消耗:
$ ghc --make Kolakoski-Unit.hs -O -fno-full-laziness && ./Kolakoski-Unit +RTS -s
2
192,001,417,688 bytes allocated in the heap
637,962,464 bytes copied during GC
66,104 bytes maximum residency (151 sample(s))
43,448 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 368364 colls, 0 par 1.34s 1.32s 0.0000s 0.0002s
Gen 1 151 colls, 0 par 0.00s 0.01s 0.0001s 0.0003s
INIT time 0.00s ( 0.00s elapsed)
MUT time 27.89s ( 28.13s elapsed)
GC time 1.34s ( 1.33s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 29.25s ( 29.46s elapsed)
%GC time 4.6% (4.5% elapsed)
Alloc rate 6,884,084,443 bytes per MUT second
Productivity 95.4% of total user, 94.7% of total elapsed
关于haskell - 为什么这里的早期术语不被垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043585/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or