-
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathmod.rs
More file actions
147 lines (123 loc) · 3.91 KB
/
Copy pathmod.rs
File metadata and controls
147 lines (123 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use arrayvec::ArrayVec;
use itertools::Itertools;
use std::{cell::Cell, fmt};
use boa_gc::GcRefCell;
use boa_macros::{Finalize, Trace};
use crate::{
JsString, JsValue,
object::shape::{Shape, WeakShape, slot::Slot},
};
#[cfg(test)]
mod tests;
pub(crate) const PIC_CAPACITY: usize = 4;
/// A cached shape-to-slot mapping for a polymorphic inline cache.
#[derive(Clone, Debug, Trace, Finalize)]
pub(crate) struct CacheEntry {
/// A weak reference is kept to the shape to avoid the shape preventing deallocation.
pub(crate) shape: WeakShape,
#[unsafe_ignore_trace]
pub(crate) slot: Slot,
}
/// An inline cache entry for a property access.
#[derive(Clone, Debug, Trace, Finalize)]
pub(crate) struct InlineCache {
/// The property that is accessed.
pub(crate) name: JsString,
/// Multiple cached shape-to-slot entries.
pub(crate) entries: GcRefCell<ArrayVec<CacheEntry, PIC_CAPACITY>>,
/// Whether this access site has seen too many shapes and should no longer be cached.
#[unsafe_ignore_trace]
pub(crate) megamorphic: Cell<bool>,
}
impl fmt::Display for InlineCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(name:{} entries:", self.name.display_escaped())?;
if self.megamorphic.get() {
return write!(f, "(megamorphic))");
}
let entries = self.entries.borrow();
let entries = entries.iter().map(|e| e.shape.to_addr_usize()).format(", ");
write!(f, "({entries:#x}))")
}
}
impl InlineCache {
pub(crate) fn new(name: JsString) -> Self {
Self {
name,
entries: GcRefCell::new(ArrayVec::new()),
megamorphic: Cell::new(false),
}
}
pub(crate) fn set(&self, shape: &Shape, slot: Slot) {
if self.megamorphic.get() {
return;
}
let mut entries = self.entries.borrow_mut();
// Add a new entry if there's space.
if entries
.try_push(CacheEntry {
shape: shape.into(),
slot,
})
.is_err()
{
// Polymorphic cache is full, transition to megamorphic.
self.megamorphic.set(true);
entries.clear();
}
}
/// Returns the cached `(Shape, Slot)` if a matching shape exists in the inline cache.
///
/// Opportunistically cleans up stale weak shape references during lookup.
pub(crate) fn get(&self, shape: &Shape) -> Option<(Shape, Slot)> {
if self.megamorphic.get() {
return None;
}
let mut entries = self.entries.borrow_mut();
let mut i = 0;
let mut result = None;
let shape_addr = shape.to_addr_usize();
while i < entries.len() {
if let Some(upgraded) = entries[i].shape.upgrade() {
if upgraded.to_addr_usize() == shape_addr {
result = Some((upgraded, entries[i].slot));
break;
}
i += 1;
} else {
// Opportunistically clean up stale weak shapes.
entries.swap_remove(i);
}
}
result
}
}
/// A cached entry for a spread call.
#[derive(Clone, Debug, Trace, Finalize)]
pub(crate) struct CallSpreadCache {
/// The cached arguments.
pub(crate) arguments: GcRefCell<Option<(usize, Vec<JsValue>)>>,
}
impl CallSpreadCache {
/// Creates a new `CallSpreadCache`.
pub(crate) fn new() -> Self {
Self {
arguments: GcRefCell::new(None),
}
}
}
/// A cached entry for an async call.
#[derive(Clone, Debug, Trace, Finalize)]
pub(crate) struct AsyncCallCache {
#[allow(dead_code)]
#[unsafe_ignore_trace]
pub(crate) placeholder: Cell<bool>,
}
impl AsyncCallCache {
/// Creates a new `AsyncCallCache`.
pub(crate) fn new() -> Self {
Self {
placeholder: Cell::new(false),
}
}
}