Skip to content

Commit 2fa2ad5

Browse files
committed
libcore: Implement an Equiv trait and use it on hashmaps.
7.3x speedup in string map search speed on a microbenchmark of pure hashmap searching against a constant string, due to the lack of allocations. I ran into a few snags. 1. The way the coherence check is set up, I can't implement `Equiv<@str>` and `Equiv<~str>` for `&str` simultaneously. 2. I wanted to implement `Equiv<T>` for all `T:Eq` (i.e. every type can be compared to itself if it implements `Eq`), but the coherence check didn't like that either. 3. I couldn't add this to the `Map` trait because `LinearMap` needs special handling for its `Q` type parameter: it must not only implement `Equiv<T>` but also `Hash` and `Eq`. 4. `find_equiv(&&"foo")` doesn't parse, because of the double ampersand. It has to be written `find_equiv(& &"foo")`. We can probably just fix this. Nevertheless, this is a huge win; it should address a major source of performance problems, including the one here: http://maniagnosis.crsr.net/2013/02/creating-letterpress-cheating-program.html
1 parent c407549 commit 2fa2ad5

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/libcore/cmp.rs

+8
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ pub pure fn gt<T:Ord>(v1: &T, v2: &T) -> bool {
150150
(*v1).gt(v2)
151151
}
152152

153+
/// The equivalence relation. Two values may be equivalent even if they are
154+
/// of different types. The most common use case for this relation is
155+
/// container types; e.g. it is often desirable to be able to use `&str`
156+
/// values to look up entries in a container with `~str` keys.
157+
pub trait Equiv<T> {
158+
pure fn equiv(&self, other: &T) -> bool;
159+
}
160+
153161
#[inline(always)]
154162
pub pure fn min<T:Ord>(v1: T, v2: T) -> T {
155163
if v1 < v2 { v1 } else { v2 }

src/libcore/container.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! Container traits
1212
13+
use cmp::Equiv;
1314
use option::Option;
1415

1516
pub trait Container {

src/libcore/hashmap.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// Open addressing with linear probing.
1414
pub mod linear {
1515
use container::{Container, Mutable, Map, Set};
16-
use cmp::Eq;
16+
use cmp::{Eq, Equiv};
1717
use hash::Hash;
1818
use to_bytes::IterBytes;
1919
use iter::BaseIter;
@@ -107,6 +107,15 @@ pub mod linear {
107107
self.bucket_for_key_with_hash(hash, k)
108108
}
109109

110+
#[inline(always)]
111+
pure fn bucket_for_key_equiv<Q:Hash + IterBytes + Equiv<K>>(
112+
&self,
113+
k: &Q)
114+
-> SearchResult {
115+
let hash = k.hash_keyed(self.k0, self.k1) as uint;
116+
self.bucket_for_key_with_hash_equiv(hash, k)
117+
}
118+
110119
#[inline(always)]
111120
pure fn bucket_for_key_with_hash(&self,
112121
hash: uint,
@@ -122,6 +131,24 @@ pub mod linear {
122131
TableFull
123132
}
124133

134+
#[inline(always)]
135+
pure fn bucket_for_key_with_hash_equiv<Q:Equiv<K>>(&self,
136+
hash: uint,
137+
k: &Q)
138+
-> SearchResult {
139+
let _ = for self.bucket_sequence(hash) |i| {
140+
match self.buckets[i] {
141+
Some(ref bkt) => {
142+
if bkt.hash == hash && k.equiv(&bkt.key) {
143+
return FoundEntry(i);
144+
}
145+
},
146+
None => return FoundHole(i)
147+
}
148+
};
149+
TableFull
150+
}
151+
125152
/// Expand the capacity of the array to the next power of two
126153
/// and re-insert each of the existing buckets.
127154
#[inline(always)]
@@ -450,6 +477,28 @@ pub mod linear {
450477
None => fail!(fmt!("No entry found for key: %?", k)),
451478
}
452479
}
480+
481+
/// Return true if the map contains a value for the specified key,
482+
/// using equivalence
483+
pure fn contains_key_equiv<Q:Hash + IterBytes + Equiv<K>>(
484+
&self,
485+
key: &Q)
486+
-> bool {
487+
match self.bucket_for_key_equiv(key) {
488+
FoundEntry(_) => {true}
489+
TableFull | FoundHole(_) => {false}
490+
}
491+
}
492+
493+
/// Return the value corresponding to the key in the map, using
494+
/// equivalence
495+
pure fn find_equiv<Q:Hash + IterBytes + Equiv<K>>(&self, k: &Q)
496+
-> Option<&self/V> {
497+
match self.bucket_for_key_equiv(k) {
498+
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
499+
TableFull | FoundHole(_) => None,
500+
}
501+
}
453502
}
454503

455504
impl<K:Hash + IterBytes + Eq,V:Eq> Eq for LinearMap<K, V> {

src/libcore/str.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use at_vec;
2121
use cast;
2222
use char;
23-
use cmp::{TotalOrd, Ordering, Less, Equal, Greater};
23+
use cmp::{Equiv, TotalOrd, Ordering, Less, Equal, Greater};
2424
use libc;
2525
use option::{None, Option, Some};
2626
use ptr;
@@ -898,6 +898,12 @@ impl Ord for @str {
898898
pure fn gt(&self, other: &@str) -> bool { gt((*self), (*other)) }
899899
}
900900

901+
#[cfg(notest)]
902+
impl Equiv<~str> for &str {
903+
#[inline(always)]
904+
pure fn equiv(&self, other: &~str) -> bool { eq_slice(*self, *other) }
905+
}
906+
901907
/*
902908
Section: Iterating through strings
903909
*/

src/libcore/vec.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use container::{Container, Mutable};
1616
use cast;
17-
use cmp::{Eq, Ord, TotalOrd, Ordering, Less, Equal, Greater};
17+
use cmp::{Eq, Equiv, Ord, TotalOrd, Ordering, Less, Equal, Greater};
1818
use iter::BaseIter;
1919
use iter;
2020
use kinds::Copy;
@@ -1572,6 +1572,12 @@ impl<T:Eq> Eq for @[T] {
15721572
pure fn ne(&self, other: &@[T]) -> bool { !(*self).eq(other) }
15731573
}
15741574

1575+
#[cfg(notest)]
1576+
impl<T:Eq> Equiv<~[T]> for &[T] {
1577+
#[inline(always)]
1578+
pure fn equiv(&self, other: &~[T]) -> bool { eq(*self, *other) }
1579+
}
1580+
15751581
// Lexicographical comparison
15761582

15771583
pure fn cmp<T: TotalOrd>(a: &[T], b: &[T]) -> Ordering {

0 commit comments

Comments
 (0)