When to "modify" copy a vector?

From https://hackage.haskell.org/package/vector-0.12.0.1/docs/Data-Vector.html#v:modify

Apply destructive operation to the vector. The operation will be performed in place if it is safe, and otherwise will change the copy of the vector.

It appears that it can have dramatically different performance characteristics depending on whether it is considered “safe” to change the vector in place. This motivates the questions ...

When will the in-place modification be performed, and when will the vector be copied? Is there a way to ensure, for example, using a type system that it will be changed in place?

+6
source share
1 answer

Change calls Data.Vector.Generic.modifythat calls clone, which has the following rewrite rule:

"clone/new [Vector]" forall p.
  clone (new p) = p

So, when something is in syntactic form new p, it is not copied. There modify, slice, init, tail, take, drop, unstreamand clone- this is important, that the well-fused here. All this is closely related to the work of merging flows (paper for deep diving), which reinforces vector design.

: , . new p . , , new, . 1, , , , new , , modify , - , , create, force, modify unstream.

, :

v = fromList [1..10]
g = modify f v
f = undefined

v , new p ( fromList unstream, new), modify .

, :

v = let v0 = fromList [1..10] in 
{-# NOINLINE v #-}
g = modify f v

v - , , . modify f (new p), .

, :

g = let v = fromList [1..10]
        t = v ! 4
        v2 = modify f v
    in t + (v2 ! 4)

, - , , ( new), v.

1 . "slice/new [Vector]", .

+4

All Articles