| @@ -21,6 +21,7 @@ |
| 21 | 21 | |
| 22 | 22 | use std::collections::HashMap; |
| 23 | 23 | use std::ffi::CStr; |
| 24 | +use std::hash::BuildHasherDefault; |
| 24 | 25 | |
| 25 | 26 | use crate::abi::{ |
| 26 | 27 | Block, Ctx, FLAG_PINNED, TAG_ARRAY, TAG_BUILDER, TAG_BYTES, TAG_CAP, TAG_FRAME, TAG_REF, |
| @@ -46,13 +47,18 @@ pub(crate) struct NotSendable(pub &'static CStr); |
| 46 | 47 | /// `ctx` must be the running actor's context and `v` an owned value. On |
| 47 | 48 | /// error, the returned value is still owned by the caller. |
| 48 | 49 | pub(crate) unsafe fn transfer(ctx: *mut Ctx, v: Value) -> Result<Value, (NotSendable, Value)> { |
| 50 | + // An immediate (an Int, a constructor without fields) needs no walk. |
| 51 | + if !is_ptr(v) { |
| 52 | + return Ok(v); |
| 53 | + } |
| 49 | 54 | let mut root = v; |
| 50 | 55 | let mut walk = Walk { |
| 51 | 56 | ctx, |
| 52 | | − slots: vec![&raw mut root], |
| 53 | | − copies: HashMap::new(), |
| 57 | + slots: Slots::default(), |
| 58 | + copies: None, |
| 54 | 59 | dead: Vec::new(), |
| 55 | 60 | }; |
| 61 | + walk.slots.push(&raw mut root); |
| 56 | 62 | // SAFETY: forwarded caller guarantee; every slot pushed is a field of |
| 57 | 63 | // a block this walk owns (or `root`). |
| 58 | 64 | let result = unsafe { walk.run() }; |
| @@ -69,13 +75,55 @@ pub(crate) unsafe fn transfer(ctx: *mut Ctx, v: Value) -> Result<Value, (NotSend |
| 69 | 75 | struct Walk { |
| 70 | 76 | ctx: *mut Ctx, |
| 71 | 77 | /// Fields still to visit. Each holds one owned reference. |
| 72 | | − slots: Vec<*mut Value>, |
| 73 | | − /// Shared blocks already copied, and their copies. |
| 74 | | − copies: HashMap<*mut Block, *mut Block>, |
| 78 | + slots: Slots, |
| 79 | + /// Shared blocks already copied, and their copies: made on the first |
| 80 | + /// copy (most messages are moved), keyed by address with the runtime's |
| 81 | + /// cheap id hasher (`sched::IdHasher`). |
| 82 | + copies: Option<HashMap<usize, *mut Block, BuildHasherDefault<crate::sched::IdHasher>>>, |
| 75 | 83 | /// Originals whose last reference the walk replaced by their copy. |
| 76 | 84 | dead: Vec<Value>, |
| 77 | 85 | } |
| 78 | 86 | |
| 87 | +/// The walk's stack of slots: inline up to [`Slots::INLINE`], so a send |
| 88 | +/// of a small message allocates nothing; deeper ones spill to the heap. |
| 89 | +/// (A `Vec` here cost a `malloc` and a `free` per send, issue #156.) |
| 90 | +struct Slots { |
| 91 | + inline: [*mut Value; Slots::INLINE], |
| 92 | + len: usize, |
| 93 | + spill: Vec<*mut Value>, |
| 94 | +} |
| 95 | + |
| 96 | +impl Default for Slots { |
| 97 | + fn default() -> Self { |
| 98 | + Slots { |
| 99 | + inline: [std::ptr::null_mut(); Slots::INLINE], |
| 100 | + len: 0, |
| 101 | + spill: Vec::new(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl Slots { |
| 107 | + const INLINE: usize = 32; |
| 108 | + |
| 109 | + fn push(&mut self, slot: *mut Value) { |
| 110 | + if self.len < Self::INLINE { |
| 111 | + self.inline[self.len] = slot; |
| 112 | + self.len += 1; |
| 113 | + } else { |
| 114 | + self.spill.push(slot); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn pop(&mut self) -> Option<*mut Value> { |
| 119 | + if let Some(slot) = self.spill.pop() { |
| 120 | + return Some(slot); |
| 121 | + } |
| 122 | + self.len = self.len.checked_sub(1)?; |
| 123 | + Some(self.inline[self.len]) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 79 | 127 | impl Walk { |
| 80 | 128 | /// # Safety |
| 81 | 129 | /// See [`transfer`]. |
| @@ -90,7 +138,7 @@ impl Walk { |
| 90 | 138 | let b = v as *mut Block; |
| 91 | 139 | check_sendable(b)?; |
| 92 | 140 | // Reached again after being copied: share the copy. |
| 93 | | − if let Some(©) = self.copies.get(&b) { |
| 141 | + if let Some(©) = self.copies.as_ref().and_then(|c| c.get(&(b as usize))) { |
| 94 | 142 | (*copy).header.rc += 1; |
| 95 | 143 | *slot = copy as Value; |
| 96 | 144 | if (*b).header.rc == 1 { |
| @@ -150,7 +198,7 @@ impl Walk { |
| 150 | 198 | dup_value(*fields.add(i)); |
| 151 | 199 | } |
| 152 | 200 | (*b).header.rc -= 1; |
| 153 | | − self.copies.insert(b, copy); |
| 201 | + self.copies.get_or_insert_default().insert(b as usize, copy); |
| 154 | 202 | self.push_fields(copy); |
| 155 | 203 | copy |
| 156 | 204 | } |