<div dir="ltr"><div><div><div><div><div>Hi!<br><br>I have a &quot;low-level&quot; function for insertion element into mutable vector. It looks like this:<br><br>place :: (PrimMonad m) =&gt;<br>     MV.MVector (PrimState m) (Int, t) -&gt; (Int, t) -&gt; Int -&gt; m ()<br>
<span style="font-family:courier new,monospace">place v max@(val1,_) i = place&#39; i<br> where <br>  place&#39; i = do<br>    let j = i - 1<br>    if j &lt; 0<br>    then return ()<br>    else do<br>      curr@(val2, _) &lt;- MV.unsafeRead v j  -- &lt;&lt;&lt;&lt;&lt;<br>
      if val2 &gt; val1<br>      then do<br>        MV.unsafeWrite v j max<br>        MV.unsafeWrite v i curr<br>        place&#39; 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&#39;    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&#39;s problem 411. But now it is a different issue.<br>
</div></div>