Why are boxed vectors so slow?

前端 未结 1 523
北恋
北恋 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条回答
  •  萌比男神i
    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
    <>
    $ cat ./testVals | ./OldUnboxed +RTS -t > UBxd.txt
    <>
    

    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
    <>
    $ cat ./testVals | ./Unboxed +RTS -t > UBxd.txt
    <>
    

    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)
提交回复
热议问题