diff --git a/hledger-lib/Hledger/Utils/Regex.hs b/hledger-lib/Hledger/Utils/Regex.hs index c944ae26f..3b377e28d 100644 --- a/hledger-lib/Hledger/Utils/Regex.hs +++ b/hledger-lib/Hledger/Utils/Regex.hs @@ -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 diff --git a/hledger-lib/hledger-lib.cabal b/hledger-lib/hledger-lib.cabal index 642d130f0..28fc625de 100644 --- a/hledger-lib/hledger-lib.cabal +++ b/hledger-lib/hledger-lib.cabal @@ -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 diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index edabcd668..f6d51a896 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -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 diff --git a/stack.yaml b/stack.yaml index a9d14f2e5..eb4b7e20b 100644 --- a/stack.yaml +++ b/stack.yaml @@ -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