diff --git a/examples/native_editor/main.rs b/examples/native_editor/main.rs index b985046..6d63d2c 100644 --- a/examples/native_editor/main.rs +++ b/examples/native_editor/main.rs @@ -235,6 +235,7 @@ impl EditorObserver { "export": ThreadUsage::between(fonts.worker, exported.worker), "ready": ThreadUsage::between(Some(initial), ready.worker), "reused_final_fingerprints": self.reused_final_fingerprints.load(Ordering::Relaxed), + "reused_source": work.reused_source.as_deref(), }); } payload diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index a608476..e531dc5 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -44,6 +44,7 @@ pub(super) struct CheckpointCache { pub(super) struct PublishedCheckpoints { current: Arc, previous_body: Option>, + previous_nested: Option>, previous_completion: Option>, previous_fingerprint: Option>, } @@ -91,6 +92,7 @@ pub(super) struct CheckpointSet { pub(super) struct SelectedCheckpoint { set: Arc, candidate: ExecutionCandidate, + current_set_for_nested_resume: Option>, previous_fingerprint: Option>, } @@ -626,6 +628,14 @@ impl CheckpointCache { published.previous_body.clone() } }); + let previous_nested = published.as_ref().and_then(|published| { + if !next.refresh_on_resume { + return None; + } + request.project.as_deref() + .and_then(|project| published.current.reserve_previous_child_size(project)) + .or_else(|| published.previous_nested.clone()) + }); let previous_completion = published.as_ref().and_then(|published| { published.current.completion.clone().or_else(|| published.previous_completion.clone()) }); @@ -644,6 +654,7 @@ impl CheckpointCache { *published = Some(Arc::new(PublishedCheckpoints { current: next, previous_body, + previous_nested, previous_completion, previous_fingerprint, })); @@ -703,10 +714,12 @@ impl PublishedCheckpoints { .current .candidates() .chain(self.previous_body.iter().flat_map(|body| body.candidates())) + .chain(self.previous_nested.iter().flat_map(|nested| nested.candidates())) .chain(self.current.completion.iter().chain(&self.previous_completion).map(|result| { SelectedCheckpoint { set: self.current.clone(), candidate: ExecutionCandidate::Completed(result.clone()), + current_set_for_nested_resume: None, previous_fingerprint: None, } })) @@ -800,6 +813,9 @@ impl PublishedCheckpoints { } *evidence = attempt; selected.previous_fingerprint = self.previous_fingerprint.clone(); + if self.previous_nested.as_ref().is_some_and(|nested| Arc::ptr_eq(nested, &selected.set)) { + selected.current_set_for_nested_resume = Some(self.current.clone()); + } return Ok((selected, prepared)); } } @@ -829,6 +845,32 @@ impl CheckpointSet { })) } + fn reserve_previous_child_size(&self, project: &ProjectSnapshot) -> Option> { + let changed = self.entries.iter().filter_map(|entry| entry.guard.nested_source.as_ref()) + .find(|source| { + source.observed_size.is_some_and(|size| { + project.file(&source.path).is_some_and(|file| file.version.bytes != size) + }) + })?; + let mut entries: Vec<_> = self.entries.iter().rev() + .filter(|entry| entry.guard.nested_source.as_ref().is_some_and(|source| { + source.path == changed.path && source.observed_size == changed.observed_size + })) + .take(NESTED_INTERVALS) + .cloned() + .collect(); + entries.reverse(); + let replay_count = entries.iter().map(|entry| entry.replay_count).max()?; + Some(Arc::new(Self { + entries, + calls: Arc::from(self.calls.get(..replay_count)?), + completion: None, + suffix_base: None, + refresh_on_resume: self.refresh_on_resume, + edit_frontier: self.edit_frontier, + })) + } + pub(super) fn report_capture(&self, work: &mut EntryCheckpointWork) { work.retained_checkpoints = self.entries.len(); work.captured_frontier = self.entries.iter().rev() @@ -842,6 +884,7 @@ impl CheckpointSet { .map(|index| SelectedCheckpoint { set: Arc::clone(self), candidate: ExecutionCandidate::Boundary(index), + current_set_for_nested_resume: None, previous_fingerprint: None, }) } @@ -972,6 +1015,23 @@ impl SelectedCheckpoint { data: file.data.to_vec(), }); let mut captures = CapturedEntries::new(request, files); + if let Some(current) = &self.current_set_for_nested_resume { + // A normal entry checkpoint may still use this replay even + // when the selected child checkpoint came from another revision. + // Keep only earlier entry anchors with the same recorded calls. + let common = current.calls.iter().zip(&self.set.calls[..checkpoint.replay_count]) + .take_while(|(current, old)| same_recorded_call(current, old)) + .count(); + for previous in ¤t.entries { + if previous.replay_count <= common + && previous.entry.completed_steps() <= checkpoint.entry.completed_steps() + && previous.entry.frontier() <= entry_frontier + && let Some(slots) = schedule.retain(previous) + { + captures.retain(Capture::Entry(slots), previous.clone()); + } + } + } // Earlier retained positions refer only to calls already replayed. for previous in &self.set.entries[..=index] { if previous.replay_count > checkpoint.replay_count { @@ -996,7 +1056,8 @@ impl SelectedCheckpoint { ); let cwd = &request.world.cwd; let cancellation = job.cancellation(); - let suffixes = self.set.completion.iter().chain(&self.set.suffix_base); + let donors = self.current_set_for_nested_resume.as_ref().unwrap_or(&self.set); + let suffixes = donors.completion.iter().chain(&donors.suffix_base); let mut suffix_base = None; let mut reused_layouts = None; let mut timings = CheckpointTimings::default(); @@ -1132,11 +1193,22 @@ impl SelectedCheckpoint { next.report_capture(work); Ok((result, next, reused_layouts)) } else { - Ok((result, self.set, reused_layouts)) + Ok((result, self.current_set_for_nested_resume.unwrap_or(self.set), reused_layouts)) } } } +fn same_recorded_call(left: &Call, right: &Call) -> bool { + match (left, right) { + (Call::Checked(left), Call::Checked(right)) => Arc::ptr_eq(left, right), + (Call::ReadSource(left), Call::ReadSource(right)) => Arc::ptr_eq(left, right), + (Call::FileSize(left), Call::FileSize(right)) => Arc::ptr_eq(left, right), + (Call::EndStatistics(left), Call::EndStatistics(right)) => Arc::ptr_eq(left, right), + (Call::ResourceUsage(left), Call::ResourceUsage(right)) => Arc::ptr_eq(left, right), + _ => false, + } +} + fn measure(elapsed: &mut Duration, enabled: bool, action: impl FnOnce() -> T) -> T { let started = enabled.then(Instant::now); let value = action(); diff --git a/tests/native_pipeline.rs b/tests/native_pipeline.rs index 452d438..a26ac4a 100644 --- a/tests/native_pipeline.rs +++ b/tests/native_pipeline.rs @@ -664,7 +664,12 @@ fn child_checkpoints_reuse_only_the_validated_physical_prefix() { let guarded = NativeCompiler::default().with_automatic_entry_checkpoints(); guarded.compile(request.clone().with_project(probed.clone())).unwrap(); - for (revision, edit, reuse_leaf) in [(2, "200 points", true), (3, "3000 points", false)] { + for (revision, edit, reuse_leaf) in [ + (2, "200 points", true), + (3, "3000 points", false), + (4, "400 points", true), + (5, "5000 points", true), + ] { let changed = original.replace("100 points", edit); let bytes = changed.len() as i32; probed = probed.with_file_edits(revision, [(leaf.clone(), Arc::from(changed.into_bytes()))].into()).unwrap();