implement a map testing benchmark
This involved some other changes: - add a managed<T> wrapper that makes send_map usable from @-data - implement map<K,V> for managed<send_map> Unit tests are coming.
This commit is contained in:
@@ -55,6 +55,7 @@ export hash;
|
|||||||
export cmp;
|
export cmp;
|
||||||
export num;
|
export num;
|
||||||
export path;
|
export path;
|
||||||
|
export managed;
|
||||||
|
|
||||||
// NDM seems to be necessary for resolve to work
|
// NDM seems to be necessary for resolve to work
|
||||||
export option_iter;
|
export option_iter;
|
||||||
@@ -261,6 +262,7 @@ mod sys;
|
|||||||
#[warn(non_camel_case_types)]
|
#[warn(non_camel_case_types)]
|
||||||
mod unsafe;
|
mod unsafe;
|
||||||
|
|
||||||
|
mod managed;
|
||||||
|
|
||||||
// Modules supporting compiler-generated code
|
// Modules supporting compiler-generated code
|
||||||
// Exported but not part of the public interface
|
// Exported but not part of the public interface
|
||||||
|
|||||||
62
src/libcore/managed.rs
Normal file
62
src/libcore/managed.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*!
|
||||||
|
|
||||||
|
Module for wrapping freezable data structures in managed boxes.
|
||||||
|
Normally freezable data structures require an unaliased reference,
|
||||||
|
such as `T` or `~T`, so that the compiler can track when they are
|
||||||
|
being mutated. The `rw<T>` type converts these static checks into
|
||||||
|
dynamic checks: your program will fail if you attempt to perform
|
||||||
|
mutation when the data structure should be immutable.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[forbid(non_camel_case_types)];
|
||||||
|
#[forbid(deprecated_mode)];
|
||||||
|
#[forbid(deprecated_pattern)];
|
||||||
|
|
||||||
|
import util::with;
|
||||||
|
import unsafe::transmute_immut;
|
||||||
|
|
||||||
|
export Managed;
|
||||||
|
|
||||||
|
enum Mode { ReadOnly, Mutable, Immutable }
|
||||||
|
|
||||||
|
struct Data<T> {
|
||||||
|
mut value: T;
|
||||||
|
mut mode: Mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Managed<T> = @Data<T>;
|
||||||
|
|
||||||
|
fn Managed<T>(+t: T) -> Managed<T> {
|
||||||
|
@Data {value: t, mode: ReadOnly}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Data<T> {
|
||||||
|
fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
|
||||||
|
match self.mode {
|
||||||
|
Immutable => fail fmt!("%? currently immutable",
|
||||||
|
self.value),
|
||||||
|
ReadOnly | Mutable => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
do with(&mut self.mode, Mutable) {
|
||||||
|
op(&mut self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn borrow_const<R>(op: &fn(t: &const T) -> R) -> R {
|
||||||
|
op(&const self.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn borrow_imm<R>(op: &fn(t: &T) -> R) -> R {
|
||||||
|
match self.mode {
|
||||||
|
Mutable => fail fmt!("%? currently mutable",
|
||||||
|
self.value),
|
||||||
|
ReadOnly | Immutable => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
do with(&mut self.mode, Immutable) {
|
||||||
|
op(unsafe{transmute_immut(&mut self.value)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -221,6 +221,13 @@ mod linear {
|
|||||||
self.size -= 1;
|
self.size -= 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clear() {
|
||||||
|
for uint::range(0, self.buckets.len()) |idx| {
|
||||||
|
self.buckets[idx] = none;
|
||||||
|
}
|
||||||
|
self.size = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
priv impl<K,V> &LinearMap<K,V> {
|
priv impl<K,V> &LinearMap<K,V> {
|
||||||
|
|||||||
@@ -12,6 +12,24 @@ pure fn id<T>(+x: T) -> T { x }
|
|||||||
/// Ignores a value.
|
/// Ignores a value.
|
||||||
pure fn ignore<T>(+_x: T) { }
|
pure fn ignore<T>(+_x: T) { }
|
||||||
|
|
||||||
|
/// Sets `*ptr` to `new_value`, invokes `op()`, and then restores the
|
||||||
|
/// original value of `*ptr`.
|
||||||
|
#[inline(always)]
|
||||||
|
fn with<T: copy, R>(
|
||||||
|
ptr: &mut T,
|
||||||
|
+new_value: T,
|
||||||
|
op: &fn() -> R) -> R
|
||||||
|
{
|
||||||
|
// NDM: if swap operator were defined somewhat differently,
|
||||||
|
// we wouldn't need to copy...
|
||||||
|
|
||||||
|
let old_value = *ptr;
|
||||||
|
*ptr = move new_value;
|
||||||
|
let result = op();
|
||||||
|
*ptr = move old_value;
|
||||||
|
return move result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Swap the values at two mutable locations of the same type, without
|
* Swap the values at two mutable locations of the same type, without
|
||||||
* deinitialising or copying either one.
|
* deinitialising or copying either one.
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
import io::WriterUtil;
|
import io::WriterUtil;
|
||||||
import to_str::ToStr;
|
import to_str::ToStr;
|
||||||
|
import managed::Managed;
|
||||||
|
import send_map::linear::LinearMap;
|
||||||
|
|
||||||
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
|
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
|
||||||
export box_str_hash;
|
export box_str_hash;
|
||||||
export bytes_hash, int_hash, uint_hash, set_add;
|
export bytes_hash, int_hash, uint_hash, set_add;
|
||||||
@@ -59,10 +62,10 @@ trait map<K: copy, V: copy> {
|
|||||||
fn find(+key: K) -> option<V>;
|
fn find(+key: K) -> option<V>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove and return a value from the map. If the key does not exist
|
* Remove and return a value from the map. Returns true if the
|
||||||
* in the map then returns none.
|
* key was present in the map, otherwise false.
|
||||||
*/
|
*/
|
||||||
fn remove(+key: K) -> option<V>;
|
fn remove(+key: K) -> bool;
|
||||||
|
|
||||||
/// Clear the map, removing all key/value pairs.
|
/// Clear the map, removing all key/value pairs.
|
||||||
fn clear();
|
fn clear();
|
||||||
@@ -279,18 +282,18 @@ mod chained {
|
|||||||
option::unwrap(opt_v)
|
option::unwrap(opt_v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(+k: K) -> option<V> {
|
fn remove(+k: K) -> bool {
|
||||||
match self.search_tbl(&k, self.hasher(&k)) {
|
match self.search_tbl(&k, self.hasher(&k)) {
|
||||||
not_found => none,
|
not_found => false,
|
||||||
found_first(idx, entry) => {
|
found_first(idx, entry) => {
|
||||||
self.count -= 1u;
|
self.count -= 1u;
|
||||||
self.chains[idx] = entry.next;
|
self.chains[idx] = entry.next;
|
||||||
some(entry.value)
|
true
|
||||||
}
|
}
|
||||||
found_after(eprev, entry) => {
|
found_after(eprev, entry) => {
|
||||||
self.count -= 1u;
|
self.count -= 1u;
|
||||||
eprev.next = entry.next;
|
eprev.next = entry.next;
|
||||||
some(entry.value)
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -468,6 +471,93 @@ fn hash_from_uints<V: copy>(items: &[(uint, V)]) -> hashmap<uint, V> {
|
|||||||
hash_from_vec(uint::hash, uint::eq, items)
|
hash_from_vec(uint::hash, uint::eq, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XXX Transitionary
|
||||||
|
impl<K: copy, V: copy> Managed<LinearMap<K, V>>: map<K, V> {
|
||||||
|
fn size() -> uint {
|
||||||
|
do self.borrow_const |p| {
|
||||||
|
p.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert(+key: K, +value: V) -> bool {
|
||||||
|
do self.borrow_mut |p| {
|
||||||
|
p.insert(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contains_key(+key: K) -> bool {
|
||||||
|
do self.borrow_const |p| {
|
||||||
|
p.contains_key(&key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contains_key_ref(key: &K) -> bool {
|
||||||
|
do self.borrow_const |p| {
|
||||||
|
p.contains_key(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(+key: K) -> V {
|
||||||
|
do self.borrow_const |p| {
|
||||||
|
p.get(&key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find(+key: K) -> option<V> {
|
||||||
|
do self.borrow_const |p| {
|
||||||
|
p.find(&key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove(+key: K) -> bool {
|
||||||
|
do self.borrow_mut |p| {
|
||||||
|
p.remove(&key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear() {
|
||||||
|
do self.borrow_mut |p| {
|
||||||
|
p.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each(op: fn(+key: K, +value: V) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each_key(op: fn(+key: K) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each_key(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each_value(op: fn(+value: V) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each_value(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each_ref(op: fn(key: &K, value: &V) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each_ref(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each_key_ref(op: fn(key: &K) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each_key_ref(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn each_value_ref(op: fn(value: &V) -> bool) {
|
||||||
|
do self.borrow_imm |p| {
|
||||||
|
p.each_value_ref(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
|
|||||||
@@ -80,13 +80,13 @@ impl<V: copy> smallintmap<V>: map::map<uint, V> {
|
|||||||
insert(self, key, value);
|
insert(self, key, value);
|
||||||
return !exists;
|
return !exists;
|
||||||
}
|
}
|
||||||
fn remove(+key: uint) -> option<V> {
|
fn remove(+key: uint) -> bool {
|
||||||
if key >= self.v.len() {
|
if key >= self.v.len() {
|
||||||
return none;
|
return false;
|
||||||
}
|
}
|
||||||
let old = self.v.get_elt(key);
|
let old = self.v.get_elt(key);
|
||||||
self.v.set_elt(key, none);
|
self.v.set_elt(key, none);
|
||||||
old
|
old.is_some()
|
||||||
}
|
}
|
||||||
fn clear() {
|
fn clear() {
|
||||||
self.v.set(~[mut]);
|
self.v.set(~[mut]);
|
||||||
|
|||||||
178
src/test/bench/core-map.rs
Normal file
178
src/test/bench/core-map.rs
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std;
|
||||||
|
import rand;
|
||||||
|
import std::map;
|
||||||
|
import managed::Managed;
|
||||||
|
import send_map::linear::*;
|
||||||
|
import io::WriterUtil;
|
||||||
|
|
||||||
|
struct Results {
|
||||||
|
sequential_ints: float;
|
||||||
|
random_ints: float;
|
||||||
|
delete_ints: float;
|
||||||
|
|
||||||
|
sequential_strings: float;
|
||||||
|
random_strings: float;
|
||||||
|
delete_strings: float;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timed(result: &mut float,
|
||||||
|
op: fn()) {
|
||||||
|
let start = std::time::precise_time_s();
|
||||||
|
op();
|
||||||
|
let end = std::time::precise_time_s();
|
||||||
|
*result = (end - start);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn int_benchmarks<M: map::map<uint, uint>>(make_map: fn() -> M,
|
||||||
|
rng: @rand::Rng,
|
||||||
|
num_keys: uint,
|
||||||
|
results: &mut Results) {
|
||||||
|
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
do timed(&mut results.sequential_ints) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
map.insert(i, i+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
assert map.get(i) == i+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
do timed(&mut results.random_ints) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
map.insert(rng.next() as uint, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
map.insert(i, i);;
|
||||||
|
}
|
||||||
|
|
||||||
|
do timed(&mut results.delete_ints) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
assert map.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn str_benchmarks<M: map::map<~str, uint>>(make_map: fn() -> M,
|
||||||
|
rng: @rand::Rng,
|
||||||
|
num_keys: uint,
|
||||||
|
results: &mut Results) {
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
do timed(&mut results.sequential_strings) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
let s = uint::to_str(i, 10);
|
||||||
|
map.insert(s, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
let s = uint::to_str(i, 10);
|
||||||
|
assert map.get(s) == i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
do timed(&mut results.random_strings) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
let s = uint::to_str(rng.next() as uint, 10);
|
||||||
|
map.insert(s, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let map = make_map();
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
map.insert(uint::to_str(i, 10), i);
|
||||||
|
}
|
||||||
|
do timed(&mut results.delete_strings) {
|
||||||
|
for uint::range(0, num_keys) |i| {
|
||||||
|
assert map.remove(uint::to_str(i, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_header(header: &str) {
|
||||||
|
io::stdout().write_str(header);
|
||||||
|
io::stdout().write_str("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_row(label: &str, value: float) {
|
||||||
|
io::stdout().write_str(fmt!("%30s %f s\n", label, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
sequential_ints: 0f,
|
||||||
|
random_ints: 0f,
|
||||||
|
delete_ints: 0f,
|
||||||
|
|
||||||
|
sequential_strings: 0f,
|
||||||
|
random_strings: 0f,
|
||||||
|
delete_strings: 0f,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(args: ~[~str]) {
|
||||||
|
let num_keys = {
|
||||||
|
if args.len() == 2 {
|
||||||
|
uint::from_str(args[1]).get()
|
||||||
|
} else {
|
||||||
|
100 // woefully inadequate for any real measurement
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let seed = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||||
|
|
||||||
|
{
|
||||||
|
let rng = rand::seeded_rng(copy seed);
|
||||||
|
let mut results = empty_results();
|
||||||
|
int_benchmarks::<map::hashmap<uint, uint>>(
|
||||||
|
map::uint_hash, rng, num_keys, &mut results);
|
||||||
|
str_benchmarks::<map::hashmap<~str, uint>>(
|
||||||
|
map::str_hash, rng, num_keys, &mut results);
|
||||||
|
write_results("libstd::map::hashmap", &results);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let rng = rand::seeded_rng(copy seed);
|
||||||
|
let mut results = empty_results();
|
||||||
|
int_benchmarks::<Managed<LinearMap<uint, uint>>>(
|
||||||
|
|| Managed(linear_map(uint::hash, uint::eq)),
|
||||||
|
rng, num_keys, &mut results);
|
||||||
|
str_benchmarks::<Managed<LinearMap<~str, uint>>>(
|
||||||
|
|| Managed(linear_map(str::hash, str::eq)),
|
||||||
|
rng, num_keys, &mut results);
|
||||||
|
write_results("libstd::map::hashmap", &results);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user