@@ -31,24 +31,24 @@ use input::{Input, InputAt};
31
31
use prog:: { Program , InstPtr } ;
32
32
use re_trait:: Slot ;
33
33
34
+ type Bits = u32 ;
35
+
36
+ const BIT_SIZE : usize = 32 ;
37
+ const MAX_SIZE_BYTES : usize = 256 * ( 1 << 10 ) ; // 256 KB
38
+
34
39
/// Returns true iff the given regex and input should be executed by this
35
40
/// engine with reasonable memory usage.
36
41
pub fn should_exec ( num_insts : usize , text_len : usize ) -> bool {
37
- num_insts <= MAX_PROG_SIZE && text_len <= MAX_INPUT_SIZE
42
+ // Total memory usage in bytes is determined by:
43
+ //
44
+ // ((len(insts) * (len(input) + 1) + bits - 1) / bits) * (size_of(u32))
45
+ //
46
+ // The actual limit picked is pretty much a heuristic.
47
+ // See: https://github.com/rust-lang-nursery/regex/issues/215
48
+ let size = ( ( num_insts * ( text_len + 1 ) + BIT_SIZE - 1 ) / BIT_SIZE ) * 4 ;
49
+ size <= MAX_SIZE_BYTES
38
50
}
39
51
40
- // Total memory usage in bytes is determined by:
41
- //
42
- // ((len(insts) * (len(input) + 1) + bits - 1) / bits) * (size_of(u32))
43
- //
44
- // With the constants below, this comes out to ~1.6MB. Mostly these numbers
45
- // were picked empirically with suspicious benchmarks.
46
-
47
- type Bits = u32 ;
48
- const BIT_SIZE : usize = 32 ;
49
- const MAX_PROG_SIZE : usize = 100 ;
50
- const MAX_INPUT_SIZE : usize = 128 * ( 1 << 10 ) ;
51
-
52
52
/// A backtracking matching engine.
53
53
#[ derive( Debug ) ]
54
54
pub struct Bounded < ' a , ' m , ' r , ' s , I > {
0 commit comments