From bc69f4bbcef4bbcf806d5bcf567e5fb20ebd3323 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Wed, 29 Jun 2022 12:33:16 +0200 Subject: [PATCH 1/2] withParams*: drop redundant map Instead of building a list of 'Int' in the fold, and then mapping 'toEnum' over it, build a list of 'CInt' directly. --- src/Database/PostgreSQL/LibPQ.hsc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Database/PostgreSQL/LibPQ.hsc b/src/Database/PostgreSQL/LibPQ.hsc index 3f8a00d..dc08473 100644 --- a/src/Database/PostgreSQL/LibPQ.hsc +++ b/src/Database/PostgreSQL/LibPQ.hsc @@ -759,9 +759,8 @@ withParams params action = withArray formats $ \fs -> action n ts vs ls fs where - (oids, values, lengths, formats) = + (oids, values, c_lengths, formats) = foldl' accum ([],[],[],[]) $ reverse params - !c_lengths = map toEnum lengths :: [CInt] !n = toEnum $ length params accum (!a,!b,!c,!d) Nothing = ( invalidOid:a @@ -771,7 +770,7 @@ withParams params action = ) accum (!a,!b,!c,!d) (Just (t,v,f)) = ( t:a , (Just v):b - , (B.length v):c + , (toEnum $ B.length v):c , (toEnum $ fromEnum f):d ) @@ -787,8 +786,7 @@ withParamsPrepared params action = withArray formats $ \fs -> action n vs ls fs where - (values, lengths, formats) = foldl' accum ([],[],[]) $ reverse params - !c_lengths = map toEnum lengths :: [CInt] + (values, c_lengths, formats) = foldl' accum ([],[],[]) $ reverse params !n = toEnum $ length params accum (!a,!b,!c) Nothing = ( Nothing:a @@ -796,7 +794,7 @@ withParamsPrepared params action = , 0:c ) accum (!a,!b,!c) (Just (v, f)) = ( (Just v):a - , (B.length v):b + , (toEnum $ B.length v):b , (toEnum $ fromEnum f):c ) From 325087f03bcd34237b8753e9d21a260a225146d8 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Wed, 29 Jun 2022 13:37:09 +0200 Subject: [PATCH 2/2] withParams*: drop redundant call to 'length' by using 'withArrayLen' We encode various arrays of the same length through 'withArray'. Each of these computes the length, so we might as well take the length from one of these. --- src/Database/PostgreSQL/LibPQ.hsc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Database/PostgreSQL/LibPQ.hsc b/src/Database/PostgreSQL/LibPQ.hsc index dc08473..2ab9f25 100644 --- a/src/Database/PostgreSQL/LibPQ.hsc +++ b/src/Database/PostgreSQL/LibPQ.hsc @@ -756,12 +756,11 @@ withParams params action = withMany (maybeWith B.useAsCString) values $ \c_values -> withArray c_values $ \vs -> withArray c_lengths $ \ls -> - withArray formats $ \fs -> - action n ts vs ls fs + withArrayLen formats $ \n fs -> + action (toEnum n) ts vs ls fs where (oids, values, c_lengths, formats) = foldl' accum ([],[],[],[]) $ reverse params - !n = toEnum $ length params accum (!a,!b,!c,!d) Nothing = ( invalidOid:a , Nothing:b @@ -783,11 +782,10 @@ withParamsPrepared params action = withMany (maybeWith B.useAsCString) values $ \c_values -> withArray c_values $ \vs -> withArray c_lengths $ \ls -> - withArray formats $ \fs -> - action n vs ls fs + withArrayLen formats $ \n fs -> + action (toEnum n) vs ls fs where (values, c_lengths, formats) = foldl' accum ([],[],[]) $ reverse params - !n = toEnum $ length params accum (!a,!b,!c) Nothing = ( Nothing:a , 0:b