Skip to content

Commit

Permalink
Add basic criterion benches, update to 2018 version. (michaelsproul#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 authored and michaelsproul committed Nov 17, 2019
1 parent 5e4d720 commit 3cb95fd
Show file tree
Hide file tree
Showing 14 changed files with 10,426 additions and 29 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "radix_trie"
version = "0.1.5"
description = "Generic radix trie data-structure."

edition = "2018"
license = "MIT"
authors = ["Michael Sproul <[email protected]>"]

Expand All @@ -19,9 +19,17 @@ endian-type = "0.1.2"
serde = { version = "1.0", optional = true }

[dev-dependencies]
criterion = "0.3"
quickcheck = "0.4"
rand = "0.3"
serde_test = "1.0"

[[bench]]
name = "trie_benches"
harness = false

[lib]
bench = false

[badges]
travis-ci = { repository = "michaelsproul/rust_radix_trie" }
60 changes: 60 additions & 0 deletions benches/trie_benches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use criterion::{criterion_group, criterion_main, Criterion};
use radix_trie::Trie;

fn get_text() -> Vec<String> {
use std::fs::File;
use std::io::Read;
const DATA: &[&str] = &["data/1984.txt", "data/sun-rising.txt"];
let mut contents = String::new();
File::open(&DATA[1])
.unwrap()
.read_to_string(&mut contents)
.unwrap();
contents
.split(|c: char| c.is_whitespace())
.map(|s| s.to_string())
.collect()
}

fn make_trie(words: &[String]) -> Trie<&str, usize> {
let mut trie = Trie::new();
for w in words {
trie.insert(&w[..], w.len());
}
trie
}

fn trie_insert(b: &mut Criterion) {
let words = get_text();
b.bench_function("trie insert", |b| b.iter(|| make_trie(&words)));
}

fn trie_get(b: &mut Criterion) {
let words = get_text();
let trie = make_trie(&words);
b.bench_function("trie get", |b| {
b.iter(|| {
words
.iter()
.map(|w| trie.get(&&w[..]))
.collect::<Vec<Option<&usize>>>()
})
});
}

fn trie_insert_remove(b: &mut Criterion) {
let words = get_text();

b.bench_function("trie remove", |b| {
b.iter(|| {
let mut trie = make_trie(&words);
for w in &words {
trie.remove(&&w[..]);
}
});
});
}

criterion_group!(benches, trie_insert, trie_get, trie_insert_remove);

criterion_main!(benches);
10,281 changes: 10,281 additions & 0 deletions data/1984.txt

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions data/sun-rising.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
BUSY old fool, unruly Sun,
Why dost thou thus,
Through windows, and through curtains, call on us ?
Must to thy motions lovers' seasons run ?
Saucy pedantic wretch, go chide
Late school-boys and sour prentices,
Go tell court-huntsmen that the king will ride,
Call country ants to harvest offices ;
Love, all alike, no season knows nor clime,
Nor hours, days, months, which are the rags of time.

Thy beams so reverend, and strong
Why shouldst thou think ?
I could eclipse and cloud them with a wink,
But that I would not lose her sight so long.
If her eyes have not blinded thine,
Look, and to-morrow late tell me,
Whether both th' Indias of spice and mine
Be where thou left'st them, or lie here with me.
Ask for those kings whom thou saw'st yesterday,
And thou shalt hear, "All here in one bed lay."

She's all states, and all princes I ;
Nothing else is ;
Princes do but play us ; compared to this,
All honour's mimic, all wealth alchemy.
Thou, Sun, art half as happy as we,
In that the world's contracted thus ;
Thine age asks ease, and since thy duties be
To warm the world, that's done in warming us.
Shine here to us, and thou art everywhere ;
This bed thy center is, these walls thy sphere.
2 changes: 1 addition & 1 deletion examples/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fn main() {
trie.insert("not related", 1u32);
trie.insert("handle nested", 5u32);

println!("{:?}", trie);
println!("{:#?}", trie);
}
4 changes: 2 additions & 2 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::iter::{FilterMap, FromIterator, Map};
use std::slice;

use trie_node::TrieNode;
use {NibbleVec, SubTrie, Trie, TrieKey};
use crate::TrieNode;
use crate::{NibbleVec, SubTrie, Trie, TrieKey};

// MY EYES.
type Child<K, V> = Box<TrieNode<K, V>>;
Expand Down
21 changes: 20 additions & 1 deletion src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::NibbleVec;
use endian_type::{BigEndian, LittleEndian};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use NibbleVec;

/// Trait for types which can be used to key a Radix Trie.
///
Expand Down Expand Up @@ -151,6 +151,25 @@ impl TrieKey for Path {
}
}

