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:
		
							parent
							
								
									591abefe9e
								
							
						
					
					
						commit
						f8f2d457b9
					
				| @ -17,7 +17,12 @@ Easy regular expression helpers, currently based on regex-tdfa. These should: | |||||||
| 
 | 
 | ||||||
| - have simple monomorphic types | - 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: | Current limitations: | ||||||
| 
 | 
 | ||||||
| @ -42,6 +47,7 @@ where | |||||||
| import Data.Array | import Data.Array | ||||||
| import Data.Char | import Data.Char | ||||||
| import Data.List (foldl') | import Data.List (foldl') | ||||||
|  | import Data.MemoUgly (memo) | ||||||
| import Text.Regex.TDFA ( | import Text.Regex.TDFA ( | ||||||
|   Regex, CompOption(..), ExecOption(..), defaultCompOpt, defaultExecOpt, |   Regex, CompOption(..), ExecOption(..), defaultCompOpt, defaultExecOpt, | ||||||
|   makeRegexOpts, AllMatches(getAllMatches), match, (=~), MatchText |   makeRegexOpts, AllMatches(getAllMatches), match, (=~), MatchText | ||||||
| @ -60,10 +66,10 @@ type Replacement = String | |||||||
| -- | Convert our string-based regexps to real ones. Can fail if the | -- | Convert our string-based regexps to real ones. Can fail if the | ||||||
| -- string regexp is malformed. | -- string regexp is malformed. | ||||||
| toRegex :: Regexp -> Regex | toRegex :: Regexp -> Regex | ||||||
| toRegex = makeRegexOpts compOpt execOpt | toRegex = memo (makeRegexOpts compOpt execOpt) | ||||||
| 
 | 
 | ||||||
| toRegexCI :: Regexp -> Regex | toRegexCI :: Regexp -> Regex | ||||||
| toRegexCI = makeRegexOpts compOpt{caseSensitive=False} execOpt | toRegexCI = memo (makeRegexOpts compOpt{caseSensitive=False} execOpt) | ||||||
| 
 | 
 | ||||||
| compOpt :: CompOption | compOpt :: CompOption | ||||||
| compOpt = defaultCompOpt | compOpt = defaultCompOpt | ||||||
|  | |||||||
| @ -68,6 +68,7 @@ library | |||||||
|     , safe >= 0.2 |     , safe >= 0.2 | ||||||
|     , split >= 0.1 && < 0.3 |     , split >= 0.1 && < 0.3 | ||||||
|     , transformers >= 0.2 && < 0.5 |     , transformers >= 0.2 && < 0.5 | ||||||
|  |     , uglymemo | ||||||
|     , utf8-string >= 0.3.5 && < 1.1 |     , utf8-string >= 0.3.5 && < 1.1 | ||||||
|     , HUnit |     , HUnit | ||||||
| 
 | 
 | ||||||
| @ -145,6 +146,7 @@ test-suite tests | |||||||
|     , safe >= 0.2 |     , safe >= 0.2 | ||||||
|     , split >= 0.1 && < 0.3 |     , split >= 0.1 && < 0.3 | ||||||
|     , transformers >= 0.2 && < 0.5 |     , transformers >= 0.2 && < 0.5 | ||||||
|  |     , uglymemo | ||||||
|     , utf8-string >= 0.3.5 && < 1.1 |     , utf8-string >= 0.3.5 && < 1.1 | ||||||
|     , HUnit |     , HUnit | ||||||
|     , hledger-lib |     , hledger-lib | ||||||
|  | |||||||
| @ -66,6 +66,7 @@ dependencies: | |||||||
|   - safe >= 0.2 |   - safe >= 0.2 | ||||||
|   - split >= 0.1 && < 0.3 |   - split >= 0.1 && < 0.3 | ||||||
|   - transformers >= 0.2 && < 0.5 |   - transformers >= 0.2 && < 0.5 | ||||||
|  |   - uglymemo | ||||||
|   - utf8-string >= 0.3.5 && < 1.1 |   - utf8-string >= 0.3.5 && < 1.1 | ||||||
|   - HUnit |   - HUnit | ||||||
| # XXX not supported | # XXX not supported | ||||||
|  | |||||||
| @ -13,4 +13,9 @@ flags: | |||||||
| extra-deps: | extra-deps: | ||||||
| - brick-0.2 | - brick-0.2 | ||||||
| - text-zipper-0.2.1 | - text-zipper-0.2.1 | ||||||
|  | - uglymemo-0.1.0.1 | ||||||
| - vty-5.3.1 | - vty-5.3.1 | ||||||
|  | 
 | ||||||
|  | ghc-options: | ||||||
|  |   rtsopts: | ||||||
|  |     all | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user