Skip to content

Fixes a performance bug in bytes::Regex::replace. #210

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

Merged
merged 1 commit into from
Apr 23, 2016
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/shootout-regex-dna-bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::thread;
macro_rules! regex { ($re:expr) => { ::regex::bytes::Regex::new($re).unwrap() } }

fn main() {
let mut seq = Vec::with_capacity(50 * (1 << 20));
let mut seq = Vec::with_capacity(51 * (1 << 20));
io::stdin().read_to_end(&mut seq).unwrap();
let ilen = seq.len();

Expand Down
2 changes: 1 addition & 1 deletion examples/shootout-regex-dna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::thread;
macro_rules! regex { ($re:expr) => { ::regex::Regex::new($re).unwrap() } }

fn main() {
let mut seq = String::with_capacity(50 * (1 << 20));
let mut seq = String::with_capacity(51 * (1 << 20));
io::stdin().read_to_string(&mut seq).unwrap();
let ilen = seq.len();

Expand Down
30 changes: 23 additions & 7 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,11 @@ impl Regex {
if limit > 0 && i >= limit {
break
}
new.extend(&text[last_match..s]);
new.extend(&*rep);
extend_from_slice(&mut new, &text[last_match..s]);
extend_from_slice(&mut new, &*rep);
last_match = e;
}
new.extend(&text[last_match..]);
extend_from_slice(&mut new, &text[last_match..]);
return new;
}

Expand All @@ -463,11 +463,11 @@ impl Regex {
}
// unwrap on 0 is OK because captures only reports matches
let (s, e) = cap.pos(0).unwrap();
new.extend(&text[last_match..s]);
extend_from_slice(&mut new, &text[last_match..s]);
rep.replace_append(&cap, &mut new);
last_match = e;
}
new.extend(&text[last_match..]);
extend_from_slice(&mut new, &text[last_match..]);
new
}

Expand Down Expand Up @@ -928,7 +928,7 @@ impl<'a> Replacer for &'a [u8] {

impl<F> Replacer for F where F: FnMut(&Captures) -> Vec<u8> {
fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>) {
dst.extend((*self)(caps))
extend_from_slice(dst, &(*self)(caps));
}
}

Expand All @@ -944,10 +944,26 @@ pub struct NoExpand<'r>(pub &'r [u8]);

impl<'a> Replacer for NoExpand<'a> {
fn replace_append(&mut self, _: &Captures, dst: &mut Vec<u8>) {
dst.extend(self.0)
extend_from_slice(dst, self.0);
}

fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
Some(Cow::Borrowed(self.0))
}
}

/// This hopefully has the same performance characteristics as
/// Vec::extend_from_slice (which was introduced in Rust 1.6), but works on
/// Rust 1.3.
///
/// N.B. Remove this once we do a semver bump. At that point, we'll bump
/// required Rust version to at least 1.6.
fn extend_from_slice(dst: &mut Vec<u8>, src: &[u8]) {
dst.reserve(src.len());
let dst_len = dst.len();
unsafe { dst.set_len(dst_len + src.len()); }
let mut dst = &mut dst[dst_len..dst_len + src.len()];
for i in 0..src.len() {
dst[i] = src[i];
}
}