Skip to content

Commit 97356b0

Browse files
committed
try out sorted vec
1 parent a7c2e21 commit 97356b0

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

signal-hook-registry/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ type Action = Fn(&siginfo_t) + Send + Sync;
140140
#[derive(Clone)]
141141
struct Slot {
142142
prev: Prev,
143-
// Actions are stored and executed in the order they were registered.
143+
// We want to run the actions in the order they were inserted. A sorted map achieves this
144+
// because the ActionIds are assigned in an increasing order.
144145
actions: VecMap<ActionId, Arc<Action>>,
145146
}
146147

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
11
use std::mem;
22

3-
/// A small map backed by an unsorted vector.
4-
///
5-
/// Maintains key uniqueness at cost of O(n) lookup/insert/remove. Maintains insertion order
6-
/// (`insert` calls that overwrite an existing value don't change order).
3+
/// A small map backed by a sorted vector.
74
#[derive(Clone, Default)]
8-
pub struct VecMap<K, V>(Vec<(K, V)>);
5+
pub struct VecMap<K, V> {
6+
keys: Vec<K>,
7+
values: Vec<V>,
8+
}
99

10-
impl<K: Eq, V> VecMap<K, V> {
10+
impl<K: Ord, V> VecMap<K, V> {
1111
pub fn new() -> Self {
12-
VecMap(Vec::new())
12+
VecMap {
13+
keys: Vec::new(),
14+
values: Vec::new(),
15+
}
1316
}
1417

1518
pub fn is_empty(&self) -> bool {
16-
self.0.is_empty()
19+
self.keys.is_empty()
1720
}
1821

1922
pub fn clear(&mut self) {
20-
self.0.clear();
21-
}
22-
23-
fn find(&self, key: &K) -> Option<usize> {
24-
for (i, (k, _)) in self.0.iter().enumerate() {
25-
if k == key {
26-
return Some(i);
27-
}
28-
}
29-
None
23+
self.keys.clear();
24+
self.values.clear();
3025
}
3126

3227
pub fn contains(&self, key: &K) -> bool {
33-
self.find(key).is_some()
28+
self.keys.binary_search(key).is_ok()
3429
}
3530

3631
pub fn get(&self, key: &K) -> Option<&V> {
37-
match self.find(key) {
38-
Some(i) => Some(&self.0[i].1),
39-
None => None,
32+
match self.keys.binary_search(key) {
33+
Ok(i) => Some(&self.values[i]),
34+
Err(_) => None,
4035
}
4136
}
4237

4338
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
44-
match self.find(key) {
45-
Some(i) => Some(&mut self.0[i].1),
46-
None => None,
39+
match self.keys.binary_search(key) {
40+
Ok(i) => Some(&mut self.values[i]),
41+
Err(_) => None,
4742
}
4843
}
4944

5045
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
51-
if let Some(old) = self.get_mut(&key) {
52-
return Some(mem::replace(old, value));
46+
match self.keys.binary_search(&key) {
47+
Ok(i) => {
48+
let old = mem::replace(&mut self.values[i], value);
49+
Some(old)
50+
}
51+
Err(i) => {
52+
self.keys.insert(i, key);
53+
self.values.insert(i, value);
54+
None
55+
}
5356
}
54-
self.0.push((key, value));
55-
None
5657
}
5758

5859
pub fn remove(&mut self, key: &K) -> Option<V> {
59-
match self.find(key) {
60-
Some(i) => Some(self.0.remove(i).1),
61-
None => None,
60+
match self.keys.binary_search(key) {
61+
Ok(i) => {
62+
self.keys.remove(i);
63+
Some(self.values.remove(i))
64+
}
65+
Err(_) => None,
6266
}
6367
}
6468

6569
pub fn values(&self) -> impl Iterator<Item = &V> {
66-
self.0.iter().map(|kv| &kv.1)
70+
self.values.iter()
6771
}
6872
}

0 commit comments

Comments
 (0)