#[cfg(windows)]
impl TrieKey for PathBuf {
fn encode_bytes(&self) -> Vec<u8> {
let str: OsString = self.clone().into();
str.into_string()
.unwrap()
.as_str()
.as_bytes()
.encode_bytes()
}
}

#[cfg(windows)]
impl TrieKey for Path {
fn encode_bytes(&self) -> Vec<u8> {
self.to_str().unwrap().as_bytes().encode_bytes()
}
}

impl<T> TrieKey for LittleEndian<T>
where
T: Eq + Copy,
Expand Down
2 changes: 1 addition & 1 deletion src/qc_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Proper testing, with QuickCheck.
use crate::{Trie, TrieCommon, TrieKey};
use quickcheck::{quickcheck, Arbitrary, Gen};
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use {Trie, TrieCommon, TrieKey};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct Key(Vec<u8>);
Expand Down
6 changes: 3 additions & 3 deletions src/subtrie.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use keys::*;
use crate::keys::*;
use crate::TrieNode;
use crate::{NibbleVec, SubTrie, SubTrieMut, SubTrieResult};
use std::borrow::Borrow;
use trie_node::TrieNode;
use {NibbleVec, SubTrie, SubTrieMut, SubTrieResult};

impl<'a, K, V> SubTrie<'a, K, V>
where
Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use keys::TrieKey;
use crate::keys::TrieKey;
use crate::{Trie, TrieCommon};
use std::collections::HashSet;
use std::iter::FromIterator;
use {Trie, TrieCommon};

const TEST_DATA: [(&'static str, u32); 7] = [
("abcdefgh", 19),
Expand Down
6 changes: 3 additions & 3 deletions src/traversal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! This module contains the core algorithms.
use keys::{match_keys, KeyMatch};
use crate::keys::{match_keys, KeyMatch};
use crate::TrieNode;
use crate::{NibbleVec, TrieKey};
use std::borrow::Borrow;
use trie_node::TrieNode;
use {NibbleVec, TrieKey};

use self::DescendantResult::*;

Expand Down
6 changes: 3 additions & 3 deletions src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::traversal::DescendantResult::*;
use crate::TrieNode;
use crate::{NibbleVec, SubTrie, SubTrieMut, Trie, TrieCommon, TrieKey};
use std::borrow::Borrow;
use traversal::DescendantResult::*;
use trie_node::TrieNode;
use {NibbleVec, SubTrie, SubTrieMut, Trie, TrieCommon, TrieKey};

impl<K, V> Trie<K, V>
where
Expand Down
6 changes: 3 additions & 3 deletions src/trie_common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iter::*;
use trie_node::TrieNode;
use {NibbleVec, SubTrie, SubTrieMut, Trie, TrieKey};
use crate::iter::*;
use crate::TrieNode;
use crate::{NibbleVec, SubTrie, SubTrieMut, Trie, TrieKey};

/// Common functionality available for tries and subtries.
pub trait TrieCommon<'a, K: 'a, V: 'a>: ContainsTrieNode<'a, K, V>
Expand Down
15 changes: 6 additions & 9 deletions src/trie_node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use keys::*;
use crate::keys::*;
use crate::{NibbleVec, SubTrie, SubTrieMut, BRANCH_FACTOR};
use std::borrow::Borrow;
use std::default::Default;
use {NibbleVec, SubTrie, SubTrieMut, BRANCH_FACTOR};

#[derive(Debug, Clone)]
pub struct TrieNode<K, V> {
Expand Down Expand Up @@ -150,10 +150,7 @@ where
/// Set the key and value of a node, given that it currently lacks one.
pub fn add_key_value(&mut self, key: K, value: V) {
debug_assert!(self.key_value.is_none());
self.key_value = Some(Box::new(KeyValue {
key: key,
value: value,
}));
self.key_value = Some(Box::new(KeyValue { key, value }));
}

/// Move the value out of a node, whilst checking that its key is as expected.
Expand Down Expand Up @@ -211,9 +208,9 @@ where
let bucket = key.get(0) as usize;
self.children[bucket] = Some(Box::new(TrieNode {
key: key,
key_value: key_value,
children: children,
child_count: child_count,
key_value,
children,
child_count,
}));
}

Expand Down

0 comments on commit 3cb95fd

Please sign in to comment.