Skip to content

Commit 920ad26

Browse files
authored
Merge pull request #1 from bergey/master
use foldr in combineParts, addressing the TODO item
2 parents 861cf54 + 0812684 commit 920ad26

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

benchmarks/Benchmarks.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Benchmarks where
2+
3+
import Database.PostgreSQL.Simple.SqlQQ.Interpolated
4+
import Database.PostgreSQL.Simple.SqlQQ.Interpolated.Parser
5+
6+
import Data.List (foldl')
7+
import Criterion
8+
import Criterion.Main
9+
import Language.Haskell.TH
10+
11+
main = defaultMain
12+
[ bench "combineParts foldl' length 2" $
13+
nf combinePartsL' sql2
14+
, bench "combineParts foldl' length 100" $
15+
nf combinePartsL' sql100
16+
, bench "combineParts foldr length 2" $ nf combinePartsR sql2
17+
, bench "combineParts foldr length 100" $ nf combinePartsR sql100
18+
]
19+
20+
21+
sql2 :: [Either String Int]
22+
sql2 =
23+
[ Left "SELECT field FROM table WHERE name = "
24+
, Right 1
25+
, Left " LIMIT "
26+
, Right 2
27+
]
28+
29+
sql100 :: [Either String Int]
30+
sql100 = concat [ [Left " ", Right n ] | n <- [1..100]]
31+
32+
-- | The same as combineParts in the library, using foldl'
33+
combinePartsL' :: [Either String Int] -> (String, [Int])
34+
combinePartsL' = foldl' step ("", [])
35+
where
36+
step (s, exprs) subExpr = case subExpr of
37+
Left str -> (s <> str, exprs)
38+
Right e -> (s <> "?", exprs <> [e]) -- TODO: Make this not slow
39+
40+
combinePartsR :: [Either String Int] -> (String, [Int])
41+
combinePartsR = foldr step ("", [])
42+
where step subExpr (s, exprs) = case subExpr of
43+
Left str -> (str <> s, exprs)
44+
Right e -> ("?" <> s, e : exprs)

postgresql-simple-interpolate.cabal

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ library
2929
postgresql-simple >= 0.1,
3030
template-haskell
3131
ghc-options: -Wall -O2
32+
33+
benchmark criterion
34+
hs-source-dirs: benchmarks
35+
main-is: Benchmarks.hs
36+
ghc-options: -O2 -Wall -rtsopts
37+
build-depends: postgresql-simple-interpolate
38+
, base
39+
, criterion
40+
default-language: Haskell2010
41+
type: exitcode-stdio-1.0
42+

src/Database/PostgreSQL/Simple/SqlQQ/Interpolated.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Database.PostgreSQL.Simple.SqlQQ.Interpolated (isql, quoteInterpolatedSql
55

66
import Language.Haskell.TH (Exp, Q, appE, listE, sigE, tupE, varE)
77
import Language.Haskell.TH.Quote (QuasiQuoter (..))
8-
import Data.List (foldl')
98
import Database.PostgreSQL.Simple.ToField (Action, toField)
109
import Database.PostgreSQL.Simple.SqlQQ (sql)
1110
import Text.Parsec (ParseError)
@@ -48,12 +47,12 @@ isql = QuasiQuoter
4847
}
4948

5049
combineParts :: [StringPart] -> (String, [Q Exp])
51-
combineParts = foldl' step ("", [])
50+
combineParts = foldr step ("", [])
5251
where
53-
step (s, exprs) subExpr = case subExpr of
54-
Lit str -> (s <> str, exprs)
55-
Esc c -> (s <> [c], exprs)
56-
Anti e -> (s <> "?", exprs <> [e]) -- TODO: Make this not slow
52+
step subExpr (s, exprs) = case subExpr of
53+
Lit str -> (str <> s, exprs)
54+
Esc c -> (c : s, exprs)
55+
Anti e -> ('?' : s, e : exprs)
5756

5857
applySql :: [StringPart] -> Q Exp
5958
applySql parts =

0 commit comments

Comments
 (0)