Skip to content

Commit

Permalink
Merge pull request #39 from chris-morgan/optional-time-dependency
Browse files Browse the repository at this point in the history
Shift the time dependency to a "stats" feature
  • Loading branch information
sfackler committed Jan 7, 2015
2 parents a042422 + c1dcae6 commit cba0a95
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,3 +1,4 @@
language: rust
script:
- (cd phf && cargo test)
- (cd phf_mac && cargo test --features stats)
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,11 @@ Rust-PHF is a library to generate efficient lookup tables at compile time using

It currently uses the
[CHD algorithm](http://cmph.sourceforge.net/papers/esa09.pdf) and can generate
a 100,000 entry map in roughly .4 seconds.
a 100,000 entry map in roughly .4 seconds. By default statistics are not
produced, but if you use the `phf_mac` crate with the `stats` feature enabled
(writing `phf_mac/stats` in the `[dependencies]` section of your `Cargo.toml`
instead of `phf_mac`) and set the environment variable `PHF_STATS` it will
issue a compiler note about how long it took.

Documentation is available at https://sfackler.github.io/doc/phf

Expand Down
8 changes: 6 additions & 2 deletions phf_mac/Cargo.toml
Expand Up @@ -12,9 +12,13 @@ path = "src/lib.rs"
plugin = true
test = false

[features]
stats = ["time"]

[dependencies.phf_shared]
path = "../phf_shared"
version = "=0.4.8"

[dependencies]
time = "0.1"
[dependencies.time]
version = "0.1"
optional = true
1 change: 1 addition & 0 deletions phf_mac/src/lib.rs
Expand Up @@ -7,6 +7,7 @@

extern crate rand;
extern crate syntax;
#[cfg(feature = "stats")]
extern crate time;
extern crate rustc;
extern crate phf_shared;
Expand Down
13 changes: 8 additions & 5 deletions phf_mac/src/util.rs
Expand Up @@ -13,8 +13,6 @@ use rand::{Rng, SeedableRng, XorShiftRng};

use phf_shared::{self, PhfHash};

use time;

const DEFAULT_LAMBDA: uint = 5;

const FIXED_SEED: [u32; 4] = [3141592653, 589793238, 462643383, 2795028841];
Expand Down Expand Up @@ -86,8 +84,13 @@ pub struct HashState {
}

pub fn generate_hash(cx: &mut ExtCtxt, sp: Span, entries: &[Entry]) -> HashState {
#[cfg(feature = "stats")]
use time::precise_time_s;
#[cfg(not(feature = "stats"))]
fn precise_time_s() -> f64 { 0.0 }

let mut rng: XorShiftRng = SeedableRng::from_seed(FIXED_SEED);
let start = time::precise_time_s();
let start = precise_time_s();
let state;
loop {
match try_generate_hash(entries, &mut rng) {
Expand All @@ -98,8 +101,8 @@ pub fn generate_hash(cx: &mut ExtCtxt, sp: Span, entries: &[Entry]) -> HashState
None => {}
}
}
let time = time::precise_time_s() - start;
if os::getenv("PHF_STATS").is_some() {
let time = precise_time_s() - start;
if cfg!(feature = "stats") && os::getenv("PHF_STATS").is_some() {
cx.span_note(sp, &*format!("PHF generation took {} seconds", time));
}

Expand Down

0 comments on commit cba0a95

Please sign in to comment.