Skip to content

Pass binary parameter data without copying #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Database/PostgreSQL/LibPQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,26 @@ connectionUsedPassword connection =
newtype Result = Result (ForeignPtr PGresult) deriving (Eq, Show)
data PGresult

-- | Prepare the given parameter bytestring for passing on to libpq,
-- without copying for binary parameters.
--
-- This is safe to use to pass parameters to libpq considering:
-- * libpq treats the parameter data as read-only
-- * 'ByteString' uses pinned memory
-- * the reference to the 'CString' doesn't escape
unsafeUseParamAsCString :: (B.ByteString, Format) -> (CString -> IO a) -> IO a
unsafeUseParamAsCString (bs, format) =
case format of
Binary -> B.unsafeUseAsCString bs
Text -> B.useAsCString bs

-- | Convert a list of parameters to the format expected by libpq FFI calls.
withParams :: [Maybe (Oid, B.ByteString, Format)]
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a)
-> IO a
withParams params action =
unsafeWithArray n oids $ \ts ->
withMany (maybeWith B.useAsCString) values $ \c_values ->
withMany (maybeWith unsafeUseParamAsCString) values $ \c_values ->
unsafeWithArray n c_values $ \vs ->
unsafeWithArray n c_lengths $ \ls ->
unsafeWithArray n formats $ \fs ->
Expand All @@ -718,20 +731,20 @@ withParams params action =
accum (Just (t,v,f)) ~(AccumParams i xs ys zs ws) =
let !z = intToCInt (B.length v)
!w = toCInt f
in AccumParams (i + 1) (t : xs) (Just v : ys) (z : zs) (w : ws)
in AccumParams (i + 1) (t : xs) (Just (v, f) : ys) (z : zs) (w : ws)

intToCInt :: Int -> CInt
intToCInt = toEnum

data AccumParams = AccumParams !Int ![Oid] ![Maybe B.ByteString] ![CInt] ![CInt]
data AccumParams = AccumParams !Int ![Oid] ![Maybe (B.ByteString, Format)] ![CInt] ![CInt]

-- | Convert a list of parameters to the format expected by libpq FFI calls,
-- prepared statement variant.
withParamsPrepared :: [Maybe (B.ByteString, Format)]
-> (CInt -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a)
-> IO a
withParamsPrepared params action =
withMany (maybeWith B.useAsCString) values $ \c_values ->
withMany (maybeWith unsafeUseParamAsCString) values $ \c_values ->
unsafeWithArray n c_values $ \vs ->
unsafeWithArray n c_lengths $ \ls ->
unsafeWithArray n formats $ \fs ->
Expand All @@ -740,16 +753,16 @@ withParamsPrepared params action =
AccumPrepParams n values c_lengths formats =
foldr accum (AccumPrepParams 0 [] [] []) params

accum :: Maybe (B.ByteString ,Format) -> AccumPrepParams -> AccumPrepParams
accum :: Maybe (B.ByteString, Format) -> AccumPrepParams -> AccumPrepParams
accum Nothing ~(AccumPrepParams i a b c) =
AccumPrepParams (i + 1) (Nothing : a) (0 : b) (0 : c)

accum (Just (v, f)) ~(AccumPrepParams i xs ys zs) =
let !y = intToCInt (B.length v)
!z = toCInt f
in AccumPrepParams (i + 1) (Just v : xs) (y : ys) (z : zs)
in AccumPrepParams (i + 1) (Just (v, f) : xs) (y : ys) (z : zs)

data AccumPrepParams = AccumPrepParams !Int ![Maybe B.ByteString] ![CInt] ![CInt]
data AccumPrepParams = AccumPrepParams !Int ![Maybe (B.ByteString, Format)] ![CInt] ![CInt]

-- | Submits a command to the server and waits for the result.
--
Expand Down