| @@ -23,12 +23,190 @@ use crate::abi::{ |
| 23 | 23 | |
| 24 | 24 | /// Blocks whose count reached zero, waiting for their children to be |
| 25 | 25 | /// released. Owned by each actor so draining never touches shared state. |
| 26 | +/// |
| 27 | +/// A drop drains it a step at a time (issue #158): at most [`DRAIN_BLOCKS`] |
| 28 | +/// blocks, then the rest waits for the actor's next safe point |
| 29 | +/// ([`continue_drain`]), so freeing a large structure doesn't hold its |
| 30 | +/// thread. Everything left is freed when the actor ends. |
| 26 | 31 | #[derive(Default)] |
| 27 | 32 | pub struct DropStack { |
| 28 | 33 | pending: Vec<*mut Block>, |
| 34 | + /// Large blocks being released from their end: each with the number |
| 35 | + /// of its scanned fields still to release. |
| 36 | + partial: Vec<(*mut Block, usize)>, |
| 29 | 37 | draining: bool, |
| 30 | 38 | } |
| 31 | 39 | |
| 40 | +/// The blocks one step of a drain frees: about 40–60 µs (a list of 4,096 |
| 41 | +/// cells takes 45 µs). |
| 42 | +const DRAIN_BLOCKS: u32 = 4096; |
| 43 | + |
| 44 | +/// A block with more scanned fields than this is set aside and released |
| 45 | +/// a step at a time, from its end: [`DRAIN_FIELDS`] fields per step. |
| 46 | +const LARGE: usize = 4096; |
| 47 | +const DRAIN_FIELDS: usize = 65536; |
| 48 | + |
| 49 | +impl DropStack { |
| 50 | + /// Whether dead blocks wait to be freed. |
| 51 | + pub(crate) fn has_pending(&self) -> bool { |
| 52 | + !self.pending.is_empty() || !self.partial.is_empty() |
| 53 | + } |
| 54 | + |
| 55 | + /// Set `b` aside to release in steps if it is large (more than |
| 56 | + /// [`LARGE`] fields, and nothing to run when freed); whether it was. |
| 57 | + /// |
| 58 | + /// # Safety |
| 59 | + /// `b` must be dead, with `scan_fsize` [`SCAN_LARGE`]. |
| 60 | + #[cold] |
| 61 | + #[inline(never)] |
| 62 | + unsafe fn set_aside(&mut self, b: *mut Block) -> bool { |
| 63 | + // SAFETY: forwarded caller guarantee. |
| 64 | + let (n, _) = unsafe { scanned(b) }; |
| 65 | + // SAFETY: as above. |
| 66 | + if n > LARGE && plain(unsafe { (*b).header.tag }) { |
| 67 | + self.partial.push((b, n)); |
| 68 | + return true; |
| 69 | + } |
| 70 | + false |
| 71 | + } |
| 72 | + |
| 73 | + /// Free up to `blocks` dead blocks; whether it stopped there (more |
| 74 | + /// may be left). Large blocks are only set aside here. |
| 75 | + /// |
| 76 | + /// # Safety |
| 77 | + /// Only blocks whose last reference was released are queued. |
| 78 | + #[inline(always)] |
| 79 | + unsafe fn free_some(&mut self, mut blocks: u32) -> bool { |
| 80 | + while let Some(b) = self.pending.pop() { |
| 81 | + // SAFETY: `b` is dead, and live until freed. |
| 82 | + unsafe { |
| 83 | + if (*b).header.scan_fsize == SCAN_LARGE && self.set_aside(b) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + free_block(b, &mut self.pending); |
| 87 | + } |
| 88 | + blocks -= 1; |
| 89 | + if blocks == 0 { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + false |
| 94 | + } |
| 95 | + |
| 96 | + /// One step of a drain: [`DRAIN_BLOCKS`] blocks, or else |
| 97 | + /// [`DRAIN_FIELDS`] fields of the large block set aside last. Whether |
| 98 | + /// nothing is left. |
| 99 | + /// |
| 100 | + /// # Safety |
| 101 | + /// As [`DropStack::free_some`]. |
| 102 | + #[cold] |
| 103 | + #[inline(never)] |
| 104 | + unsafe fn step(&mut self) -> bool { |
| 105 | + // SAFETY: forwarded caller guarantee. |
| 106 | + if unsafe { self.free_some(DRAIN_BLOCKS) } { |
| 107 | + return !self.has_pending(); |
| 108 | + } |
| 109 | + if let Some((b, left)) = self.partial.last_mut() { |
| 110 | + let (b, end) = (*b, *left); |
| 111 | + let start = end.saturating_sub(DRAIN_FIELDS); |
| 112 | + // SAFETY: `b` is dead; its fields `start..end` were not |
| 113 | + // released yet. |
| 114 | + unsafe { |
| 115 | + let (fields, _) = scanned_fields(b); |
| 116 | + for i in start..end { |
| 117 | + release_into(*fields.add(i), &mut self.pending); |
| 118 | + } |
| 119 | + } |
| 120 | + *left = start; |
| 121 | + if start == 0 { |
| 122 | + self.partial.pop(); |
| 123 | + // SAFETY: every field was released; nothing else refers |
| 124 | + // to `b`. |
| 125 | + unsafe { blocks::free(b) }; |
| 126 | + stats::free(); |
| 127 | + } |
| 128 | + } |
| 129 | + !self.has_pending() |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl Drop for DropStack { |
| 134 | + fn drop(&mut self) { |
| 135 | + debug_assert!( |
| 136 | + !self.has_pending(), |
| 137 | + "louise: an actor ended with blocks left to free" |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// Continue freeing what an earlier drop left (a step), at a safe point |
| 143 | +/// of the running actor: when it was preempted or woken. If more is left, |
| 144 | +/// its next function entry yields again (`reductions` 0). |
| 145 | +/// |
| 146 | +/// # Safety |
| 147 | +/// `ctx` must be the running actor's context. |
| 148 | +#[inline(always)] |
| 149 | +pub(crate) unsafe fn continue_drain(ctx: *mut Ctx) { |
| 150 | + // SAFETY: forwarded caller guarantee. |
| 151 | + unsafe { |
| 152 | + let stack = crate::sched::drop_stack(ctx); |
| 153 | + if stack.has_pending() && !stack.draining { |
| 154 | + drain_step(ctx, stack); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/// # Safety |
| 160 | +/// `ctx` must be the running actor's context, `stack` its drop stack. |
| 161 | +#[cold] |
| 162 | +#[inline(never)] |
| 163 | +unsafe fn drain_step(ctx: *mut Ctx, stack: &mut DropStack) { |
| 164 | + stack.draining = true; |
| 165 | + // SAFETY: only dead blocks are queued. |
| 166 | + let done = unsafe { stack.step() }; |
| 167 | + stack.draining = false; |
| 168 | + if !done { |
| 169 | + // SAFETY: the running actor's context. |
| 170 | + unsafe { (*ctx).reductions = 0 }; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/// Free what an actor's drops left when its body returned, a step at a |
| 175 | +/// time, yielding to other actors between steps: its exit is recorded |
| 176 | +/// after. On the actor's own stack. |
| 177 | +/// |
| 178 | +/// # Safety |
| 179 | +/// `ctx` must be the running actor's context, on its own stack. |
| 180 | +pub(crate) unsafe fn drain_before_exit(ctx: *mut Ctx) { |
| 181 | + // SAFETY: forwarded caller guarantee. |
| 182 | + unsafe { |
| 183 | + while crate::sched::drop_stack(ctx).has_pending() { |
| 184 | + let stack = crate::sched::drop_stack(ctx); |
| 185 | + stack.draining = true; |
| 186 | + let done = stack.step(); |
| 187 | + stack.draining = false; |
| 188 | + if !done { |
| 189 | + crate::sched::preempt_root(ctx); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +/// Free everything left when an actor ends (after |
| 196 | +/// [`drain_before_exit`], what releasing its mailbox dropped), at once. |
| 197 | +/// |
| 198 | +/// # Safety |
| 199 | +/// `ctx` must be the ending actor's context. |
| 200 | +pub(crate) unsafe fn finish_drain(ctx: *mut Ctx) { |
| 201 | + // SAFETY: forwarded caller guarantee. |
| 202 | + unsafe { |
| 203 | + let stack = crate::sched::drop_stack(ctx); |
| 204 | + stack.draining = true; |
| 205 | + while !stack.step() {} |
| 206 | + stack.draining = false; |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 32 | 210 | /// The allocator of blocks (see the module documentation). |
| 33 | 211 | /// |
| 34 | 212 | /// mimalloc is called directly: its `GlobalAlloc` (the `mimalloc` crate) |
| @@ -322,6 +500,18 @@ pub unsafe extern "C" fn tl_drop_slow(ctx: *mut Ctx, b: *mut Block) { |
| 322 | 500 | return; |
| 323 | 501 | } |
| 324 | 502 | } |
| 503 | + // SAFETY: forwarded caller guarantee; `b` is dead. |
| 504 | + unsafe { drop_queued(ctx, b) } |
| 505 | +} |
| 506 | + |
| 507 | +/// Free the dead block `b` and what dies with it, through the actor's |
| 508 | +/// worklist, [`DRAIN_BLOCKS`] at most. Out of line, so that the leaf case |
| 509 | +/// above stays small enough to inline into callers (issue #158). |
| 510 | +/// |
| 511 | +/// # Safety |
| 512 | +/// `ctx` must be the running actor's context; `b` dead. |
| 513 | +#[inline(never)] |
| 514 | +unsafe fn drop_queued(ctx: *mut Ctx, b: *mut Block) { |
| 325 | 515 | // SAFETY: the running actor's context always has a live actor. |
| 326 | 516 | let stack = unsafe { crate::sched::drop_stack(ctx) }; |
| 327 | 517 | stack.pending.push(b); |
| @@ -329,11 +519,15 @@ pub unsafe extern "C" fn tl_drop_slow(ctx: *mut Ctx, b: *mut Block) { |
| 329 | 519 | return; // An outer tl_drop_slow on this actor is already draining. |
| 330 | 520 | } |
| 331 | 521 | stack.draining = true; |
| 332 | | − while let Some(b) = stack.pending.pop() { |
| 333 | | − // SAFETY: only blocks whose last reference was released are pushed. |
| 334 | | − unsafe { free_block(b, &mut stack.pending) }; |
| 335 | | − } |
| 522 | + // SAFETY: only blocks whose last reference was released are pushed. |
| 523 | + let stopped = unsafe { stack.free_some(DRAIN_BLOCKS) }; |
| 336 | 524 | stack.draining = false; |
| 525 | + if stopped || !stack.partial.is_empty() { |
| 526 | + // The rest at the next safe point: the next function entry yields, |
| 527 | + // and the actor continues when it runs again (`continue_drain`). |
| 528 | + // SAFETY: the running actor's context. |
| 529 | + unsafe { (*ctx).reductions = 0 }; |
| 530 | + } |
| 337 | 531 | } |
| 338 | 532 | |
| 339 | 533 | /// Deallocate a unique block whose children were moved out by the caller |
| @@ -373,20 +567,9 @@ unsafe fn free_block(b: *mut Block, pending: &mut Vec<*mut Block>) { |
| 373 | 567 | crate::abi::TAG_RESOURCE => crate::resource::finalize_resource(b), |
| 374 | 568 | _ => {} |
| 375 | 569 | } |
| 376 | | − let (n, offset) = scanned(b); |
| 377 | | − let fields = Block::fields_ptr(b).add(offset); |
| 570 | + let (fields, n) = scanned_fields(b); |
| 378 | 571 | for i in 0..n { |
| 379 | | − let v = *fields.add(i); |
| 380 | | − if !is_ptr(v) { |
| 381 | | − continue; |
| 382 | | − } |
| 383 | | − let child = v as *mut Block; |
| 384 | | − let rc = (*child).header.rc; |
| 385 | | − if rc > 1 { |
| 386 | | − (*child).header.rc = rc - 1; |
| 387 | | − } else if release(child) { |
| 388 | | − pending.push(child); |
| 389 | | − } |
| 572 | + release_into(*fields.add(i), pending); |
| 390 | 573 | } |
| 391 | 574 | blocks::free(b); |
| 392 | 575 | } |
| @@ -528,3 +711,65 @@ pub(crate) unsafe fn release_into(v: Value, pending: &mut Vec<*mut Block>) { |
| 528 | 711 | } |
| 529 | 712 | } |
| 530 | 713 | } |
| 714 | + |
| 715 | +#[cfg(test)] |
| 716 | +mod tests { |
| 717 | + use super::*; |
| 718 | + use crate::abi::UNIT; |
| 719 | + use crate::abi::int; |
| 720 | + use crate::values::{list, string}; |
| 721 | + |
| 722 | + /// Run `continue_drain` until nothing is left; the number of steps. |
| 723 | + unsafe fn steps(ctx: *mut Ctx) -> usize { |
| 724 | + // SAFETY: forwarded caller guarantee. |
| 725 | + unsafe { |
| 726 | + let stack = crate::sched::drop_stack(ctx); |
| 727 | + let mut steps = 1; |
| 728 | + while stack.has_pending() { |
| 729 | + assert_eq!((*ctx).reductions, 0, "the next function entry yields"); |
| 730 | + (*ctx).reductions = crate::sched::REDUCTIONS; |
| 731 | + continue_drain(ctx); |
| 732 | + steps += 1; |
| 733 | + } |
| 734 | + steps |
| 735 | + } |
| 736 | + } |
| 737 | + |
| 738 | + /// Large structures are freed a step at a time (issue #158), and what |
| 739 | + /// is left when the actor ends is freed then. |
| 740 | + unsafe extern "C" fn drops_in_steps(ctx: *mut Ctx) -> Value { |
| 741 | + // SAFETY: `ctx` is the root actor's; every value built is owned. |
| 742 | + unsafe { |
| 743 | + let before = stats::tl_stats_live_blocks(); |
| 744 | + // A million cells: about 3,600 per step. |
| 745 | + drop_value(ctx, list(ctx, (0..1_000_000).map(int))); |
| 746 | + let n = steps(ctx); |
| 747 | + assert!((200..400).contains(&n), "{n} steps"); |
| 748 | + assert_eq!(stats::tl_stats_live_blocks(), before); |
| 749 | + // An array of 100,000 strings, released from its end (its |
| 750 | + // fields cost one each, its strings 16), after the list it was |
| 751 | + // made from (cells cost 18). |
| 752 | + let strings = list(ctx, (0..100_000).map(|i| string(ctx, &i.to_string()))); |
| 753 | + let array = crate::array::tl_array_from_list(ctx, strings); |
| 754 | + drop_value(ctx, strings); |
| 755 | + drop_value(ctx, array); |
| 756 | + let n = steps(ctx); |
| 757 | + assert!((40..80).contains(&n), "{n} steps"); |
| 758 | + assert_eq!(stats::tl_stats_live_blocks(), before); |
| 759 | + // Small drops take one step. |
| 760 | + drop_value(ctx, list(ctx, (0..1000).map(int))); |
| 761 | + assert!(!crate::sched::drop_stack(ctx).has_pending()); |
| 762 | + // Left over: the actor's end frees it (`finish_drain`; the |
| 763 | + // stack's `Drop` checks). |
| 764 | + drop_value(ctx, list(ctx, (0..1_000_000).map(int))); |
| 765 | + assert!(crate::sched::drop_stack(ctx).has_pending()); |
| 766 | + } |
| 767 | + UNIT |
| 768 | + } |
| 769 | + |
| 770 | + #[test] |
| 771 | + fn large_drops_are_freed_in_steps() { |
| 772 | + // SAFETY: `drops_in_steps` follows the generated-code convention. |
| 773 | + assert_eq!(unsafe { crate::sched::run_test_actor(drops_in_steps) }, 0); |
| 774 | + } |
| 775 | +} |