[Haskell-cafe] How to ensure optimization for large immutable vectors to be shared w.r.t. Referential Transparency

YueCompl compl.yue at icloud.com
Tue Apr 6 13:51:29 UTC 2021


Appears it'd work as expected when the immutable vector is originally created from foreign ptr, I think it'll work for my cases. 
(Though it's still strangely unexpected for ad hoc immutable vectors unshared when wrapped.)

```hs
λ> :set -XBangPatterns
λ> 
λ> :set -package vector
package flags have changed, resetting and loading new packages...
λ> 
λ> import Prelude
λ> 
λ> import Foreign.Marshal.Alloc
λ> import Foreign.Storable
λ> import Foreign.ForeignPtr
λ> 
λ> import qualified Data.Vector.Storable as VS
λ> 
λ> :{
λ| 
λ| data SomeVector = SomeVector (VS.Vector Int)
λ| 
λ| isSameVector :: Storable a => VS.Vector a -> VS.Vector a -> Bool
λ| isSameVector !x !y = 
λ|   x'offset == y'offset && x'fp == y'fp
λ|  where
λ|   (x'fp, x'offset, _x'len) = VS.unsafeToForeignPtr x
λ|   (y'fp, y'offset, _y'len) = VS.unsafeToForeignPtr y
λ| 
λ| isSame :: SomeVector -> SomeVector -> Bool
λ| isSame (SomeVector !x) (SomeVector !y) = isSameVector x y
λ| 
λ| :}
λ> 
λ> let !v = VS.fromList [3,2,5] in isSame (SomeVector v) (SomeVector v)
False
λ> 
λ> let !v = SomeVector (VS.fromList [3,2,5]) in isSame v v
True
λ> 
λ> 
λ> (fp :: ForeignPtr Int) <- mallocBytes 256 >>= newForeignPtr_
λ> let !v = VS.unsafeFromForeignPtr fp 0 32
λ| 
λ> isSame (SomeVector v) (SomeVector v)
True
λ> 
```

> On 2021-04-06, at 20:50, Henning Thielemann <lemming at henning-thielemann.de> wrote:
> 
> 
> On Tue, 6 Apr 2021, YueCompl wrote:
> 
>> ```hsλ> let !v = VS.fromList [3,2,5] in isSameVector (SomeVector v) (SomeVector v)
>> False
>> λ> 
>> λ> let !v = SomeVector (VS.fromList [3,2,5]) in isSameVector v v
>> True
>> λ> 
>> ```
> 
> Then I have no idea.
> 
> Maybe ghc-heap-view/ghc-vis can reveal the mystery.
> 
> https://github.com/nomeata/haskell-bytes-bobkonf2021

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20210406/65c698f3/attachment.html>


More information about the ghc-devs mailing list