Skip to content

Commit 4471212

Browse files
committed
Merge pull request #216 from rust-lang-nursery/tweak-backtrack-limit
Fix #215.
2 parents 3279286 + 553ef80 commit 4471212

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/backtrack.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ use input::{Input, InputAt};
3131
use prog::{Program, InstPtr};
3232
use re_trait::Slot;
3333

34+
type Bits = u32;
35+
36+
const BIT_SIZE: usize = 32;
37+
const MAX_SIZE_BYTES: usize = 256 * (1 << 10); // 256 KB
38+
3439
/// Returns true iff the given regex and input should be executed by this
3540
/// engine with reasonable memory usage.
3641
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
3850
}
3951

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-
5252
/// A backtracking matching engine.
5353
#[derive(Debug)]
5454
pub struct Bounded<'a, 'm, 'r, 's, I> {

0 commit comments

Comments
 (0)