lib: memoize toRegex[CI] (#244)

The regex helpers were converting strings to regex-tdfa regular
expressions on the fly every time, but this appears to be quite
expensive. The simplest memoisation lib seems to solve it nicely.
This commit is contained in:
Simon Michael 2015-09-26 15:39:21 -10:00
parent 591abefe9e
commit f8f2d457b9
4 changed files with 17 additions and 3 deletions

View File

@ -17,7 +17,12 @@ Easy regular expression helpers, currently based on regex-tdfa. These should:
- have simple monomorphic types
- work with strings
- work with simple strings
Regex strings are automatically compiled into regular expressions the
first time they are seen, and these are cached. If you use a huge
number of unique regular expressions this might lead to increased
memory usage.
Current limitations:
@ -42,6 +47,7 @@ where
import Data.Array
import Data.Char
import Data.List (foldl')
import Data.MemoUgly (memo)
import Text.Regex.TDFA (
Regex, CompOption(..), ExecOption(..), defaultCompOpt, defaultExecOpt,
makeRegexOpts, AllMatches(getAllMatches), match, (=~), MatchText
@ -60,10 +66,10 @@ type Replacement = String
-- | Convert our string-based regexps to real ones. Can fail if the
-- string regexp is malformed.
toRegex :: Regexp -> Regex
toRegex = makeRegexOpts compOpt execOpt
toRegex = memo (makeRegexOpts compOpt execOpt)
toRegexCI :: Regexp -> Regex
toRegexCI = makeRegexOpts compOpt{caseSensitive=False} execOpt
toRegexCI = memo (makeRegexOpts compOpt{caseSensitive=False} execOpt)
compOpt :: CompOption
compOpt = defaultCompOpt

View File

@ -68,6 +68,7 @@ library
, safe >= 0.2
, split >= 0.1 && < 0.3
, transformers >= 0.2 && < 0.5
, uglymemo
, utf8-string >= 0.3.5 && < 1.1
, HUnit
@ -145,6 +146,7 @@ test-suite tests
, safe >= 0.2
, split >= 0.1 && < 0.3
, transformers >= 0.2 && < 0.5
, uglymemo
, utf8-string >= 0.3.5 && < 1.1
, HUnit
, hledger-lib

View File

@ -66,6 +66,7 @@ dependencies:
- safe >= 0.2
- split >= 0.1 && < 0.3
- transformers >= 0.2 && < 0.5
- uglymemo
- utf8-string >= 0.3.5 && < 1.1
- HUnit
# XXX not supported

View File

@ -13,4 +13,9 @@ flags:
extra-deps:
- brick-0.2
- text-zipper-0.2.1
- uglymemo-0.1.0.1
- vty-5.3.1
ghc-options:
rtsopts:
all