feat: park idle task refresh and bound delivery to one callback per turn
This commit is contained in:
@@ -499,10 +499,10 @@ Flex main-axis policy: flexBasis is "auto" by default (zero basis when flex > 0,
|
||||
When an occurrence's cross-axis constraint changes during settling, its preferred main-axis size is invalidated and remeasured. Any obsolete native flex allocation on that axis is removed for the replacement measurement, so narrowing a nested Column can increase wrapped text height. Source arrange allocations remain explicit. Preferred-size presence and values participate in convergence/cycle detection.
|
||||
|
||||
|
||||
The source layout driver now uses Aster's resumable read-only call API with a soft quantum of 256 instructions. Pauses retain the same VM frames, operand/local roots, return contracts and read tracking; total instruction/frame limits still apply across slices. ui.stats().layoutSlices counts slices in the most recent solve (native fast path: 1). The worker currently resumes Pending immediately after yielding its OS time slice. Parking the solve across worker turns, scheduling other work and handling newer inputs before completion are not implemented yet. Native callbacks are not preempted. Only the completed settled frame is published.
|
||||
The source layout driver now uses Aster's resumable read-only call API with a soft quantum of 256 instructions. Pauses retain the same VM frames, operand/local roots, return contracts and read tracking; total instruction/frame limits still apply across slices. ui.stats().layoutSlices counts slices in the most recent solve (native fast path: 1). Viewport and idle-task refresh park Pending across worker turns; newer viewport targets can cancel and replace unpublished work. Explicit source/event inputs still drain synchronously. Native callbacks are not preempted. Only the completed settled frame is published.
|
||||
|
||||
|
||||
source_layout::start now returns either Complete(Draft) or Pending(Work). The host-owned Work retains the unpublished frame, derived runtime/cache snapshots, convergence plan and prepare root without borrowing caller frame/cache variables. Work::resume consumes the saved job and advances the same VM continuation; Work::cancel closes that continuation and releases the prepare root without merging a draft. A second start is rejected while any read-only execution is active. Every pending Work must be resumed or explicitly cancelled with its owning VM; dropping it is not cancellation. Synchronous ReplSession operations drain the same saved refresh state. Mount preparation/lifecycle cancellation and parking jobs in the execution worker remain unfinished.
|
||||
source_layout::start now returns either Complete(Draft) or Pending(Work). The host-owned Work retains the unpublished frame, derived runtime/cache snapshots, convergence plan and prepare root without borrowing caller frame/cache variables. Work::resume consumes the saved job and advances the same VM continuation; Work::cancel closes that continuation and releases the prepare root without merging a draft. A second start is rejected while any read-only execution is active. Every pending Work must be resumed or explicitly cancelled with its owning VM; dropping it is not cancellation. Synchronous ReplSession operations drain the same saved refresh state. The worker can park viewport and idle-task refresh jobs; cancellation restores still-pending Mount preparation.
|
||||
|
||||
|
||||
UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around the source job. This bridge-level continuation also owns its target window, page snapshot, tab projection, viewport restoration and retained lifecycle hook root. Only the Complete branch projects scroll/focus, captures dependency versions, merges derived caches, commits Mount geometry and runs deferred hooks. Resume restores the saved window context, and failure releases hook roots. ReplSession uses this protocol through its saved refresh state. Viewport refresh supports cross-worker-turn scheduling and prepared-Mount rollback; callers must not drop a pending bridge layout as a substitute for cancellation.
|
||||
@@ -511,7 +511,7 @@ UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around
|
||||
ReplSession now owns a resumable RefreshWork containing window traversal, pending bridge layout, unpublished per-window documents and the cumulative refresh work budget. poll_refresh advances one refresh stage or VM quantum; only a complete stable pass replaces the session's full document map. Synchronous callers drain the same state machine. Failure stops the domain, releases any pending bridge work and preserves the externally published picture. refreshYields counts cooperative refresh returns, not wall-clock time. Only still-pending Mount preparation is rolled back; completed callbacks remain committed.
|
||||
|
||||
|
||||
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads, explicit REPL/event refresh and task callbacks still drain synchronously; native Rust measurement/paint calls remain atomic.
|
||||
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads and explicit REPL/event refresh still drain synchronously. Idle task delivery starts at most one ready callback per turn and parks its subsequent layout; callbacks themselves and individual Rust measurement/paint calls remain atomic.
|
||||
|
||||
|
||||
Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active.
|
||||
@@ -527,3 +527,6 @@ Cancellation checkpoints own explicit VM roots until restored or released. Initi
|
||||
|
||||
|
||||
Large intrinsic layout trees (more than 64 occurrences) now use the resumable per-occurrence driver too. Native measurement caches remain active, and a single native callback remains atomic; VM instruction boundaries between nodes allow the worker to yield and replace an obsolete viewport. Small intrinsic trees retain the allocation-free native fast path. Structural extraction, driver preparation, settling checks and final paint/projection still run atomically; this is not a wall-clock preemption guarantee.
|
||||
|
||||
|
||||
Idle timer delivery now processes at most one ready callback (or one ineligible task cancellation) before starting an incremental refresh. No further task is delivered while that refresh is pending. Viewport changes can replace its unfinished layout without replaying the completed task callback. Task effects and native window commands publish only after settling; a previously completed resize may publish separately. Explicit input still drains pending work first. Synchronous host callers retain a maximum of 64 task deliveries per poll.
|
||||
|
||||
@@ -182,3 +182,6 @@ Cancellation checkpoints own explicit VM roots until restored or released. Initi
|
||||
|
||||
|
||||
source_layout uses the same resumable node driver for trees over 64 occurrences even when measure/arrange are intrinsic. Small intrinsic trees retain LayoutRuntime::frame. The larger path keeps LayoutRuntime measurement caches and yields between node calls; structural extraction, bytecode preparation, a single node, settling and paint remain atomic.
|
||||
|
||||
|
||||
Worker idle polls use ReplSession::poll_tasks_incremental with the current viewport map. Scheduler::poll accepts a checked 1..=64 work limit, counting both delivery and ineligible-task cancellation; idle uses one, synchronous compatibility uses 64. Once delivery begins layout, further tasks and window-command projection wait until the saved refresh completes. The viewport stamp allows cancellation of task-induced pending layout without repeating callback effects.
|
||||
|
||||
+11
-2
@@ -563,7 +563,16 @@ impl Worker {
|
||||
if runtime.has_pending_refresh() {
|
||||
return Ok((None, Vec::new()));
|
||||
}
|
||||
let completed = runtime.poll_tasks()?;
|
||||
let completed = if drain {
|
||||
runtime.poll_tasks()?
|
||||
} else {
|
||||
runtime.poll_tasks_incremental(&viewports)?
|
||||
};
|
||||
if runtime.has_pending_refresh() {
|
||||
// A complete earlier resize may publish; task effects and their
|
||||
// native commands wait until the task's refresh has settled.
|
||||
return Ok((resized, Vec::new()));
|
||||
}
|
||||
Ok((completed.or(resized), runtime.take_window_commands()?))
|
||||
}));
|
||||
let mut current = match namespace.write() {
|
||||
@@ -1165,7 +1174,7 @@ source_layout::start now returns either Complete(Draft) or Pending(Work). The ho
|
||||
UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around the source job. This bridge-level continuation also owns its target window, page snapshot, tab projection, viewport restoration and retained lifecycle hook root. Only the Complete branch projects scroll/focus, captures dependency versions, merges derived caches, commits Mount geometry and runs deferred hooks. Resume restores the saved window context, and failure releases hook roots. ReplSession uses this protocol through its saved refresh state. Viewport refresh supports cross-worker-turn scheduling and prepared-Mount rollback; callers must not drop a pending bridge layout as a substitute for cancellation.
|
||||
|
||||
|
||||
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads, explicit REPL/event refresh and task callbacks still drain synchronously; native Rust measurement/paint calls remain atomic.
|
||||
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads, explicit REPL/event refresh and task callback execution remains atomic; its idle-triggered layout yields across worker turns; native Rust measurement/paint calls remain atomic.
|
||||
|
||||
|
||||
Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active.
|
||||
|
||||
+30
-3
@@ -792,6 +792,21 @@ impl ReplSession {
|
||||
/// Deliver ready work on the owning VM worker, never from the GUI or a
|
||||
/// provider thread. A failed callback stops the same shared fault domain.
|
||||
pub fn poll_tasks(&mut self) -> Result<Option<RenderDocument>> {
|
||||
self.poll_tasks_mode(None)
|
||||
}
|
||||
|
||||
/// Idle delivery starts at most one callback, then parks its layout work.
|
||||
pub fn poll_tasks_incremental(
|
||||
&mut self,
|
||||
viewports: &std::collections::BTreeMap<u64, crate::layout::Size>,
|
||||
) -> Result<Option<RenderDocument>> {
|
||||
self.poll_tasks_mode(Some(viewports))
|
||||
}
|
||||
|
||||
fn poll_tasks_mode(
|
||||
&mut self,
|
||||
viewports: Option<&std::collections::BTreeMap<u64, crate::layout::Size>>,
|
||||
) -> Result<Option<RenderDocument>> {
|
||||
if self.has_pending_refresh() {
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -804,11 +819,23 @@ impl ReplSession {
|
||||
}
|
||||
let mut completed = false;
|
||||
let result = (|| {
|
||||
if self.bridge.poll_tasks(&mut self.candidate.vm)? == 0 {
|
||||
let limit = if viewports.is_some() { 1 } else { 64 };
|
||||
if self.bridge.poll_tasks(&mut self.candidate.vm, limit)? == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
completed = true;
|
||||
self.refresh_document()
|
||||
if let Some(viewports) = viewports {
|
||||
let progress = self.poll_refresh(true)?;
|
||||
if let Some(work) = &mut self.refresh {
|
||||
work.viewports = Some(viewports.clone());
|
||||
}
|
||||
Ok(match progress {
|
||||
RefreshProgress::Pending => None,
|
||||
RefreshProgress::Complete(document) => document.map(|document| *document),
|
||||
})
|
||||
} else {
|
||||
self.refresh_document()
|
||||
}
|
||||
})();
|
||||
if result.is_err() {
|
||||
self.faulted = true;
|
||||
@@ -816,7 +843,7 @@ impl ReplSession {
|
||||
self.take_window_commands()?;
|
||||
self.bridge.discard_pending(&mut self.candidate.vm)?;
|
||||
}
|
||||
if completed || result.is_err() {
|
||||
if (completed || result.is_err()) && !self.has_pending_refresh() {
|
||||
self.bridge
|
||||
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
|
||||
}
|
||||
|
||||
+6
-2
@@ -399,7 +399,8 @@ pub struct Scheduler {
|
||||
impl Scheduler {
|
||||
/// Mutation boundaries collect unreachable owner/callback cycles before
|
||||
/// this weak lookup. Idle timer polling does not rescan the entire heap.
|
||||
pub fn poll(&self, vm: &mut Vm) -> Result<usize> {
|
||||
pub fn poll(&self, vm: &mut Vm, limit: usize) -> Result<usize> {
|
||||
anyhow::ensure!((1..=MAX_READY).contains(&limit), "invalid task poll limit");
|
||||
let snapshot = self
|
||||
.index
|
||||
.lock()
|
||||
@@ -408,6 +409,9 @@ impl Scheduler {
|
||||
let now = Instant::now();
|
||||
let mut work = 0;
|
||||
for weak in snapshot {
|
||||
if work >= limit {
|
||||
break;
|
||||
}
|
||||
let Some(reference) = vm.upgrade(weak)? else {
|
||||
continue;
|
||||
};
|
||||
@@ -428,7 +432,7 @@ impl Scheduler {
|
||||
work += 1;
|
||||
return finish(vm, reference, State::Cancelled);
|
||||
}
|
||||
if task.due > now || work >= MAX_READY {
|
||||
if task.due > now {
|
||||
return Ok(());
|
||||
}
|
||||
work += 1;
|
||||
|
||||
+2
-2
@@ -1799,8 +1799,8 @@ fn project_tabs(
|
||||
}
|
||||
|
||||
impl UiBridge {
|
||||
pub fn poll_tasks(&self, vm: &mut Vm) -> Result<usize> {
|
||||
self.tasks.poll(vm)
|
||||
pub fn poll_tasks(&self, vm: &mut Vm, limit: usize) -> Result<usize> {
|
||||
self.tasks.poll(vm, limit)
|
||||
}
|
||||
pub fn cancel_tasks(&self, vm: &mut Vm) -> Result<usize> {
|
||||
self.tasks.cancel_all(vm)
|
||||
|
||||
Reference in New Issue
Block a user