Reuse the active edit line within the checkpoint budget
This commit is contained in:
1 parent
600160f624
commit
ca60c66a17
2 files changed
+235
-7
No files matched your search
@@ -44,6 +44,7 @@ pub(super) struct CheckpointSet {
|
||||
entries: Vec<Arc<CachedCheckpoint>>,
|
||||
calls: Vec<Call>,
|
||||
refresh_on_resume: bool,
|
||||
edit_frontier: Option<usize>,
|
||||
}
|
||||
|
||||
pub(super) struct SelectedCheckpoint {
|
||||
@@ -61,6 +62,7 @@ struct CaptureSchedule {
|
||||
seen: [bool; SOURCE_INTERVALS],
|
||||
next_page: usize,
|
||||
fixed_frontier: Option<usize>,
|
||||
pending_edit_frontier: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -68,20 +70,38 @@ struct CaptureSlots {
|
||||
interval: usize,
|
||||
early: bool,
|
||||
page: bool,
|
||||
edit: bool,
|
||||
next_page: usize,
|
||||
}
|
||||
|
||||
impl CaptureSchedule {
|
||||
fn new(bytes: usize, fixed_frontier: Option<usize>) -> Self {
|
||||
fn new(bytes: usize, fixed_frontier: Option<usize>, edit_frontier: Option<usize>) -> Self {
|
||||
Self {
|
||||
bytes,
|
||||
stride: bytes.div_ceil(SOURCE_INTERVALS).max(1),
|
||||
seen: [false; SOURCE_INTERVALS],
|
||||
next_page: 1,
|
||||
fixed_frontier,
|
||||
pending_edit_frontier: edit_frontier,
|
||||
}
|
||||
}
|
||||
|
||||
fn take_edit_frontier(&mut self, frontier: usize, ordinary_capture: bool) -> bool {
|
||||
let edit = self
|
||||
.pending_edit_frontier
|
||||
.take_if(|pending| *pending == frontier)
|
||||
.is_some();
|
||||
if edit && !ordinary_capture {
|
||||
// Move a later byte-interval capture to the active editing line.
|
||||
// Keep the earlier prefix and all shipout capture opportunities.
|
||||
let next_interval = (frontier - 1) / self.stride + 1;
|
||||
if let Some(seen) = self.seen.get_mut(next_interval) {
|
||||
*seen = true;
|
||||
}
|
||||
}
|
||||
edit
|
||||
}
|
||||
|
||||
fn select(&mut self, frontier: usize, pages: usize) -> Option<CaptureSlots> {
|
||||
if let Some(minimum) = self.fixed_frontier {
|
||||
if self.seen[0] || frontier < minimum {
|
||||
@@ -92,6 +112,7 @@ impl CaptureSchedule {
|
||||
interval: 0,
|
||||
early: true,
|
||||
page: false,
|
||||
edit: false,
|
||||
next_page: self.next_page,
|
||||
});
|
||||
}
|
||||
@@ -104,10 +125,12 @@ impl CaptureSchedule {
|
||||
if page {
|
||||
self.next_page = pages.saturating_add((pages / 8).max(1).next_power_of_two());
|
||||
}
|
||||
(early || page).then_some(CaptureSlots {
|
||||
let edit = self.take_edit_frontier(frontier, early || page);
|
||||
(early || page || edit).then_some(CaptureSlots {
|
||||
interval,
|
||||
early,
|
||||
page,
|
||||
edit,
|
||||
next_page: self.next_page,
|
||||
})
|
||||
}
|
||||
@@ -123,6 +146,7 @@ impl CaptureSchedule {
|
||||
Some(CaptureSlots {
|
||||
interval,
|
||||
early,
|
||||
edit: self.take_edit_frontier(frontier, early || checkpoint.slots.page),
|
||||
..checkpoint.slots
|
||||
})
|
||||
}
|
||||
@@ -134,6 +158,7 @@ struct CapturedEntries<'a> {
|
||||
initial: Option<GeneratedInitialState>,
|
||||
early: [Option<Arc<CachedCheckpoint>>; SOURCE_INTERVALS],
|
||||
shipped: [Option<Arc<CachedCheckpoint>>; SOURCE_INTERVALS],
|
||||
edit: Option<Arc<CachedCheckpoint>>,
|
||||
changed: bool,
|
||||
}
|
||||
|
||||
@@ -149,6 +174,7 @@ impl<'a> CapturedEntries<'a> {
|
||||
initial: files.preparation().generated_initial.clone(),
|
||||
early: Default::default(),
|
||||
shipped: Default::default(),
|
||||
edit: None,
|
||||
changed: false,
|
||||
}
|
||||
}
|
||||
@@ -157,6 +183,9 @@ impl<'a> CapturedEntries<'a> {
|
||||
if slots.early {
|
||||
self.early[slots.interval] = Some(checkpoint.clone());
|
||||
}
|
||||
if slots.edit {
|
||||
self.edit = Some(checkpoint.clone());
|
||||
}
|
||||
if slots.page {
|
||||
self.shipped[slots.interval] = Some(checkpoint);
|
||||
}
|
||||
@@ -199,14 +228,34 @@ impl<'a> CapturedEntries<'a> {
|
||||
if !self.changed {
|
||||
return None;
|
||||
}
|
||||
let edit_frontier = self
|
||||
.edit
|
||||
.as_ref()
|
||||
.map(|checkpoint| checkpoint.entry.frontier());
|
||||
let mut entries: Vec<_> = self
|
||||
.early
|
||||
.into_iter()
|
||||
.chain(self.shipped)
|
||||
.flatten()
|
||||
.chain(self.edit)
|
||||
.collect();
|
||||
entries.sort_by_key(|candidate| candidate.entry.completed_steps());
|
||||
entries.dedup_by(|a, b| Arc::ptr_eq(a, b));
|
||||
if entries.len() > 2 * SOURCE_INTERVALS {
|
||||
let edit = edit_frontier.expect("an edit checkpoint can fill the remaining slot");
|
||||
// Preserve the earliest continuation and the page checkpoints.
|
||||
// Displace the ordinary continuation closest to the edited line.
|
||||
let (index, _) = entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(1)
|
||||
.filter(|(_, checkpoint)| {
|
||||
!checkpoint.slots.page && checkpoint.entry.frontier() != edit
|
||||
})
|
||||
.min_by_key(|(_, checkpoint)| checkpoint.entry.frontier().abs_diff(edit))
|
||||
.expect("the full schedule has ordinary continuations");
|
||||
entries.remove(index);
|
||||
}
|
||||
let replay_count = entries.iter().map(|entry| entry.replay_count).max()?;
|
||||
let mut calls = recording.into_calls()?;
|
||||
calls.truncate(replay_count);
|
||||
@@ -214,6 +263,7 @@ impl<'a> CapturedEntries<'a> {
|
||||
entries,
|
||||
calls,
|
||||
refresh_on_resume,
|
||||
edit_frontier,
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -291,7 +341,7 @@ impl CheckpointCache {
|
||||
Result<Arc<RevisionExecution>, RevisionFailure>,
|
||||
Option<Arc<CheckpointSet>>,
|
||||
) {
|
||||
let mut schedule = CaptureSchedule::new(source.data.len(), self.minimum_frontier);
|
||||
let mut schedule = CaptureSchedule::new(source.data.len(), self.minimum_frontier, None);
|
||||
let mut captures = CapturedEntries::new(request, files);
|
||||
let mut recording = RecordingHost::new(files);
|
||||
let result = job.run_capturing_entries(
|
||||
@@ -418,6 +468,7 @@ impl CheckpointSet {
|
||||
entries,
|
||||
calls: self.calls[..replay_count].to_vec(),
|
||||
refresh_on_resume: self.refresh_on_resume,
|
||||
edit_frontier: self.edit_frontier,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -485,7 +536,13 @@ impl SelectedCheckpoint {
|
||||
return Ok((result, self.set));
|
||||
}
|
||||
|
||||
let mut schedule = CaptureSchedule::new(source.data.len(), None);
|
||||
let previous = checkpoint
|
||||
.guard
|
||||
.project
|
||||
.file(&checkpoint.guard.entry)
|
||||
.expect("a checkpoint records its source");
|
||||
let edit = changed_line_start(&previous.data, &source.data).or(self.set.edit_frontier);
|
||||
let mut schedule = CaptureSchedule::new(source.data.len(), None, edit);
|
||||
let mut captures = CapturedEntries::new(request, files);
|
||||
// Share only calls and continuations from the prefix just replayed.
|
||||
for previous in &self.set.entries[..=self.index] {
|
||||
@@ -528,6 +585,28 @@ impl SelectedCheckpoint {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed_line_start(previous: &[u8], current: &[u8]) -> Option<usize> {
|
||||
if previous == current {
|
||||
return None;
|
||||
}
|
||||
let common = previous
|
||||
.iter()
|
||||
.zip(current)
|
||||
.take_while(|(old, new)| old == new)
|
||||
.count();
|
||||
let mut prefix = ¤t[..common];
|
||||
// A CR followed by LF is one physical line ending.
|
||||
if prefix.ends_with(b"\r") && current.get(common) == Some(&b'\n') {
|
||||
prefix = &prefix[..prefix.len() - 1];
|
||||
}
|
||||
Some(
|
||||
prefix
|
||||
.iter()
|
||||
.rposition(|byte| matches!(*byte, b'\r' | b'\n'))
|
||||
.map_or(0, |offset| offset + 1),
|
||||
)
|
||||
}
|
||||
|
||||
struct PrefixInputGuard {
|
||||
original: AttemptRequest,
|
||||
project: Arc<ProjectSnapshot>,
|
||||
@@ -1461,6 +1540,122 @@ fn immutable_file_creation_and_removal_replay_real_lookup_results() {
|
||||
assert_eq!(pdfs.values().collect::<BTreeSet<_>>().len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn automatic_checkpoints_refresh_the_previous_edit_line_after_a_child_edit() {
|
||||
let mut schedule = CaptureSchedule::new(400, None, Some(290));
|
||||
assert!(schedule.select(215, 0).unwrap().early);
|
||||
let edit = schedule.select(290, 0).unwrap();
|
||||
assert!(edit.edit && !edit.early);
|
||||
assert!(schedule.select(345, 0).is_none());
|
||||
assert!(schedule.select(345, 1).unwrap().page);
|
||||
|
||||
let cwd = std::env::temp_dir().join(format!("texrra-edit-line-{}", std::process::id()));
|
||||
fs::create_dir(&cwd).unwrap();
|
||||
let cwd = cwd.canonicalize().unwrap();
|
||||
let entry = cwd.join("main.tex");
|
||||
let child = cwd.join("child.tex");
|
||||
let source_prefix = format!(
|
||||
"\\catcode123=1 \\catcode125=2\n{}\\input child.tex\n{}",
|
||||
"\\advance\\count0 by 1\\relax\n".repeat(40),
|
||||
"\\advance\\count2 by 1\\relax\n".repeat(18),
|
||||
);
|
||||
let edited_line_start = source_prefix.len();
|
||||
let original = format!(
|
||||
"{source_prefix}\\shipout\\hbox{{\\vrule width1pt height\\heightfromchild pt}}\n{}\\end\n",
|
||||
"\\advance\\count1 by 1\\relax\n".repeat(5),
|
||||
);
|
||||
let mut world = WorldConfig::new(&cwd);
|
||||
world.input_policy = InputPolicy::Tex;
|
||||
let project = ProjectSnapshot::new(ProjectDefinition {
|
||||
configuration: world.clone(),
|
||||
revision: 1,
|
||||
roots: BTreeSet::from([cwd.clone()]),
|
||||
files: BTreeMap::from([
|
||||
(entry.clone(), Arc::from(original.as_bytes())),
|
||||
(
|
||||
child.clone(),
|
||||
Arc::from(b"\\def\\heightfromchild{1}\n".as_slice()),
|
||||
),
|
||||
]),
|
||||
directories: BTreeSet::new(),
|
||||
aliases: BTreeMap::new(),
|
||||
generated_roots: BTreeSet::from([cwd.clone()]),
|
||||
generated: BTreeSet::new(),
|
||||
})
|
||||
.unwrap();
|
||||
let mut request = CompileRequest::new(
|
||||
world,
|
||||
"main.tex",
|
||||
"main.pdf",
|
||||
JobTime::new(2026, 11, 20, 540)
|
||||
.unwrap()
|
||||
.with_pdf_clock(0, 0)
|
||||
.unwrap(),
|
||||
)
|
||||
.with_project(project.clone())
|
||||
.observe_generated_initial();
|
||||
request.entry = Entry::Initex;
|
||||
NativeCompiler::default().compile(request.clone()).unwrap();
|
||||
let session = NativeCompiler::default().with_automatic_entry_checkpoints();
|
||||
session.compile(request.clone()).unwrap();
|
||||
|
||||
let entry_edit = project
|
||||
.with_file_edits(
|
||||
2,
|
||||
BTreeMap::from([(
|
||||
entry.clone(),
|
||||
Arc::from(original.replace("width1pt", "width2pt").into_bytes()),
|
||||
)]),
|
||||
)
|
||||
.unwrap();
|
||||
session
|
||||
.compile(request.clone().with_project(entry_edit.clone()))
|
||||
.unwrap();
|
||||
let child_edit = entry_edit
|
||||
.with_file_edits(
|
||||
3,
|
||||
BTreeMap::from([(child, Arc::from(b"\\def\\heightfromchild{3}\n".as_slice()))]),
|
||||
)
|
||||
.unwrap();
|
||||
let child_result = session
|
||||
.compile(request.clone().with_project(child_edit.clone()))
|
||||
.unwrap();
|
||||
let child_work = &child_result.entry_checkpoint_work;
|
||||
assert!(
|
||||
child_work.reused_frontier.unwrap() < edited_line_start,
|
||||
"{child_work:?}"
|
||||
);
|
||||
|
||||
let revision = child_edit
|
||||
.with_file_edits(
|
||||
4,
|
||||
BTreeMap::from([(
|
||||
entry,
|
||||
Arc::from(original.replace("width1pt", "width4pt").into_bytes()),
|
||||
)]),
|
||||
)
|
||||
.unwrap();
|
||||
let request = request.with_project(revision);
|
||||
let updated = session.compile(request.clone()).unwrap();
|
||||
let full = NativeCompiler::default().compile(request).unwrap();
|
||||
let work = &updated.entry_checkpoint_work;
|
||||
assert_eq!(work.reused_frontier, Some(edited_line_start), "{work:?}");
|
||||
assert!(work.reused_steps > child_work.reused_steps, "{work:?}");
|
||||
assert!(
|
||||
work.retained_checkpoints <= 2 * SOURCE_INTERVALS,
|
||||
"{work:?}"
|
||||
);
|
||||
assert!(updated.pages[0].items.iter().any(|item| matches!(
|
||||
item,
|
||||
texrra_tex_core::render::DisplayItem::Rule { width, height, .. }
|
||||
if (*width, *height) == (4 * 65_536, 3 * 65_536)
|
||||
)));
|
||||
assert_eq!(updated.pdf, full.pdf);
|
||||
assert_eq!(updated.source_map.sources, full.source_map.sources);
|
||||
assert_eq!(updated.source_map.pages, full.source_map.pages);
|
||||
assert!(updated.execution.execution == full.execution.execution);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_input_change_uses_a_checkpoint_before_the_read() {
|
||||
let cwd = std::env::temp_dir().join(format!("texrra-generated-prefix-{}", std::process::id()));
|
||||
|
||||
Reference in new issue
Block a user