Reuse settled suffixes after verified closed child edits
This commit is contained in:
1 parent
55c497dccd
commit
e4d6e049f8
11 files changed
+565
-72
No files matched your search
@@ -271,6 +271,24 @@ impl RetainedHistoryOffset {
|
||||
}
|
||||
}
|
||||
|
||||
/// Byte counts affected by the sole changed physical source at convergence.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct SuffixSourceResize {
|
||||
pub(super) sizes: (usize, usize),
|
||||
pub(super) active_at_anchor: bool,
|
||||
pub(super) prefix_resource_reads: usize,
|
||||
}
|
||||
|
||||
impl SuffixSourceResize {
|
||||
pub(super) fn entry(sizes: (usize, usize)) -> Self {
|
||||
Self {
|
||||
sizes,
|
||||
active_at_anchor: true,
|
||||
prefix_resource_reads: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatisticsReport {
|
||||
pub(super) fn new(phase: StatisticsPhase, destination: WriteDestination, span: Span) -> Self {
|
||||
let text = phase.text();
|
||||
@@ -292,7 +310,7 @@ impl StatisticsReport {
|
||||
if usage.host.is_some() && replayed_entry_bytes != u64::try_from(current).ok() {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, true)?;
|
||||
usage.resize_source_payload(SuffixSourceResize::entry((previous, current)), true)?;
|
||||
}
|
||||
debug_assert_eq!(self.text, self.phase.text());
|
||||
Some(())
|
||||
@@ -324,6 +342,16 @@ impl StatisticsReport {
|
||||
boxes: BoxNodeOffset,
|
||||
entry_sizes: Option<(usize, usize)>,
|
||||
history: Option<&RetainedHistoryOffset>,
|
||||
) -> Option<Self> {
|
||||
self.with_source_resize(steps, boxes, entry_sizes.map(SuffixSourceResize::entry), history)
|
||||
}
|
||||
|
||||
pub(super) fn with_source_resize(
|
||||
&self,
|
||||
steps: StepOffset,
|
||||
boxes: BoxNodeOffset,
|
||||
resize: Option<SuffixSourceResize>,
|
||||
history: Option<&RetainedHistoryOffset>,
|
||||
) -> Option<Self> {
|
||||
let translate = |usage: &mut ResourceUsage| {
|
||||
usage.steps = steps.apply(usage.steps)?;
|
||||
@@ -338,22 +366,24 @@ impl StatisticsReport {
|
||||
}
|
||||
for usage in [before, after] {
|
||||
translate(usage)?;
|
||||
if let Some((previous, current)) = entry_sizes {
|
||||
let active = usage.mouth_payload_bytes == previous;
|
||||
if !active && usage.mouth_payload_bytes != 0 {
|
||||
if let Some(resize) = resize {
|
||||
let active = resize.active_at_anchor && usage.mouth_payload_bytes == resize.sizes.0;
|
||||
if resize.active_at_anchor && !active && usage.mouth_payload_bytes != 0 {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, active)?;
|
||||
usage.resize_source_payload(resize, active)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
StatisticsPhase::End { usage, .. } => {
|
||||
translate(usage)?;
|
||||
if let Some((previous, current)) = entry_sizes {
|
||||
if usage.stacks.input_frames != 0 || usage.mouth_payload_bytes != 0 {
|
||||
if let Some(resize) = resize {
|
||||
if resize.active_at_anchor
|
||||
&& (usage.stacks.input_frames != 0 || usage.mouth_payload_bytes != 0)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, false)?;
|
||||
usage.resize_source_payload(resize, false)?;
|
||||
}
|
||||
if let Some(offset) = history {
|
||||
offset.replace_prefix(usage)?;
|
||||
@@ -376,18 +406,17 @@ impl StatisticsReport {
|
||||
}
|
||||
|
||||
impl ResourceUsage {
|
||||
fn resize_entry_payload(
|
||||
&mut self,
|
||||
previous: usize,
|
||||
current: usize,
|
||||
active: bool,
|
||||
) -> Option<()> {
|
||||
fn resize_source_payload(&mut self, resize: SuffixSourceResize, active: bool) -> Option<()> {
|
||||
let (previous, current) = resize.sizes;
|
||||
self.source_payload_bytes = self.source_payload_bytes.checked_sub(previous)?.checked_add(current)?;
|
||||
if active {
|
||||
self.mouth_payload_bytes = self.mouth_payload_bytes.checked_sub(previous)?.checked_add(current)?;
|
||||
}
|
||||
if let Some(host) = &mut self.host {
|
||||
host.rebind_entry_bytes(u64::try_from(previous).ok()?, u64::try_from(current).ok()?)?;
|
||||
let reads = u64::try_from(resize.prefix_resource_reads).ok()?;
|
||||
let previous = u64::try_from(previous).ok()?.checked_mul(reads)?;
|
||||
let current = u64::try_from(current).ok()?.checked_mul(reads)?;
|
||||
host.rebind_entry_bytes(previous, current)?;
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
@@ -920,6 +949,45 @@ mod tests {
|
||||
).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_source_resize_counts_each_host_read_and_leaves_other_mouths() {
|
||||
let usage = ResourceUsage {
|
||||
source_payload_bytes: 50,
|
||||
mouth_payload_bytes: 30,
|
||||
host: Some(HostResourceUsage {
|
||||
resource_reads: 5,
|
||||
resource_bytes_read: 100,
|
||||
..HostResourceUsage::default()
|
||||
}),
|
||||
token_limit: 4096,
|
||||
..ResourceUsage::default()
|
||||
};
|
||||
let resize = SuffixSourceResize {
|
||||
sizes: (8, 11),
|
||||
active_at_anchor: false,
|
||||
prefix_resource_reads: 3,
|
||||
};
|
||||
let report = StatisticsReport::new(
|
||||
StatisticsPhase::Shipout { before: usage.clone(), after: usage },
|
||||
WriteDestination::Log,
|
||||
Span::default(),
|
||||
);
|
||||
let adjusted = report.with_source_resize(
|
||||
StepOffset::new(0, 0), BoxNodeOffset::new(0, 0), Some(resize), None,
|
||||
).unwrap();
|
||||
assert_eq!(adjusted.text, report.text);
|
||||
let StatisticsPhase::Shipout { before, after } = adjusted.phase else {
|
||||
unreachable!();
|
||||
};
|
||||
for usage in [before, after] {
|
||||
assert_eq!(usage.source_payload_bytes, 53);
|
||||
assert_eq!(usage.mouth_payload_bytes, 30);
|
||||
let host = usage.host.unwrap();
|
||||
assert_eq!(host.resource_reads, 5);
|
||||
assert_eq!(host.resource_bytes_read, 109);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracked_macro_counts_share_the_ordinal_index_without_losing_visits() {
|
||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||
|
||||
Reference in new issue
Block a user