Skip to content

Commit db1f865

Browse files
committed
Expose unboxed vector constructors for IOOp
And add some zip/unzip utility functions for unboxed `IOOp` vectors.
1 parent d11b6ee commit db1f865

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

blockio-uring.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ library
4545
System.IO.BlockIO.URingFFI
4646

4747
build-depends:
48+
, base >=4.16 && <4.22
4849
, bitvec ^>=1.1
49-
, base >=4.16 && <4.22
5050
, primitive ^>=0.9
5151
, vector ^>=0.13.2
5252

@@ -68,7 +68,7 @@ benchmark bench
6868
, primitive
6969
, random
7070
, time
71-
, unix ^>=2.8.7.0
71+
, unix ^>=2.8.7.0
7272
, vector
7373

7474
pkgconfig-depends: liburing
@@ -90,8 +90,8 @@ test-suite test
9090
, blockio-uring
9191
, primitive
9292
, tasty
93-
, tasty-quickcheck
9493
, tasty-hunit
94+
, tasty-quickcheck
9595
, vector
9696

9797
ghc-options: -threaded

src/System/IO/BlockIO.hs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{-# LANGUAGE CPP #-}
33
{-# LANGUAGE DerivingVia #-}
44
{-# LANGUAGE FlexibleInstances #-}
5-
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
65
{-# LANGUAGE MultiParamTypeClasses #-}
76
{-# LANGUAGE MultiWayIf #-}
87
{-# LANGUAGE NamedFieldPuns #-}
8+
{-# LANGUAGE PatternSynonyms #-}
99
{-# LANGUAGE RecordWildCards #-}
1010
{-# LANGUAGE StandaloneDeriving #-}
1111
{-# LANGUAGE TypeFamilies #-}
@@ -23,10 +23,14 @@ module System.IO.BlockIO (
2323

2424
-- * Performing I\/O
2525
submitIO,
26-
IOOp(IOOpRead, IOOpWrite),
2726
IOResult(IOResult, IOError),
2827
ByteCount, Errno(..),
2928

29+
-- ** IOOps
30+
IOOp(IOOpRead, IOOpWrite),
31+
pattern V_IOOp, pattern MV_IOOp,
32+
unzipIOOp, zipIOOp,
33+
unzipIOOpMutable, zipIOOpMutable,
3034
) where
3135

3236
import Data.Bit
@@ -218,11 +222,9 @@ data IOOp s = IOOpRead !Fd !FileOffset !(MutableByteArray s) !Int !ByteCount
218222
| IOOpWrite !Fd !FileOffset !(MutableByteArray s) !Int !ByteCount
219223

220224
-- Pseudo-representation used for the Unbox instance (via IsoUnbox).
221-
type UReprIOOp s = (Bit, Int, Int64,
222-
VU.DoNotUnboxStrict (MutableByteArray s),
223-
Int, Word)
225+
#define UReprIOOp(s) (Bit, Int, Int64, VU.DoNotUnboxStrict (MutableByteArray s), Int, Word)
224226

225-
instance VU.IsoUnbox (IOOp s) (UReprIOOp s) where
227+
instance VU.IsoUnbox (IOOp s) UReprIOOp(s) where
226228
{-# INLINE toURepr #-}
227229
toURepr (IOOpRead (Fd !fd) !off !buf !bufOff !cnt) =
228230
(Bit True, fromIntegral fd, fromIntegral off,
@@ -239,12 +241,62 @@ instance VU.IsoUnbox (IOOp s) (UReprIOOp s) where
239241
IOOpWrite (Fd (fromIntegral fd)) (fromIntegral off)
240242
buf (fromIntegral bufOff) (fromIntegral cnt)
241243

242-
newtype instance VUM.MVector s1 (IOOp s2) = MV_IOOp (VU.MVector s1 (UReprIOOp s2))
243-
newtype instance VU.Vector (IOOp s2) = V_IOOp (VU.Vector (UReprIOOp s2))
244-
deriving via (IOOp s `VU.As` UReprIOOp s) instance VGM.MVector VUM.MVector (IOOp s)
245-
deriving via (IOOp s `VU.As` UReprIOOp s) instance VG.Vector VU.Vector (IOOp s)
244+
newtype instance VUM.MVector s1 (IOOp s2) = MV_IOOp (VU.MVector s1 UReprIOOp(s2))
245+
newtype instance VU.Vector (IOOp s2) = V_IOOp (VU.Vector UReprIOOp(s2))
246+
deriving via (IOOp s `VU.As` UReprIOOp(s)) instance VGM.MVector VUM.MVector (IOOp s)
247+
deriving via (IOOp s `VU.As` UReprIOOp(s)) instance VG.Vector VU.Vector (IOOp s)
246248
instance VU.Unbox (IOOp s)
247249

250+
{-# INLINE unzipIOOp #-}
251+
-- | \( O(1) \) Unzip an unboxed vector of 'IOOp's into its components.
252+
unzipIOOp ::
253+
VU.Vector (IOOp s)
254+
-> ( VU.Vector Bit
255+
, VU.Vector Int
256+
, VU.Vector Int64
257+
, VU.Vector (VU.DoNotUnboxStrict (MutableByteArray s))
258+
, VU.Vector Int
259+
, VU.Vector Word
260+
)
261+
unzipIOOp (V_IOOp v) = VU.unzip6 v
262+
263+
{-# INLINE zipIOOp #-}
264+
-- | \( O(1) \) Zip an unboxed vector of 'IOOp's from its components.
265+
zipIOOp ::
266+
VU.Vector Bit
267+
-> VU.Vector Int
268+
-> VU.Vector Int64
269+
-> VU.Vector (VU.DoNotUnboxStrict (MutableByteArray s))
270+
-> VU.Vector Int
271+
-> VU.Vector Word
272+
-> VU.Vector (IOOp s)
273+
zipIOOp a b c d e f = V_IOOp (VU.zip6 a b c d e f)
274+
275+
{-# INLINE unzipIOOpMutable #-}
276+
-- | \( O(1) \) Unzip an unboxed, mutable vector of 'IOOp's into its components.
277+
unzipIOOpMutable ::
278+
VUM.MVector s1 (IOOp s2)
279+
-> ( VUM.MVector s1 Bit
280+
, VUM.MVector s1 Int
281+
, VUM.MVector s1 Int64
282+
, VUM.MVector s1 (VU.DoNotUnboxStrict (MutableByteArray s2))
283+
, VUM.MVector s1 Int
284+
, VUM.MVector s1 Word
285+
)
286+
unzipIOOpMutable (MV_IOOp v) = VUM.unzip6 v
287+
288+
{-# INLINE zipIOOpMutable #-}
289+
-- | \( O(1) \) Zip an unboxed, mutable vector of 'IOOp's from its components.
290+
zipIOOpMutable ::
291+
VUM.MVector s1 Bit
292+
-> VUM.MVector s1 Int
293+
-> VUM.MVector s1 Int64
294+
-> VUM.MVector s1 (VU.DoNotUnboxStrict (MutableByteArray s2))
295+
-> VUM.MVector s1 Int
296+
-> VUM.MVector s1 Word
297+
-> VUM.MVector s1 (IOOp s2)
298+
zipIOOpMutable a b c d e f = MV_IOOp (VUM.zip6 a b c d e f)
299+
248300
-- | Submit a batch of I\/O operations, and wait for them all to complete.
249301
-- The sequence of results matches up with the sequence of operations.
250302
-- Any I\/O errors are reported in the result list, not as IO exceptions.

0 commit comments

Comments
 (0)