diff --git a/crates/texrra-tex-core/src/execution/dependency_reads.rs b/crates/texrra-tex-core/src/execution/dependency_reads.rs new file mode 100644 index 0000000..2db6984 --- /dev/null +++ b/crates/texrra-tex-core/src/execution/dependency_reads.rs @@ -0,0 +1,74 @@ +//! Deduplicate execution reads before producing the ordered public evidence. +use super::Dependency; +use std::collections::{BTreeSet, HashSet}; + +#[derive(Clone, Default)] +pub(super) struct DependencyReads { + byte_catcodes: [u64; 4], + other: HashSet, +} + +impl DependencyReads { + pub(super) fn catcode(&mut self, character: char) { + let index = character as usize; + if let Some(bits) = self.byte_catcodes.get_mut(index / 64) { + *bits |= 1 << (index % 64); + } else { + self.other.insert(Dependency::CatCode(character)); + } + } + + pub(super) fn insert(&mut self, dependency: Dependency) { + if let Dependency::CatCode(character) = dependency { + self.catcode(character); + } else { + self.other.insert(dependency); + } + } + + pub(super) fn into_ordered(self) -> BTreeSet { + let byte_catcodes = (0..=u8::MAX) + .filter(|&index| { + let index = usize::from(index); + self.byte_catcodes[index / 64] & (1 << (index % 64)) != 0 + }) + .map(|index| Dependency::CatCode(char::from(index))); + self.other.into_iter().chain(byte_catcodes).collect() + } +} + +impl Extend for DependencyReads { + fn extend>(&mut self, dependencies: T) { + for dependency in dependencies { + self.insert(dependency); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cloned_record_preserves_byte_and_unicode_dependencies() { + let mut read = DependencyReads::default(); + for character in ['\0', '@', 'ÿ', 'Ā', '文'] { + read.catcode(character); + read.insert(Dependency::CatCode(character)); + } + read.insert(Dependency::EndLineChar); + let early = read.clone(); + read.extend([Dependency::EndLineChar, Dependency::ParagraphToken]); + let mut expected = BTreeSet::from([ + Dependency::CatCode('\0'), + Dependency::CatCode('@'), + Dependency::CatCode('ÿ'), + Dependency::CatCode('Ā'), + Dependency::CatCode('文'), + Dependency::EndLineChar, + ]); + assert_eq!(early.into_ordered(), expected); + expected.insert(Dependency::ParagraphToken); + assert_eq!(read.into_ordered(), expected); + } +} diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index aef20f3..3ec72b0 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -215,7 +215,7 @@ pub struct Executor { sources: Vec>, stream_sources: BTreeSet, generated_inputs: std::collections::BTreeMap, - reads: BTreeSet, + reads: DependencyReads, meaning_reads: HashSet, reads_tracing_macros: bool, conditionals: Vec, @@ -410,7 +410,7 @@ impl Executor { sources: Vec::new(), stream_sources: BTreeSet::new(), generated_inputs: Default::default(), - reads: BTreeSet::new(), + reads: DependencyReads::default(), meaning_reads: HashSet::new(), reads_tracing_macros: false, conditionals: Vec::new(), @@ -647,9 +647,10 @@ impl Executor { } } - fn collect_meaning_reads(&mut self) { - self.reads - .extend(self.meaning_reads.drain().map(Dependency::Meaning)); + fn take_dependency_reads(&mut self) -> BTreeSet { + let mut reads = std::mem::take(&mut self.reads).into_ordered(); + reads.extend(self.meaning_reads.drain().map(Dependency::Meaning)); + reads } fn meaning(&mut self, token: &Token) -> Meaning { @@ -3369,7 +3370,7 @@ impl Executor { for node in nodes { self.output_node(node, files)?; } - self.collect_meaning_reads(); + let reads = self.take_dependency_reads(); let format_pdf_images = self.captured_format_pdf_images(); Ok(Execution { format_dumped: self.format_dumped, @@ -3392,7 +3393,7 @@ impl Executor { sources: self.sources, stream_sources: self.stream_sources, generated_inputs: self.generated_inputs, - reads: self.reads, + reads, events: self.events.into_vec(), steps: self.steps, }) @@ -3802,7 +3803,7 @@ impl Executor { incoming_state: u64, previous: Option<&Execution>, ) -> Result { - self.collect_meaning_reads(); + let reads = self.take_dependency_reads(); let result = result.and_then(|()| { if self.shipped_pages > 0 && !self.format_dumped && let Some(objects) = self.state.pdf_objects.as_deref_mut() @@ -3828,7 +3829,7 @@ impl Executor { source_versions, stream_sources: self.stream_sources, generated_inputs: self.generated_inputs, - reads: self.reads, + reads, events: self.events.into_vec(), input_position: self.input_position, incoming_state, @@ -3871,7 +3872,7 @@ impl Executor { sources: self.sources, stream_sources: self.stream_sources, generated_inputs: self.generated_inputs, - reads: self.reads, + reads, incoming_state, steps: self.steps, }) diff --git a/crates/texrra-tex-core/src/execution/insertions.rs b/crates/texrra-tex-core/src/execution/insertions.rs index 92cbd80..17ae25e 100644 --- a/crates/texrra-tex-core/src/execution/insertions.rs +++ b/crates/texrra-tex-core/src/execution/insertions.rs @@ -1,6 +1,6 @@ use super::page_builder::PageObservation; use super::*; -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::BTreeMap; /// Insertion material and the values captured inside its restricted group. #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] @@ -80,7 +80,7 @@ impl PageInsertions { at: usize, dims: &mut [i64; 8], state: &State, - reads: &mut BTreeSet, + reads: &mut super::DependencyReads, steps: &mut usize, limit: usize, observations: &mut Vec, @@ -198,7 +198,7 @@ impl PageInsertions { &mut self, page: Vec, state: &mut State, - reads: &mut BTreeSet, + reads: &mut super::DependencyReads, steps: &mut usize, limit: usize, ) -> Result, String> { diff --git a/crates/texrra-tex-core/src/execution/mod.rs b/crates/texrra-tex-core/src/execution/mod.rs index 5ab2661..489f37b 100644 --- a/crates/texrra-tex-core/src/execution/mod.rs +++ b/crates/texrra-tex-core/src/execution/mod.rs @@ -7,6 +7,8 @@ mod cancellation; mod case; mod context; mod device; +mod dependency_reads; +use dependency_reads::DependencyReads; mod diagnostics; mod direction; mod dvi; diff --git a/crates/texrra-tex-core/src/execution/mouth.rs b/crates/texrra-tex-core/src/execution/mouth.rs index 2d8e2ac..0459583 100644 --- a/crates/texrra-tex-core/src/execution/mouth.rs +++ b/crates/texrra-tex-core/src/execution/mouth.rs @@ -1,5 +1,5 @@ -use super::{CatCode, CommandName, Dependency, Span, State, Token, TokenKind}; -use std::{collections::BTreeSet, sync::Arc}; +use super::{CatCode, CommandName, Dependency, DependencyReads, Span, State, Token, TokenKind}; +use std::sync::Arc; use super::xe_encoding::{Character, Encoding}; @@ -18,7 +18,7 @@ mod tests { fn tokens(text: &str) -> Vec { let mut mouth = Mouth::new(0, text.into(), super::super::InputProfile::UnicodeScalar); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); let mut tokens = Vec::new(); while let Some(token) = mouth.next(&State::default(), &mut reads).unwrap() { tokens.push(token); @@ -52,7 +52,7 @@ mod tests { let mut mouth = Mouth::new(0, "\\~ A\\ B".into(), super::super::InputProfile::TeX8Bit); let mut state = State::default(); state.assign_catcode('~', CatCode::Space, false); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); let mut kinds = Vec::new(); while let Some(token) = mouth.next(&state, &mut reads).unwrap() { kinds.push(token.kind); @@ -77,7 +77,7 @@ mod tests { super::super::InputProfile::UnicodeScalar, ); let mut state = State::default(); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); assert_eq!( mouth.next(&state, &mut reads).unwrap().unwrap().kind, TokenKind::Command(CommandName::Control("set".into())) @@ -96,7 +96,7 @@ mod tests { super::super::InputProfile::UnicodeScalar, ); let mut state = State::default(); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); assert_eq!( mouth.next(&state, &mut reads).unwrap().unwrap().kind, TokenKind::Character('A', CatCode::Letter) @@ -122,7 +122,7 @@ mod tests { state.assign_catcode(mark, CatCode::Letter, false); } let mut mouth = Mouth::new(0, "é\n\\a\u{0315}\u{0300}!\na\u{0315}\u{0300}é".into(), InputProfile::XeTeX); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); let first = mouth.next(&state, &mut reads).unwrap().unwrap(); assert!(matches!(first.kind, TokenKind::Character('é', _))); assert_eq!((first.span.start, first.span.end), (0, 2)); @@ -142,7 +142,7 @@ mod tests { assert!(matches!(token.kind, TokenKind::Character(c, _) if c == value)); assert_eq!((token.span.start, token.span.end), range); } - assert!(reads.contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization))); + assert!(reads.into_ordered().contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization))); } #[test] @@ -154,7 +154,7 @@ mod tests { ); let mut state = State::default(); state.assign_integer(super::super::state::Key::Endlinechar, -1, false); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); assert_eq!( mouth.next(&state, &mut reads).unwrap().unwrap().kind, TokenKind::Command(CommandName::Control("word".into())) @@ -191,7 +191,7 @@ mod tests { state.assign_catcode('中', CatCode::Letter, false); state.assign_catcode('😀', CatCode::Letter, false); let mut mouth = Mouth::new(0, text.into(), InputProfile::XeTeX); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); let prime = mouth.next(&state, &mut reads).unwrap().unwrap(); assert_eq!(prime.kind, TokenKind::Character('′', state.catcode('′'))); assert_eq!((prime.span.start, prime.span.end), (0, 8)); @@ -230,7 +230,7 @@ mod tests { .collect::>() }; let state = State::default().with_input_profile(InputProfile::XeTeX); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); let mut mouth = Mouth::new(8, utf16("A\r\n😀\r\n"), InputProfile::XeTeX); let first = mouth.next(&state, &mut reads).unwrap().unwrap(); assert_eq!(first.span, Span { source: 8, start: 2, end: 4 }); @@ -442,7 +442,7 @@ impl Mouth { pub(super) fn load_line( &mut self, state: &State, - reads: &mut BTreeSet, + reads: &mut DependencyReads, ) -> Result { if self.end_after_line || self.offset == self.data.len() { return Ok(false); @@ -538,7 +538,7 @@ impl Mouth { fn load_xe_line( &mut self, state: &State, - reads: &mut BTreeSet, + reads: &mut DependencyReads, ) -> Result { let xe = self.xe.as_mut().unwrap(); let framing = xe.encoding.framing(); @@ -578,7 +578,7 @@ impl Mouth { content_end: usize, end: usize, state: &State, - reads: &mut BTreeSet, + reads: &mut DependencyReads, ) { reads.insert(Dependency::EndLineChar); if let Ok(value) = u32::try_from(state.endlinechar) @@ -594,8 +594,8 @@ impl Mouth { self.cursor = 0; self.state = LineState::New; } - fn catcode(state: &State, reads: &mut BTreeSet, c: char) -> CatCode { - reads.insert(Dependency::CatCode(c)); + fn catcode(state: &State, reads: &mut DependencyReads, c: char) -> CatCode { + reads.catcode(c); state.catcode(c) } // next points at the second superscript mark. The returned length @@ -640,7 +640,7 @@ impl Mouth { /// Decode TeX's ^^ form before category lookup. Category changes affect /// unread characters; tokens already in macro bodies are never retokenized. - fn peek(&mut self, state: &State, reads: &mut BTreeSet) -> Option { + fn peek(&mut self, state: &State, reads: &mut DependencyReads) -> Option { loop { let c = *self.line.get(self.cursor)?; if Self::catcode(state, reads, c.value) != CatCode::Superscript { @@ -659,7 +659,7 @@ impl Mouth { /// get_next's ordinary ^^ conversion changes cur_chr and advances loc. /// Only the control-sequence scanner rewrites the input buffer (peek). /// Keeping these paths distinct also preserves TeX's displayed context. - fn next_character(&mut self, state: &State, reads: &mut BTreeSet) -> Character { + fn next_character(&mut self, state: &State, reads: &mut DependencyReads) -> Character { let mut c = self.line[self.cursor]; self.cursor += 1; while Self::catcode(state, reads, c.value) == CatCode::Superscript { @@ -677,7 +677,7 @@ impl Mouth { pub fn next( &mut self, state: &State, - reads: &mut BTreeSet, + reads: &mut DependencyReads, ) -> Result, (Span, String)> { loop { while self.cursor == self.line.len() { @@ -777,7 +777,7 @@ mod byte_tests { } state.assign_integer(crate::execution::state::Key::Endlinechar, -1, false); let mut mouth = Mouth::new(7, (0..=255u8).collect(), InputProfile::TeX8Bit); - let mut reads = BTreeSet::new(); + let mut reads = DependencyReads::default(); for b in (0..=255u8).filter(|b| !matches!(b, b'\r' | b'\n')) { let t = mouth.next(&state, &mut reads).unwrap().unwrap(); assert_eq!(t.kind, TokenKind::Character(char::from(b), CatCode::Other)); diff --git a/crates/texrra-tex-core/src/execution/page_builder.rs b/crates/texrra-tex-core/src/execution/page_builder.rs index 99dd472..f0dd771 100644 --- a/crates/texrra-tex-core/src/execution/page_builder.rs +++ b/crates/texrra-tex-core/src/execution/page_builder.rs @@ -1,6 +1,6 @@ //! Online TeX page construction over owned contributions, independent of paint. use super::*; -use std::collections::{BTreeSet, VecDeque}; +use std::collections::VecDeque; const AWFUL: i64 = 1_073_741_823; #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] @@ -178,7 +178,7 @@ impl NativePageState { pub(super) fn advance( &mut self, state: &mut State, - reads: &mut BTreeSet, + reads: &mut super::DependencyReads, steps: &mut usize, limit: usize, observations: &mut Vec, diff --git a/crates/texrra-tex-core/src/execution/vertical_break.rs b/crates/texrra-tex-core/src/execution/vertical_break.rs index 75ad462..9256e60 100644 --- a/crates/texrra-tex-core/src/execution/vertical_break.rs +++ b/crates/texrra-tex-core/src/execution/vertical_break.rs @@ -34,7 +34,7 @@ pub(super) fn find_break( height: i64, max_depth: i32, state: &State, - reads: &mut BTreeSet, + reads: &mut super::DependencyReads, steps: &mut usize, limit: usize, mut ignored_shrink: impl FnMut() -> Result<(), String>, diff --git a/crates/texrra-tex-core/src/execution/xe_encoding.rs b/crates/texrra-tex-core/src/execution/xe_encoding.rs index d079419..75cb66b 100644 --- a/crates/texrra-tex-core/src/execution/xe_encoding.rs +++ b/crates/texrra-tex-core/src/execution/xe_encoding.rs @@ -1,5 +1,5 @@ //! XeTeX physical input encodings. Decoded units retain their source bytes. -use std::{collections::BTreeSet, sync::Arc}; +use std::sync::Arc; use super::{Dependency, Parameter, ParameterValue, State}; @@ -121,7 +121,7 @@ impl Encoding { data: &[u8], base: usize, state: &State, - reads: &mut BTreeSet, + reads: &mut super::DependencyReads, ) -> Result<(Vec, usize), String> { let (mut characters, warnings) = self.decode(data, base)?; let parameter = Parameter::XeTeXinputnormalization; diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 1697e95..b29b7be 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,30 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-11-29 执行依赖去重与字符类别读取 + +字节范围内的字符类别读取通过位图登记,其他依赖在执行时用 +哈希集合去重。字符类别值仍在扫描时从当前状态读取。检查点 +保留目前已观察的完整依赖,成功、失败及出页 I/O 的公共回执 +仍以相同顺序发布。定向检查覆盖 U+0000、字节边界、Unicode +和检查点复制。两版实际故障探针的 113 项读取、事件和进出状态 +指纹完全相同,包括未定义命令和 Unicode 控制序列。 + +在原样 NeurIPS 2025 的同一修订序列中,第 3 页编辑的执行耗时 +由 3.56 秒降到 3.25 秒,恢复由 3.04 秒降到 2.83 秒。前版 +891 个执行样本有 57 个落在依赖树查找,候选 813 个样本中 +没有采到该查找。两版 PDF、SVG 逐字节一致,均只转换第 3 页。 +编辑、故障恢复、依赖、资源读取、来源映射与辅助文件均按独立 +全量或上次发布核对。原样 A4 主、子文件编辑也通过独立全量对照。 + +该计时来自单次阶段对照。正式浏览器可见 p95、峰值内存和同版 +出版矩阵继续统一验收。候选执行样本中原始取 token 是最大的 +已定位内核热点。后续检查同一字符扫描内的重复类别查询, +并保留现有的物理输入位置和 TeX 诊断语义。 + +证据在 target/p4-dependency-reads/。SHA256SUMS 指纹为 +4ad9bc91ddf3c77950dfffdd6ee51086cfd78c52fe27cf604780206abf1d45d8。 + ## 2026-11-29 控制序列查找与热编辑执行 在原样 NeurIPS 2025 编辑中分段测量。旧版第 3 页编辑的