2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-02-18 00:24:14 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-10 19:39:08 -05:00
|
|
|
// ignore-pretty very bad with line comments
|
|
|
|
|
2014-12-08 07:35:34 -06:00
|
|
|
#![feature(unboxed_closures)]
|
|
|
|
|
2014-02-14 12:10:06 -06:00
|
|
|
extern crate collections;
|
2014-03-01 18:33:24 -06:00
|
|
|
extern crate rand;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::collections::BTreeSet;
|
2014-10-30 20:25:08 -05:00
|
|
|
use std::collections::BitvSet;
|
2014-11-10 14:27:56 -06:00
|
|
|
use std::collections::HashSet;
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
use std::collections::hash_map::Hasher;
|
2014-10-30 15:43:24 -05:00
|
|
|
use std::hash::Hash;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::os;
|
2014-11-10 14:27:56 -06:00
|
|
|
use std::time::Duration;
|
2013-02-18 00:24:14 -06:00
|
|
|
|
|
|
|
struct Results {
|
2014-11-10 14:27:56 -06:00
|
|
|
sequential_ints: Duration,
|
|
|
|
random_ints: Duration,
|
|
|
|
delete_ints: Duration,
|
2013-02-18 00:24:14 -06:00
|
|
|
|
2014-11-10 14:27:56 -06:00
|
|
|
sequential_strings: Duration,
|
|
|
|
random_strings: Duration,
|
|
|
|
delete_strings: Duration,
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2014-12-08 07:35:34 -06:00
|
|
|
fn timed<F>(result: &mut Duration, op: F) where F: FnOnce() {
|
2014-11-10 14:27:56 -06:00
|
|
|
*result = Duration::span(op);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2014-10-30 15:43:24 -05:00
|
|
|
trait MutableSet<T> {
|
|
|
|
fn insert(&mut self, k: T);
|
|
|
|
fn remove(&mut self, k: &T) -> bool;
|
|
|
|
fn contains(&self, k: &T) -> bool;
|
|
|
|
}
|
|
|
|
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
impl<T: Hash<Hasher> + Eq> MutableSet<T> for HashSet<T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
fn insert(&mut self, k: T) { self.insert(k); }
|
|
|
|
fn remove(&mut self, k: &T) -> bool { self.remove(k) }
|
|
|
|
fn contains(&self, k: &T) -> bool { self.contains(k) }
|
|
|
|
}
|
2014-12-16 22:09:16 -06:00
|
|
|
impl<T: Ord> MutableSet<T> for BTreeSet<T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
fn insert(&mut self, k: T) { self.insert(k); }
|
|
|
|
fn remove(&mut self, k: &T) -> bool { self.remove(k) }
|
|
|
|
fn contains(&self, k: &T) -> bool { self.contains(k) }
|
|
|
|
}
|
|
|
|
impl MutableSet<uint> for BitvSet {
|
|
|
|
fn insert(&mut self, k: uint) { self.insert(k); }
|
|
|
|
fn remove(&mut self, k: &uint) -> bool { self.remove(k) }
|
|
|
|
fn contains(&self, k: &uint) -> bool { self.contains(k) }
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Results {
|
2013-07-13 22:30:05 -05:00
|
|
|
pub fn bench_int<T:MutableSet<uint>,
|
2015-01-05 07:23:55 -06:00
|
|
|
R:rand::Rng,
|
|
|
|
F:FnMut() -> T>(
|
2013-11-19 18:34:19 -06:00
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
|
|
|
num_keys: uint,
|
|
|
|
rand_cap: uint,
|
2015-01-05 07:23:55 -06:00
|
|
|
mut f: F) {
|
|
|
|
{
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut set = f();
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.sequential_ints, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(i);
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(set.contains(&i));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.random_ints, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0..num_keys {
|
2013-09-21 07:06:50 -05:00
|
|
|
set.insert(rng.gen::<uint>() % rand_cap);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(i);
|
|
|
|
}
|
|
|
|
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.delete_ints, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(set.remove(&i));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn bench_str<T:MutableSet<String>,
|
2015-01-05 07:23:55 -06:00
|
|
|
R:rand::Rng,
|
|
|
|
F:FnMut() -> T>(
|
2013-11-19 18:34:19 -06:00
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
|
|
|
num_keys: uint,
|
2015-01-05 07:23:55 -06:00
|
|
|
mut f: F) {
|
2013-02-18 00:24:14 -06:00
|
|
|
{
|
|
|
|
let mut set = f();
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.sequential_strings, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2014-06-21 05:39:03 -05:00
|
|
|
set.insert(i.to_string());
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2014-06-21 05:39:03 -05:00
|
|
|
assert!(set.contains(&i.to_string()));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.random_strings, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0..num_keys {
|
2014-06-21 05:39:03 -05:00
|
|
|
let s = rng.gen::<uint>().to_string();
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(s);
|
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2014-06-21 05:39:03 -05:00
|
|
|
set.insert(i.to_string());
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2014-12-13 10:08:27 -06:00
|
|
|
timed(&mut self.delete_strings, || {
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..num_keys {
|
2014-06-21 05:39:03 -05:00
|
|
|
assert!(set.remove(&i.to_string()));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_header(header: &str) {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("{}", header);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2014-11-10 14:27:56 -06:00
|
|
|
fn write_row(label: &str, value: Duration) {
|
2014-11-17 13:29:38 -06:00
|
|
|
println!("{:30} {} s\n", label, value);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_results(label: &str, results: &Results) {
|
|
|
|
write_header(label);
|
|
|
|
write_row("sequential_ints", results.sequential_ints);
|
|
|
|
write_row("random_ints", results.random_ints);
|
|
|
|
write_row("delete_ints", results.delete_ints);
|
|
|
|
write_row("sequential_strings", results.sequential_strings);
|
|
|
|
write_row("random_strings", results.random_strings);
|
|
|
|
write_row("delete_strings", results.delete_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_results() -> Results {
|
|
|
|
Results {
|
2014-11-10 14:27:56 -06:00
|
|
|
sequential_ints: Duration::seconds(0),
|
|
|
|
random_ints: Duration::seconds(0),
|
|
|
|
delete_ints: Duration::seconds(0),
|
2013-02-18 00:24:14 -06:00
|
|
|
|
2014-11-10 14:27:56 -06:00
|
|
|
sequential_strings: Duration::seconds(0),
|
|
|
|
random_strings: Duration::seconds(0),
|
|
|
|
delete_strings: Duration::seconds(0),
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2014-05-05 02:29:59 -05:00
|
|
|
let args = args.as_slice();
|
2013-02-18 00:24:14 -06:00
|
|
|
let num_keys = {
|
|
|
|
if args.len() == 2 {
|
2015-01-02 01:53:35 -06:00
|
|
|
args[1].parse::<uint>().unwrap()
|
2013-02-18 00:24:14 -06:00
|
|
|
} else {
|
|
|
|
100 // woefully inadequate for any real measurement
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2013-02-18 00:24:14 -06:00
|
|
|
let max = 200000;
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-08-27 20:45:13 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || {
|
|
|
|
let s: HashSet<uint> = HashSet::new();
|
|
|
|
s
|
|
|
|
});
|
|
|
|
results.bench_str(&mut rng, num_keys, || {
|
2014-05-22 18:57:53 -05:00
|
|
|
let s: HashSet<String> = HashSet::new();
|
2013-08-27 20:45:13 -05:00
|
|
|
s
|
|
|
|
});
|
2014-02-19 21:29:58 -06:00
|
|
|
write_results("collections::HashSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-08-27 20:45:13 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || {
|
2014-12-16 22:09:16 -06:00
|
|
|
let s: BTreeSet<uint> = BTreeSet::new();
|
2013-08-27 20:45:13 -05:00
|
|
|
s
|
|
|
|
});
|
|
|
|
results.bench_str(&mut rng, num_keys, || {
|
2014-12-16 22:09:16 -06:00
|
|
|
let s: BTreeSet<String> = BTreeSet::new();
|
2013-08-27 20:45:13 -05:00
|
|
|
s
|
|
|
|
});
|
2014-12-16 22:09:16 -06:00
|
|
|
write_results("collections::BTreeSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-05-06 21:29:04 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || BitvSet::new());
|
2014-02-19 21:08:12 -06:00
|
|
|
write_results("collections::bitv::BitvSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|