<p><br>
On Jun 19, 2012 12:16 AM, "Henning Thielemann" <<a href="mailto:lemming@henning-thielemann.de">lemming@henning-thielemann.de</a>> wrote:<br>
><br>
><br>
> On Mon, 18 Jun 2012, Roman Leshchinskiy wrote:<br>
><br>
>> There are type families, rank-n types, unboxed types and other goodies deep in the guts of vector so the Storable part is very much GHC-specific. To be honest, I don't think being portable is feasible for high-performance code at the moment, the language standard simply doesn't have enough tools for this. Which is a shame, really.<br>
><br>
><br>
> I am not mainly interested in the efficient implementation. I am completely ok with having the definition of (Vector a) in a separate package, such that it can be used by vector (GHC only) and storablevector (portable).<br>
><br>
> However, I have just looked into Vector.Storable and it looks like<br>
><br>
> data Vector a = Vector Int (ForeignPtr a)<br>
><br>
> I thought it was<br>
><br>
> data Vector a = Vector {len :: Int, allocated :: ForeignPtr a, start :: Ptr a}<br>
><br>
> ByteString looks like:<br>
><br>
> data ByteString = PS {allocated :: ForeignPtr Word8, start, length :: Int}<br>
><br>
> Both forms allow efficient slicing.<br>
> How do you perform efficient 'take' and 'drop' ?</p>
<p>Slicing is done by directly updating the pointer in the ForeignPtr:</p>
<p>{-# INLINE basicUnsafeSlice #-}<br>
basicUnsafeSlice i n (Vector _ fp) = <br>
Vector n (updPtr (`advancePtr` i) fp)</p>
<p>{-# INLINE updPtr #-}<br>
updPtr :: (Ptr a -> Ptr a) -> ForeignPtr a -> ForeignPtr a<br>
updPtr f (ForeignPtr p c) = <br>
case f (Ptr p) of { Ptr q -> ForeignPtr q c }</p>
<p>This saves an Int.</p>
<p>Regards,</p>
<p>Bas</p>