Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #215. #216

Merged
merged 1 commit into from
Apr 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/backtrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ use input::{Input, InputAt};
use prog::{Program, InstPtr};
use re_trait::Slot;

type Bits = u32;

const BIT_SIZE: usize = 32;
const MAX_SIZE_BYTES: usize = 256 * (1 << 10); // 256 KB

/// Returns true iff the given regex and input should be executed by this
/// engine with reasonable memory usage.
pub fn should_exec(num_insts: usize, text_len: usize) -> bool {
num_insts <= MAX_PROG_SIZE && text_len <= MAX_INPUT_SIZE
// Total memory usage in bytes is determined by:
//
// ((len(insts) * (len(input) + 1) + bits - 1) / bits) * (size_of(u32))
//
// The actual limit picked is pretty much a heuristic.
// See: https://github.com/rust-lang-nursery/regex/issues/215
let size = ((num_insts * (text_len + 1) + BIT_SIZE - 1) / BIT_SIZE) * 4;
size <= MAX_SIZE_BYTES
}

// Total memory usage in bytes is determined by:
//
// ((len(insts) * (len(input) + 1) + bits - 1) / bits) * (size_of(u32))
//
// With the constants below, this comes out to ~1.6MB. Mostly these numbers
// were picked empirically with suspicious benchmarks.

type Bits = u32;
const BIT_SIZE: usize = 32;
const MAX_PROG_SIZE: usize = 100;
const MAX_INPUT_SIZE: usize = 128 * (1 << 10);

/// A backtracking matching engine.
#[derive(Debug)]
pub struct Bounded<'a, 'm, 'r, 's, I> {
Expand Down