Skip to content

Commit 2c22ee6

Browse files
authored
feat(symbols): definition paths (#320)
1 parent e78db9f commit 2c22ee6

38 files changed

+548
-527
lines changed

src/symbols/analyzer.rs

+90-198
Large diffs are not rendered by default.

src/symbols/collections.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

3-
use std::cell::RefCell;
3+
use std::cell::UnsafeCell;
44
use std::collections::HashMap;
55

66
/// A hash map that supports inserting data while holding references to
@@ -9,8 +9,8 @@ use std::collections::HashMap;
99
/// previously inserted to key will cause a panic.
1010
pub struct AdditiveOnlyMap<K, V> {
1111
// store the values in a box to ensure the references are always stored
12-
// in the same place
13-
data: RefCell<HashMap<K, Box<V>>>,
12+
// in the same place. Uses an UnsafeCell for faster performance.
13+
data: UnsafeCell<HashMap<K, Box<V>>>,
1414
}
1515

1616
impl<K, V> Default for AdditiveOnlyMap<K, V> {
@@ -25,29 +25,32 @@ impl<K, V> AdditiveOnlyMap<K, V> {
2525
#[cfg(test)]
2626
pub fn with_capacity(capacity: usize) -> Self {
2727
Self {
28-
data: RefCell::new(HashMap::with_capacity(capacity)),
28+
data: UnsafeCell::new(HashMap::with_capacity(capacity)),
2929
}
3030
}
3131

3232
pub fn len(&self) -> usize {
33-
self.data.borrow().len()
33+
let data = unsafe { &*self.data.get() };
34+
data.len()
3435
}
3536
}
3637

3738
impl<K: Eq + std::hash::Hash, V> AdditiveOnlyMap<K, V> {
39+
pub fn contains_key(&self, key: &K) -> bool {
40+
let data = unsafe { &*self.data.get() };
41+
data.contains_key(key)
42+
}
43+
3844
pub fn insert(&self, key: K, value: V) {
45+
let data = unsafe { &mut *self.data.get() };
3946
// assert that we never replace any data
40-
assert!(self
41-
.data
42-
.borrow_mut()
43-
.insert(key, Box::new(value))
44-
.is_none());
47+
assert!(data.insert(key, Box::new(value)).is_none());
4548
}
4649

4750
pub fn get<'a>(&'a self, key: &K) -> Option<&'a V> {
48-
// this is ok because we never remove from the map
4951
unsafe {
50-
let data = self.data.borrow();
52+
let data = &*self.data.get();
53+
// this is ok because we never remove from the map
5154
data
5255
.get(key)
5356
.map(|value_box| value_box.as_ref() as *const V)
@@ -76,5 +79,7 @@ mod test {
7679
assert_eq!(data.value, 987);
7780
assert_eq!(map.get(&0).unwrap().value, 987);
7881
assert_eq!(map.get(&99).unwrap().value, 99);
82+
assert!(map.contains_key(&99));
83+
assert!(!map.contains_key(&100));
7984
}
8085
}

0 commit comments

Comments
 (0)