Skip to content

Commit 601f904

Browse files
hearnadamkommander
authored andcommitted
perf(rust): a run reads the image's built-ins in place until it writes one
Of the corpus's 16 programs, 14 never write to a built-in object; the two that do bring in a lazy group. A run now reads the image's objects themselves (Base::Shared, the heap's first ids) and their property lists (Items::Shared). The first write to a built-in copies them (Heap::own_base, Items::own); the run's own objects are in the chunks after them. examples/corpus: setup 104k to 56k instructions, return 1 115k to 66k. Idle waiting run: 82.1 to 51.3 KB. Cost: a branch on every object access, 1 to 3% more instructions on the bench programs. A program that writes to a built-in pays the copy then instead of in its setup, about even.
1 parent edc9be2 commit 601f904

3 files changed

Lines changed: 161 additions & 30 deletions

File tree

‎rust/crates/psychoscript/src/builtins/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) fn image() -> &'static Image {
6161
vm.setup();
6262
Image {
6363
atoms: std::mem::replace(&mut vm.atoms, crate::strings::Strings::new()),
64-
heap: vm.heap.clone_base(),
64+
heap: std::mem::replace(&mut vm.heap, crate::collector::Heap::with_capacity(0)),
6565
realm: vm.realm.clone(),
6666
names: vm.names.clone(),
6767
global_map: std::mem::take(&mut vm.global_map),

‎rust/crates/psychoscript/src/collector.rs‎

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,30 @@ pub struct Heap {
123123
/// half of it unused, and macOS does not reuse a large block `realloc` grew once it is freed: a process running
124124
/// one large program after another kept every heap it ever grew.
125125
chunks: Vec<Vec<Obj>>,
126+
/// The fresh runtime's objects, which have the first ids: the image's own, read in place, until the run writes
127+
/// one, which copies them all (`own_base`). Most runs never do. Object `i` past them is at `i - base.len()` in
128+
/// the chunks. Empty in a heap made from scratch, as the image's is.
129+
base: Base,
126130
len: usize,
127131
pub(crate) marking: bool,
128132
marks: Marks,
129133
}
130134

135+
enum Base {
136+
Shared(&'static [Obj]),
137+
Own(Vec<Obj>),
138+
}
139+
140+
impl Base {
141+
#[inline(always)]
142+
fn get(&self) -> &[Obj] {
143+
match self {
144+
Base::Shared(objs) => objs,
145+
Base::Own(objs) => objs,
146+
}
147+
}
148+
}
149+
131150
#[derive(Default)]
132151
struct Marks {
133152
color: Vec<u8>,
@@ -142,22 +161,36 @@ impl Heap {
142161
pub fn with_capacity(n: usize) -> Heap {
143162
Heap {
144163
chunks: vec![Vec::with_capacity(n.min(HEAP_CHUNK))],
164+
base: Base::Own(Vec::new()),
145165
len: 0,
146166
marking: false,
147167
marks: Marks::default(),
148168
}
149169
}
150170

151-
/// A copy of a fresh runtime's heap, which holds only what `Obj::clone_base` copies, with room for a small
152-
/// program's own objects.
153-
pub(crate) fn clone_base(&self) -> Heap {
154-
let mut heap = Heap::with_capacity(self.len + 34);
155-
for o in self.iter() {
156-
heap.push(o.clone_base());
157-
}
171+
/// A run's heap over the image's, whose objects it reads in place, with room for a small program's own.
172+
pub(crate) fn clone_base(&'static self) -> Heap {
173+
let mut heap = Heap::with_capacity(34);
174+
// The image's heap is made from scratch, so all of it is in its first chunk.
175+
let objs = &self.chunks[0][..];
176+
heap.base = Base::Shared(objs);
177+
heap.len = objs.len();
158178
heap
159179
}
160180

181+
/// The fresh runtime's objects as the run's own, copied from the image's at the first write to one.
182+
#[cold]
183+
#[inline(never)]
184+
fn own_base(&mut self) -> &mut Vec<Obj> {
185+
if let Base::Shared(objs) = self.base {
186+
self.base = Base::Own(objs.iter().map(Obj::clone_base).collect());
187+
}
188+
match &mut self.base {
189+
Base::Own(objs) => objs,
190+
Base::Shared(_) => unreachable!("made its own above"),
191+
}
192+
}
193+
161194
/// The bytes of storage object `i` (`Kind::Storage`), past the barrier: bytes hold no references, so writing them
162195
/// changes nothing marking must see.
163196
#[inline(always)]
@@ -178,11 +211,28 @@ impl Heap {
178211

179212
/// The objects in id order.
180213
pub fn iter(&self) -> impl Iterator<Item = &Obj> {
181-
self.chunks.iter().flatten()
214+
self.base.get().iter().chain(self.chunks.iter().flatten())
215+
}
216+
217+
#[inline(always)]
218+
fn slot(&self, i: usize) -> &Obj {
219+
let base = self.base.get();
220+
match base.get(i) {
221+
Some(o) => o,
222+
None => {
223+
let i = i - base.len();
224+
&self.chunks[i >> HEAP_CHUNK_BITS][i & HEAP_CHUNK_MASK]
225+
}
226+
}
182227
}
183228

184229
#[cfg_attr(not(debug_assertions), inline(always))]
185230
fn slot_mut(&mut self, i: usize) -> &mut Obj {
231+
let base = self.base.get().len();
232+
if i < base {
233+
return &mut self.own_base()[i];
234+
}
235+
let i = i - base;
186236
&mut self.chunks[i >> HEAP_CHUNK_BITS][i & HEAP_CHUNK_MASK]
187237
}
188238

@@ -232,18 +282,25 @@ impl Heap {
232282
}
233283
m.color[i] = BLACK;
234284
// Objects are greyed here; strings, cells and atoms wait for the next step.
235-
crate::reach::children(
236-
&self.chunks[i >> HEAP_CHUNK_BITS][i & HEAP_CHUNK_MASK],
237-
&mut |r| match r {
238-
Ref::Obj(id) => {
239-
if m.color.get(id as usize) == Some(&WHITE) {
240-
m.color[id as usize] = GREY;
241-
m.grey.push(id);
242-
}
285+
let o = {
286+
let base = self.base.get();
287+
match base.get(i) {
288+
Some(o) => o,
289+
None => {
290+
let i = i - base.len();
291+
&self.chunks[i >> HEAP_CHUNK_BITS][i & HEAP_CHUNK_MASK]
243292
}
244-
_ => m.pending.push(r),
245-
},
246-
);
293+
}
294+
};
295+
crate::reach::children(o, &mut |r| match r {
296+
Ref::Obj(id) => {
297+
if m.color.get(id as usize) == Some(&WHITE) {
298+
m.color[id as usize] = GREY;
299+
m.grey.push(id);
300+
}
301+
}
302+
_ => m.pending.push(r),
303+
});
247304
}
248305
}
249306

@@ -252,7 +309,7 @@ impl Index<usize> for Heap {
252309

253310
#[cfg_attr(not(debug_assertions), inline(always))]
254311
fn index(&self, i: usize) -> &Obj {
255-
&self.chunks[i >> HEAP_CHUNK_BITS][i & HEAP_CHUNK_MASK]
312+
self.slot(i)
256313
}
257314
}
258315

‎rust/crates/psychoscript/src/object.rs‎

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,74 @@ impl Prop {
5454
}
5555
}
5656

57+
/// An object's properties in insertion order: its own vector, or for a built-in of the fresh runtime that the run
58+
/// has not changed, the image's (`builtins::image`), which every run reads in place. The first write through it
59+
/// (`DerefMut`, `own`) copies them, so a run pays for the built-ins it changes and no others.
60+
#[derive(Clone)]
61+
pub enum Items {
62+
Shared(&'static [Prop]),
63+
Own(Vec<Prop>),
64+
}
65+
66+
impl Default for Items {
67+
fn default() -> Self {
68+
Items::Own(Vec::new())
69+
}
70+
}
71+
72+
impl Items {
73+
/// The properties as the object's own vector, copied from the image's first if they are still its.
74+
#[inline]
75+
pub fn own(&mut self) -> &mut Vec<Prop> {
76+
if let Items::Shared(shared) = *self {
77+
*self = Items::Own(shared.to_vec());
78+
}
79+
match self {
80+
Items::Own(items) => items,
81+
Items::Shared(_) => unreachable!("made its own above"),
82+
}
83+
}
84+
85+
fn capacity(&self) -> usize {
86+
match self {
87+
Items::Own(items) => items.capacity(),
88+
Items::Shared(_) => 0,
89+
}
90+
}
91+
}
92+
93+
impl std::ops::Deref for Items {
94+
type Target = [Prop];
95+
96+
#[inline(always)]
97+
fn deref(&self) -> &[Prop] {
98+
match self {
99+
Items::Own(items) => items,
100+
Items::Shared(items) => items,
101+
}
102+
}
103+
}
104+
105+
impl std::ops::DerefMut for Items {
106+
#[inline]
107+
fn deref_mut(&mut self) -> &mut [Prop] {
108+
self.own()
109+
}
110+
}
111+
112+
impl<'a> IntoIterator for &'a Items {
113+
type Item = &'a Prop;
114+
type IntoIter = std::slice::Iter<'a, Prop>;
115+
116+
fn into_iter(self) -> Self::IntoIter {
117+
self.iter()
118+
}
119+
}
120+
57121
/// Insertion-ordered properties; small objects scan linearly, larger ones keep a hash index.
58122
#[derive(Default, Clone)]
59123
pub struct Props {
60-
pub items: Vec<Prop>,
124+
pub items: Items,
61125
index: Option<Box<FxHashMap<Atom, u32>>>,
62126
}
63127

@@ -75,7 +139,15 @@ impl Props {
75139
/// Room for exactly `n` properties, for an object whose count is known as it is made.
76140
#[inline]
77141
pub fn reserve_exact(&mut self, n: usize) {
78-
self.items.reserve_exact(n);
142+
self.items.own().reserve_exact(n);
143+
}
144+
145+
/// These properties, read in place from the image's object they are of.
146+
pub(crate) fn shared(&'static self) -> Props {
147+
Props {
148+
items: Items::Shared(&self.items),
149+
index: self.index.clone(),
150+
}
79151
}
80152

81153
#[inline]
@@ -104,9 +176,10 @@ impl Props {
104176
let i = self.items.len();
105177
// The first allocation holds two properties, not the four a vector starts with: most objects are small.
106178
if self.items.capacity() == 0 {
107-
self.items.reserve_exact(2);
179+
let n = self.items.len();
180+
self.items.own().reserve_exact(n.max(2));
108181
}
109-
self.items.push(Prop::new(key, value, flags));
182+
self.items.own().push(Prop::new(key, value, flags));
110183
if let Some(idx) = &mut self.index {
111184
idx.insert(key, i as u32);
112185
} else if self.items.len() > INDEX_THRESHOLD {
@@ -125,7 +198,7 @@ impl Props {
125198
let Some(i) = self.find(before) else {
126199
return self.push_new(key, value, flags);
127200
};
128-
self.items.insert(i, Prop::new(key, value, flags));
201+
self.items.own().insert(i, Prop::new(key, value, flags));
129202
if self.index.is_some() || self.items.len() > INDEX_THRESHOLD {
130203
let map = self
131204
.items
@@ -141,7 +214,7 @@ impl Props {
141214
let Some(i) = self.find(key) else {
142215
return false;
143216
};
144-
self.items.remove(i);
217+
self.items.own().remove(i);
145218
if self.index.is_some() {
146219
let map = self
147220
.items
@@ -686,8 +759,9 @@ impl Obj {
686759
}
687760
}
688761

689-
/// A copy of an object of the fresh runtime, whose kinds are the few with nothing a run owns alone.
690-
pub(crate) fn clone_base(&self) -> Obj {
762+
/// A run's copy of an object of the image, whose kinds are the few with nothing a run owns alone. Its properties
763+
/// stay the image's until the run writes one.
764+
pub(crate) fn clone_base(&'static self) -> Obj {
691765
let kind = match &self.kind {
692766
Kind::Ordinary => Kind::Ordinary,
693767
Kind::Array(items) => Kind::Array(items.clone()),
@@ -712,7 +786,7 @@ impl Obj {
712786
};
713787
Obj {
714788
proto: self.proto,
715-
props: self.props.clone(),
789+
props: self.props.shared(),
716790
kind,
717791
flags: self.flags,
718792
visit: 0,

0 commit comments

Comments
 (0)