diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-03-07 16:56:45 -0800 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-03-07 16:56:45 -0800 |
| commit | c245d9e980946d4472e9c830a109db77e1bcb038 (patch) | |
| tree | 5a29ce4c40b546c81701b48fe8d1acd1f2003022 /src/libstd | |
| parent | f0250a23d3fa2f8b4a4a4113ca89e41945a4cfed (diff) | |
| download | rust-c245d9e980946d4472e9c830a109db77e1bcb038.tar.gz rust-c245d9e980946d4472e9c830a109db77e1bcb038.zip | |
Revert "stdlib: Stop incurring vtable dispatch costs when hashmaps are used"
This reverts commit f0250a23d3fa2f8b4a4a4113ca89e41945a4cfed.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/json.rs | 3 | ||||
| -rw-r--r-- | src/libstd/map.rs | 54 | ||||
| -rw-r--r-- | src/libstd/uv.rs | 13 |
3 files changed, 33 insertions, 37 deletions
diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 26e4cc3165e..51069cf9729 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -5,7 +5,6 @@ import result::{ok, err}; import io; import io::{reader_util, writer_util}; import map; -import map::hashmap; export json; export error; @@ -37,7 +36,7 @@ enum json { /* Variant: list */ list([json]), /* Variant: dict */ - dict(map::hashmap<str,json>), + dict(map::map<str,json>), /* Variant: null */ null, } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 0511b82b5ba..c2320e93f29 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -4,10 +4,6 @@ Module: map A map type */ -import chained::hashmap; -export hashmap, hashfn, eqfn, set, map, chained, mk_hashmap, new_str_hash; -export new_bytes_hash, new_int_hash, new_uint_hash, set_add; - /* Section: Types */ /* @@ -27,13 +23,14 @@ Equality type eqfn<K> = fn@(K, K) -> bool; /* -Type: set +Type: hashset -A convenience type to treat a hashmap as a set +A convenience type to treat a map as a set */ -type set<K> = hashmap<K, ()>; +type set<K> = map<K, ()>; -type hashmap<K, V> = chained::t<K, V>; +// Temporary alias to make migration easier +type hashmap<K, V> = map<K, V>; /* IFace: map @@ -106,7 +103,8 @@ iface map<K: copy, V: copy> { } // FIXME: package this up and export it as a datatype usable for -// external code that doesn't want to pay the cost of a box. +// external code that doesn't want to pay the cost of a box and vtable +// lookups. mod chained { type entry<K, V> = { hash: uint, @@ -120,8 +118,8 @@ mod chained { absent } - type t<K, V> = @{ - mutable count: uint, + type t<K, V> = { + mutable size: uint, mutable chains: [mutable chain<K,V>], hasher: hashfn<K>, eqer: eqfn<K> @@ -187,7 +185,7 @@ mod chained { let hash = tbl.hasher(k); alt search_tbl(tbl, k, hash) { not_found { - tbl.count += 1u; + tbl.size += 1u; let idx = hash % vec::len(tbl.chains); let old_chain = tbl.chains[idx]; tbl.chains[idx] = present(@{ @@ -231,13 +229,13 @@ mod chained { } found_first(idx, entry) { - tbl.count -= 1u; + tbl.size -= 1u; tbl.chains[idx] = entry.next; ret core::option::some(entry.value); } found_after(eprev, entry) { - tbl.count -= 1u; + tbl.size -= 1u; eprev.next = entry.next; ret core::option::some(entry.value); } @@ -293,12 +291,12 @@ mod chained { } } - impl hashmap<K: copy, V: copy> of map<K, V> for t<K, V> { - fn size() -> uint { self.count } + impl <K: copy, V: copy> of map<K, V> for t<K, V> { + fn size() -> uint { self.size } fn insert(k: K, v: V) -> bool { let nchains = vec::len(self.chains); - let load = {num: (self.count + 1u) as int, den: nchains as int}; + let load = {num: (self.size + 1u) as int, den: nchains as int}; // Structural consts would be nice. This is a const 3/4 // load factor that we compare against. if !util::rational_leq(load, {num:3, den:4}) { rehash(self); } @@ -320,13 +318,13 @@ mod chained { fn values(blk: fn(V)) { items(self) { |_k, v| blk(v) } } } - fn mk<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>) -> t<K,V> { + fn mk<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>) -> map<K,V> { let initial_capacity: uint = 32u; // 2^5 - let slf: t<K, V> = @{mutable count: 0u, - mutable chains: chains(initial_capacity), - hasher: hasher, - eqer: eqer}; - slf + let slf: t<K, V> = {mutable size: 0u, + mutable chains: chains(initial_capacity), + hasher: hasher, + eqer: eqer}; + slf as map::<K, V> } } @@ -341,7 +339,7 @@ hasher - The hash function for key type K eqer - The equality function for key type K */ fn mk_hashmap<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>) - -> hashmap<K, V> { + -> map<K, V> { chained::mk(hasher, eqer) } @@ -350,7 +348,7 @@ Function: new_str_hash Construct a hashmap for string keys */ -fn new_str_hash<V: copy>() -> hashmap<str, V> { +fn new_str_hash<V: copy>() -> map<str, V> { ret mk_hashmap(str::hash, str::eq); } @@ -359,7 +357,7 @@ Function: new_bytes_hash Construct a hashmap for byte string keys */ -fn new_bytes_hash<V: copy>() -> hashmap<[u8], V> { +fn new_bytes_hash<V: copy>() -> map<[u8], V> { ret mk_hashmap(vec::u8::hash, vec::u8::eq); } @@ -368,7 +366,7 @@ Function: new_int_hash Construct a hashmap for int keys */ -fn new_int_hash<V: copy>() -> hashmap<int, V> { +fn new_int_hash<V: copy>() -> map<int, V> { fn hash_int(&&x: int) -> uint { int::hash(x) } fn eq_int(&&a: int, &&b: int) -> bool { ret a == b; } ret mk_hashmap(hash_int, eq_int); @@ -379,7 +377,7 @@ Function: new_uint_hash Construct a hashmap for uint keys */ -fn new_uint_hash<V: copy>() -> hashmap<uint, V> { +fn new_uint_hash<V: copy>() -> map<uint, V> { fn hash_uint(&&x: uint) -> uint { uint::hash(x) } fn eq_uint(&&a: uint, &&b: uint) -> bool { ret a == b; } ret mk_hashmap(hash_uint, eq_uint); diff --git a/src/libstd/uv.rs b/src/libstd/uv.rs index 9991d68b295..875656d3a22 100644 --- a/src/libstd/uv.rs +++ b/src/libstd/uv.rs @@ -1,4 +1,3 @@ -import map::hashmap; export loop_new, loop_delete, run, close, run_in_bg; export async_init, async_send; export timer_init, timer_start, timer_stop; @@ -130,17 +129,17 @@ fn loop_new() -> uv_loop unsafe { process_operation); // all state goes here - let handles: map::hashmap<[u8], *ctypes::void> = + let handles: map::map<[u8], *ctypes::void> = map::new_bytes_hash(); - let id_to_handle: map::hashmap<[u8], uv_handle> = + let id_to_handle: map::map<[u8], uv_handle> = map::new_bytes_hash(); - let after_cbs: map::hashmap<[u8], fn~(uv_handle)> = + let after_cbs: map::map<[u8], fn~(uv_handle)> = map::new_bytes_hash(); - let close_callbacks: map::hashmap<[u8], fn~()> = + let close_callbacks: map::map<[u8], fn~()> = map::new_bytes_hash(); - let async_cbs: map::hashmap<[u8], fn~(uv_handle)> = + let async_cbs: map::map<[u8], fn~(uv_handle)> = map::new_bytes_hash(); - let timer_cbs: map::hashmap<[u8], fn~(uv_handle)> = + let timer_cbs: map::map<[u8], fn~(uv_handle)> = map::new_bytes_hash(); // the main loop that this task blocks on. |
