Start route D incremental TeX core with M0 checkpoint spike
This commit is contained in:
1 parent
b41a2a9a97
commit
99dbb5ebb0
8 files changed
+447
-3
No files matched your search
@@ -0,0 +1,331 @@
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
pub type UnitId = u64;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct VerticalBox {
|
||||
pub width: i64,
|
||||
pub height: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum VerticalItem {
|
||||
Box(VerticalBox),
|
||||
Glue(i64),
|
||||
Penalty(i64),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Unit {
|
||||
pub id: UnitId,
|
||||
pub source: String,
|
||||
pub items: Vec<VerticalItem>,
|
||||
}
|
||||
|
||||
impl Unit {
|
||||
/// M0 placeholder for the real TeX paragraph builder: one line per 40
|
||||
/// characters, 12pt baseline distance, 10pt text.
|
||||
pub fn paragraph(id: UnitId, source: impl Into<String>) -> Self {
|
||||
let source = source.into();
|
||||
let lines = source.chars().count().div_ceil(40).max(1) as i64;
|
||||
Self {
|
||||
id,
|
||||
source,
|
||||
items: vec![VerticalItem::Box(VerticalBox {
|
||||
width: 10 * 40,
|
||||
height: lines * 12,
|
||||
})],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Document {
|
||||
pub units: Vec<Unit>,
|
||||
pub page_goal: i64,
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
|
||||
Self { units, page_goal }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Page {
|
||||
pub items: Vec<VerticalItem>,
|
||||
pub sources: Vec<(UnitId, u64)>,
|
||||
pub hash: u64,
|
||||
}
|
||||
|
||||
impl Page {
|
||||
fn seal_in_place(&mut self) {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
self.items.hash(&mut hasher);
|
||||
self.sources.hash(&mut hasher);
|
||||
self.hash = hasher.finish();
|
||||
}
|
||||
}
|
||||
|
||||
fn source_hash(source: &str) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
source.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Checkpoint {
|
||||
/// Index of the next unit to execute.
|
||||
pub unit_index: usize,
|
||||
/// Number of completed pages.
|
||||
pub page_index: usize,
|
||||
/// Vertical space already used on the current page.
|
||||
pub used: i64,
|
||||
/// Page items already emitted on the current page.
|
||||
pub page_items: Vec<VerticalItem>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Layout {
|
||||
pub pages: Vec<Page>,
|
||||
/// `checkpoints[i]` is the state before executing unit `i`.
|
||||
pub checkpoints: Vec<Checkpoint>,
|
||||
/// Number of pages whose content had to be rebuilt by the last edit.
|
||||
pub recomputed_pages: usize,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
pub fn page_index_for(&self, unit_index: usize) -> Option<usize> {
|
||||
self.checkpoints.get(unit_index).map(|c| c.page_index)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
layout: Layout,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn layout(document: &Document) -> Self {
|
||||
let mut pages = Vec::new();
|
||||
let mut checkpoints = Vec::with_capacity(document.units.len());
|
||||
let mut current = Page {
|
||||
items: Vec::new(),
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
};
|
||||
let mut used = 0_i64;
|
||||
for (unit_index, unit) in document.units.iter().enumerate() {
|
||||
checkpoints.push(Checkpoint {
|
||||
unit_index,
|
||||
page_index: pages.len(),
|
||||
used,
|
||||
page_items: current.items.clone(),
|
||||
});
|
||||
for item in &unit.items {
|
||||
match item {
|
||||
VerticalItem::Box(vertical_box) => {
|
||||
if used + vertical_box.height > document.page_goal
|
||||
&& !current.items.is_empty()
|
||||
{
|
||||
pages.push(std::mem::replace(
|
||||
&mut current,
|
||||
Page {
|
||||
items: Vec::new(),
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
},
|
||||
));
|
||||
used = 0;
|
||||
}
|
||||
used += vertical_box.height;
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
VerticalItem::Glue(amount) => {
|
||||
if !current.items.is_empty() {
|
||||
used += amount;
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
}
|
||||
VerticalItem::Penalty(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !current.items.is_empty() {
|
||||
pages.push(current);
|
||||
}
|
||||
for (index, unit) in document.units.iter().enumerate() {
|
||||
if let Some(checkpoint) = checkpoints.get(index)
|
||||
&& let Some(page) = pages.get_mut(checkpoint.page_index)
|
||||
{
|
||||
page.sources.push((unit.id, source_hash(&unit.source)));
|
||||
}
|
||||
}
|
||||
for page in &mut pages {
|
||||
page.seal_in_place();
|
||||
}
|
||||
Self {
|
||||
layout: Layout {
|
||||
pages,
|
||||
checkpoints,
|
||||
recomputed_pages: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pages(&self) -> &[Page] {
|
||||
&self.layout.pages
|
||||
}
|
||||
|
||||
pub fn checkpoints(&self) -> &[Checkpoint] {
|
||||
&self.layout.checkpoints
|
||||
}
|
||||
|
||||
pub fn recomputed_pages(&self) -> usize {
|
||||
self.layout.recomputed_pages
|
||||
}
|
||||
|
||||
pub fn edit(&mut self, document: &Document, unit_id: UnitId, source: impl Into<String>) {
|
||||
let Some(unit_index) = document.units.iter().position(|unit| unit.id == unit_id) else {
|
||||
self.layout = Self::layout(document).layout;
|
||||
self.layout.recomputed_pages = self.layout.pages.len();
|
||||
return;
|
||||
};
|
||||
let mut next = document.clone();
|
||||
next.units[unit_index].source = source.into();
|
||||
next.units[unit_index] = Unit::paragraph(unit_id, next.units[unit_index].source.clone());
|
||||
|
||||
let checkpoint = self
|
||||
.layout
|
||||
.checkpoints
|
||||
.get(unit_index)
|
||||
.cloned()
|
||||
.unwrap_or(Checkpoint {
|
||||
unit_index,
|
||||
page_index: 0,
|
||||
used: 0,
|
||||
page_items: Vec::new(),
|
||||
});
|
||||
let old_pages = self.layout.pages.clone();
|
||||
let mut pages = old_pages[..checkpoint.page_index].to_vec();
|
||||
let mut current = Page {
|
||||
items: checkpoint.page_items,
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
};
|
||||
let mut used = checkpoint.used;
|
||||
let mut checkpoints = self.layout.checkpoints[..unit_index].to_vec();
|
||||
|
||||
for (index, unit) in next.units.iter().enumerate().skip(unit_index) {
|
||||
checkpoints.push(Checkpoint {
|
||||
unit_index: index,
|
||||
page_index: pages.len(),
|
||||
used,
|
||||
page_items: current.items.clone(),
|
||||
});
|
||||
for item in &unit.items {
|
||||
match item {
|
||||
VerticalItem::Box(vertical_box) => {
|
||||
if used + vertical_box.height > next.page_goal && !current.items.is_empty()
|
||||
{
|
||||
pages.push(std::mem::replace(
|
||||
&mut current,
|
||||
Page {
|
||||
items: Vec::new(),
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
},
|
||||
));
|
||||
used = 0;
|
||||
}
|
||||
used += vertical_box.height;
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
VerticalItem::Glue(amount) => {
|
||||
if !current.items.is_empty() {
|
||||
used += amount;
|
||||
current.items.push(item.clone());
|
||||
}
|
||||
}
|
||||
VerticalItem::Penalty(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !current.items.is_empty() {
|
||||
pages.push(current);
|
||||
}
|
||||
for (index, unit) in next.units.iter().enumerate() {
|
||||
if let Some(checkpoint) = checkpoints.get(index)
|
||||
&& let Some(page) = pages.get_mut(checkpoint.page_index)
|
||||
{
|
||||
page.sources.push((unit.id, source_hash(&unit.source)));
|
||||
}
|
||||
}
|
||||
for page in &mut pages {
|
||||
page.seal_in_place();
|
||||
}
|
||||
|
||||
let mut rebuilt = 0;
|
||||
for (index, page) in pages.iter().enumerate().skip(checkpoint.page_index) {
|
||||
if old_pages.get(index).is_none_or(|old| old.hash != page.hash) {
|
||||
rebuilt += 1;
|
||||
}
|
||||
}
|
||||
self.layout = Layout {
|
||||
pages,
|
||||
checkpoints,
|
||||
recomputed_pages: rebuilt,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn document() -> Document {
|
||||
Document::new(
|
||||
50,
|
||||
vec![
|
||||
Unit::paragraph(1, "a"),
|
||||
Unit::paragraph(2, "b"),
|
||||
Unit::paragraph(3, "c"),
|
||||
Unit::paragraph(4, "d"),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_splits_pages_and_records_checkpoints() {
|
||||
let engine = Engine::layout(&document());
|
||||
assert_eq!(engine.pages().len(), 1);
|
||||
assert_eq!(engine.checkpoints().len(), 4);
|
||||
assert_eq!(engine.checkpoints()[1].unit_index, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_on_second_page_only_rebuilds_second_page() {
|
||||
let mut document = document();
|
||||
document.units.push(Unit::paragraph(5, "e"));
|
||||
document.units.push(Unit::paragraph(6, "f"));
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert!(engine.pages().len() >= 2);
|
||||
engine.edit(&document, 5, "edited second page");
|
||||
assert_eq!(engine.recomputed_pages(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_on_first_page_rebuilds_following_pages() {
|
||||
let mut engine = Engine::layout(&document());
|
||||
engine.edit(&document(), 1, "edited first page");
|
||||
assert!(engine.recomputed_pages() >= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_round_trips_through_serde() {
|
||||
let engine = Engine::layout(&document());
|
||||
let encoded = serde_json::to_string(engine.checkpoints()).unwrap();
|
||||
let decoded: Vec<Checkpoint> = serde_json::from_str(&encoded).unwrap();
|
||||
assert_eq!(engine.checkpoints(), decoded.as_slice());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user