<div dir="ltr"><div><div><div><div><div>Hi!<br><br>I have a "low-level" function for insertion element into mutable vector. It looks like this:<br><br>place :: (PrimMonad m) =><br> MV.MVector (PrimState m) (Int, t) -> (Int, t) -> Int -> m ()<br>
<span style="font-family:courier new,monospace">place v max@(val1,_) i = place' i<br> where <br> place' i = do<br> let j = i - 1<br> if j < 0<br> then return ()<br> else do<br> curr@(val2, _) <- MV.unsafeRead v j -- <<<<<<br>
if val2 > val1<br> then do<br> MV.unsafeWrite v j max<br> MV.unsafeWrite v i curr<br> place' j<br> else return ()</span><br><br></div>It searches a right place for the i-th element, moving it towards beginning<br>
</div>. Surprisingly, it works, but is slow.<br><br>Time profiling says this:<br><span style="font-family:courier new,monospace">COST CENTRE MODULE %time %alloc ticks bytes<br><br>place.place' Main 40.1 77.1 9167 12169223232</span><br>
<br>And heap profiling says that most of allocations are for Int and (,).<br><br></div>I think, binding of <span style="font-family:courier new,monospace">unsafeRead</span> to <span style="font-family:courier new,monospace">curr</span> and <span style="font-family:courier new,monospace">val</span> might allocate my Precious memory more than, maybe, it is needed.<br>
<br></div>Am I right? Is it some strictness that I need? Is there anything I can do in this situation? Or should I look outside of this function?<br><br></div><div>PS Emergence of this piece of code is a long story. Originally I was trying to solve Project Euler's problem 411. But now it is a different issue.<br>
</div></div>