This is a continuation of the question . Since the vector library does not seem to have O (1) smooth output capability, I wonder if it is possible to write O (1) smooth output function that does not include unsafeFreeze and unsafeThaw . It would use the vector stream view, I think - I am not familiar with how to write one using stream and unstream - hence this question. The reason is that this will give us the opportunity to write a caching-friendly update function on a vector where only a narrow region of the vector changes, and therefore we donโt want to go through the entire vector just to process this narrow region (and this operation can happen billions of times in each function call, so the motivation to keep overhead very low). Conversion functions such as map process the entire vector, so they will be too slow.
I have an example of what I want to do, but the upd function below uses unsafeThaw and unsafeFreeze - it does not seem to be optimized in the kernel, and also breaks the promise without using the buffer further:
module Main where import Data.Vector.Unboxed as U import Data.Vector.Unboxed.Mutable as MU import Control.Monad.ST upd :: Vector Int -> Int -> Int -> Vector Int upd vix = runST $ do v' <- U.unsafeThaw v MU.write v' ix U.unsafeFreeze v' sum :: Vector Int -> Int sum = U.sum . (\x -> upd x 0 73) . (\x -> upd x 1 61) main = print $ Main.sum $ U.fromList [1..3]
I know how to implement imperative algorithms using STVector . In case you are wondering why this alternative approach, I want to try this approach of using pure vectors to check how the GHC conversion of a particular algorithm differs when writing using fusible pure vector flows (with monadic operations under the hood, of course),
When an algorithm is written using STVector , it does not look as iterative as I would like. (I think itโs more difficult for the GHC optimizer to define loops when there is a lot of variability.) So, I am exploring this alternative approach to see that I can get a more enjoyable cycle.
source share