Why are boxed vectors so slow?

前端 未结 1 474
北恋
北恋 2021-02-13 05:55

I am trying to get amortized O(n) time concatenation of vectors. It seems to be working but if I need to store boxed values (such as vectors) the result is still very s

相关标签:
1条回答
  • 2021-02-13 06:08

    I'm not sure why it has such a dramatic impact on boxed Vectors, but you're wasting a lot of time in

    V.freeze m3
    

    That creates a copy of m3 each time. So you're copying 100,000 Vectors of increasing length. You don't need the old ones anymore, so they're garbage-collected. Garbage collection of boxed Vectors takes much longer than collection of unboxed Vectors because all pointers have to be followed to see whether the pointees can be collected too. I'm a bit surprised by how much difference it makes, though.

    A few stats:

    $ cat ./testVals | ./OldBoxed +RTS -t > Bxd.txt
    <<ghc: 72590744976 bytes, 79999 GCs, 5696847/15655472 avg/max bytes residency (16 samples),
    802M in use, 0.00 INIT (0.00 elapsed), 36.97 MUT (37.01 elapsed), 52.60 GC (52.67 elapsed) :ghc>>
    $ cat ./testVals | ./OldUnboxed +RTS -t > UBxd.txt
    <<ghc: 72518297568 bytes, 78256 GCs, 1013955/2294848 avg/max bytes residency (63 samples),
    81M in use, 0.00 INIT (0.00 elapsed), 9.14 MUT (9.16 elapsed), 0.60 GC (0.60 elapsed) :ghc>>
    

    So you see that the enormous difference is due to GC, althogh MUT (the time your programme does actual work) is far lower for unboxed, too.
    Now, if we replace the offending freeze by unsafeFreeze, we get

    $ cat ./testVals | ./Boxed +RTS -t > Bxd.txt
    <<ghc: 1149880088 bytes, 2214 GCs, 5236803/17102432 avg/max bytes residency (11 samples),
    39M in use, 0.00 INIT (0.00 elapsed), 0.53 MUT (0.53 elapsed), 0.29 GC (0.29 elapsed) :ghc>>
    $ cat ./testVals | ./Unboxed +RTS -t > UBxd.txt
    <<ghc: 1152277200 bytes, 2229 GCs, 767477/2267200 avg/max bytes residency (31 samples),
    7M in use, 0.00 INIT (0.00 elapsed), 0.61 MUT (0.62 elapsed), 0.04 GC (0.04 elapsed) :ghc>>
    

    which exposes a far smaller difference. In fact, here the boxed Vector needed less mutator time than unboxed. The GC time is still much higher, though, so overall unboxed still is faster, but at 0.66s vs 0.82s, it's nothing dramatic.

    0 讨论(0)
提交回复
热议问题