Skip to content

Commit

Permalink
Merge branch 'thread_local'
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Apr 13, 2016
2 parents 01f23d2 + cdee1e7 commit 2cda56e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- 1.3.0
- 1.6.0
- stable
- beta
- nightly
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ aho-corasick = "0.5.1"
# For skipping along search text quickly when a leading byte is known.
memchr = "0.1.9"
# For managing regex caches quickly across multiple threads.
mempool = "0.3.0"
thread_local = "0.2.0"
# For parsing regular expressions.
regex-syntax = { path = "regex-syntax", version = "0.3.1" }
# For compiling UTF-8 decoding into automata.
Expand Down
21 changes: 7 additions & 14 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;

use mempool::Pool;
use thread_local::CachedThreadLocal;
use syntax::{Expr, ExprBuilder, Literals};

use backtrack;
Expand All @@ -33,12 +33,11 @@ use set;
/// In particular, this manages the various compiled forms of a single regular
/// expression and the choice of which matching engine to use to execute a
/// regular expression.
#[derive(Debug)]
pub struct Exec {
/// All read only state.
ro: Arc<ExecReadOnly>,
/// Caches for the various matching engines.
cache: Pool<ProgramCache>,
cache: CachedThreadLocal<ProgramCache>,
}

/// ExecNoSync is like Exec, except it embeds a reference to a cache. This
Expand Down Expand Up @@ -204,8 +203,7 @@ impl ExecBuilder {
suffixes: LiteralSearcher::empty(),
match_type: MatchType::Nothing,
});
let ro_ = ro.clone();
return Ok(Exec { ro: ro, cache: create_pool(ro_) });
return Ok(Exec { ro: ro, cache: CachedThreadLocal::new() });
}
let parsed = try!(Parsed::parse(&self.res, self.only_utf8));
let mut nfa = try!(
Expand Down Expand Up @@ -245,8 +243,7 @@ impl ExecBuilder {
// println!("MATCH TYPE for '{:?}': {:?}", ro.res, ro.match_type);

let ro = Arc::new(ro);
let ro_ = ro.clone();
Ok(Exec { ro: ro, cache: create_pool(ro_) })
Ok(Exec { ro: ro, cache: CachedThreadLocal::new() })
}
}

Expand Down Expand Up @@ -767,9 +764,10 @@ impl Exec {
/// Get a searcher that isn't Sync.
#[inline(always)] // reduces constant overhead
pub fn searcher(&self) -> ExecNoSync {
let create = || Box::new(RefCell::new(ProgramCacheInner::new(&self.ro)));
ExecNoSync {
ro: &self.ro, // a clone is too expensive here! (and not needed)
cache: self.cache.get(),
cache: self.cache.get_or(create),
}
}

Expand Down Expand Up @@ -823,7 +821,7 @@ impl Clone for Exec {
fn clone(&self) -> Exec {
Exec {
ro: self.ro.clone(),
cache: create_pool(self.ro.clone()),
cache: CachedThreadLocal::new(),
}
}
}
Expand Down Expand Up @@ -934,11 +932,6 @@ pub struct ProgramCacheInner {
pub dfa_reverse: dfa::Cache,
}

/// Creates a fresh pool.
fn create_pool(ro: Arc<ExecReadOnly>) -> Pool<ProgramCache> {
Pool::new(Box::new(move || RefCell::new(ProgramCacheInner::new(&ro))))
}

impl ProgramCacheInner {
fn new(ro: &ExecReadOnly) -> Self {
ProgramCacheInner {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@

extern crate aho_corasick;
extern crate memchr;
extern crate mempool;
extern crate thread_local;
#[cfg(test)] extern crate quickcheck;
extern crate regex_syntax as syntax;
extern crate utf8_ranges;
Expand Down

0 comments on commit 2cda56e

Please sign in to comment.