[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 12:29:19 UTC 2021


Thanks, it's a good idea. Unfortunately the result remains, making me even more curious.

```hs
λ> :set -XBangPatterns
λ> 
λ> :set -package vector
package flags have changed, resetting and loading new packages...
λ> 
λ> import Prelude
λ> 
λ> import Control.Monad.ST
λ> import qualified Data.Vector.Storable as VS
λ> 
λ> :{
λ| 
λ| newtype SomeVector = SomeVector (VS.Vector Int)
λ| 
λ| isSameVector :: SomeVector -> SomeVector -> Bool
λ| isSameVector (SomeVector !x) (SomeVector !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
λ| :}
λ> 
λ> let !v = VS.fromList [5..200000] in isSameVector (SomeVector v) (SomeVector v)
False
λ> 
λ> let !v = SomeVector (VS.fromList [3,2,5]) in isSameVector v v
True
λ> 
```

> On 2021-04-06, at 20:00, Henning Thielemann <lemming at henning-thielemann.de> wrote:
> 
> 
> On Tue, 6 Apr 2021, YueCompl via Haskell-Cafe wrote:
> 
>> In an attempt to determine whether two immutable vectors can be treated as the same one to enable specific optimizations for that case, I tried to use ST to determine their respective backing foreign ptrs for comparison. But appears it can be copied when wrapped in a newtype, I wonder why it is the case, and how to avoid the copy?
> 
> You compare the ForeignPtrs of the mutable vectors. What about comparing the ForeignPtrs of the original immutable vectors?

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


More information about the ghc-devs mailing list