Reuse fixed file identity within a live alias scan

This commit is contained in:
gloamxun committed 2026-11-19 22:02:53 +08:00
1 parent 1c02d8af25
commit 24ea6ef7ee
4 files changed
+168 -32

No files matched your search

@@ -174,7 +174,7 @@ impl ProjectSnapshot {
pub fn generated(&self) -> impl Iterator<Item = &Path> {
self.generated.iter().map(PathBuf::as_path)
}
pub fn immutable_paths(&self) -> impl Iterator<Item = &Path> {
pub fn immutable_paths(&self) -> impl Iterator<Item = &Path> + Clone {
self.files.keys().map(PathBuf::as_path)
}
pub fn protects_artifact(&self, path: &Path) -> bool {
+44
View File
@@ -621,6 +621,50 @@ SHA-256 为 `ae44671f7d0cee93ab3a32008ec4ef82ec52ef61020be6ecd3eac1d8512b5b72`
[TeX Live 正则实现](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/pdftexdir/utils.c#L762-L852)
[GNU C 库匹配接口](https://sourceware.org/glibc/manual/latest/html_node/Matching-POSIX-Regexps.html)
### 单次 Unix 别名扫描共用固定目标的身份
固定路径的设备与 inode 观察只在同一轮扫描内共用。没有发现冲突时,
返回前重查固定路径,观察发生变化则用实时逐对比较重跑。真实单元测试
在检查两条候选之间,把固定路径原子替换为指向应保护文件的硬链接,
检查仍发现冲突。定向通过65项,包含已存在的硬链接、保护路径、生成
文件、实际错误和取消。实际项目的新一轮扫描重新获取身份。
在同样的24次真实主文件编辑中,`statx` 调用 61992 → 32664,其中
别名检查 61296 → 31968,别名查询本身合计 242.1 → 134.0 ms。
候选调用栈在 `target/p4-alias-scan-identity/statx/original-a4/statx-summary.json`,
SHA-256 为 `d2fa1a80f378445e1dccc29378cfc1546d94e823a49f460bffda827ba5790b0d`。
两轮 Edge 各做每版30次主文件和30次子文件编辑。第一轮每文件的正文
版本都不同,第二轮是往返编辑,调整首测顺序。每轮同格式、字体与
输入版本,两版的实际执行和复用步数、PDF、SVG、来源映射、最终 aux
对齐。两轮最后的编辑缓冲区用同版本另开零复用全量,各自输出一致。
| 场景 | 准备 p50 基线 → 候选 (ms) | 可见 p95 基线 → 候选 (ms) |
| --- | ---: | ---: |
| 不同正文主文件 | 17.8 → 13.3 | 219.2 → 224.0 |
| 不同正文子文件 | 18.6 → 13.3 | 293.3 → 205.0 |
| 往返主文件 | 17.2 → 13.6 | 209.5 → 208.6 |
| 往返子文件 | 17.0 → 13.2 | 210.5 → 244.5 |
准备耗时改善复现,两轮可见 p95 的方向有变化,100 ms端到端目标仍待
达到。峰值 RSS 在两轮分别从603280降到601528、591968降到590028 KiB。
两个浏览器报告 `edge-a4/run-{1,2}/receipt.json` SHA-256 为
`dec5ad9d3d48960147a3d2957a1061e9625d7f032841acc927463d3cab0db1eb`、
`5a4b5d454d06bf1313af37f61231935e9bf6a17a0a2d64a39cd10a1256119a80`。
真实 A3 手动和共享编译器两项也已通过,共享续跑仍只更新第7页。
回执 `a3-shared/test.log` 与 `a3-manual/test.log` SHA-256 为
`34fd27652fb0afb59deaee5b68c0dcede8bd2d8e38460746031f134d698683f6`、
`f4c65f08d98a2c15544dde3f3bd82cc57f7f139fbd50782541a04e65cc407d03`。
完整工作区和原样模板复验在同一补丁上运行。
为验证可停止的匹配方式,用 GNU 正则子进程独立测量管道往返。6个对照
覆盖最长选择、反向引用、字节位置、大小写、无匹配和模式错误。60次
短模式往返 p50 0.246 ms,p95 0.372 ms。受控阻塞时,终止并回收 worker
耗时1.360 ms,旧作业未产生匹配答复,随后新的 helper 返回实际匹配。
`target/p3-pdfmatch-worker-probe/receipt.json` 的 SHA-256 为
`492bd9a411e6547b41bf294061e45c8c79567107e4f8d959e92a2843a5cf170a`。
目前只完成独立进程探针,编译会话接线仍待验收。
### TeX 输入的默认扩展名候选
默认 TeX 编译在每个搜索目录中为带未知后缀的输入先试附加 `.tex`,并保留
+65
View File
@@ -91,6 +91,13 @@ pub struct HostPreparation {
#[serde(default)]
pub generated_initial: Option<crate::native_generated::GeneratedInitialState>,
}
#[cfg(unix)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum UnixPathIdentity {
Present(u64, u64),
Missing,
}
impl HostPreparation {
pub(crate) fn environment(&mut self, name: &str) -> Option<OsString> {
let value = std::env::var_os(name);
@@ -141,6 +148,64 @@ impl HostPreparation {
}
pub(crate) fn same_file(&mut self, a: &Path, b: &Path) -> bool {
let same = a == b || self.same_file_target(a, b);
self.record_alias(a, b, same)
}
pub(crate) fn aliases_any<'a>(
&mut self,
path: &Path,
candidates: impl Iterator<Item = &'a Path> + Clone,
) -> bool {
#[cfg(unix)]
{
let mut candidates = candidates.peekable();
let Some(first) = candidates.peek().copied() else {
return false;
};
if path == first {
return self.same_file(first, path);
}
if let Some(identity) = self.path_identity(path) {
let same = candidates.clone().any(|candidate| {
let alias = if candidate == path {
true
} else if identity == UnixPathIdentity::Missing {
false
} else if let Some(candidate_identity) = self.path_identity(candidate) {
identity == candidate_identity
} else {
return self.same_file(candidate, path);
};
self.record_alias(candidate, path, alias)
});
// Keep observations local to this scan. If the fixed path
// changed during a negative scan, use fresh pairwise probes.
if same || self.path_identity(path) == Some(identity) {
return same;
}
}
return candidates.any(|candidate| self.same_file(candidate, path));
}
#[cfg(not(unix))]
{
let mut candidates = candidates;
candidates.any(|candidate| self.same_file(candidate, path))
}
}
#[cfg(unix)]
fn path_identity(&mut self, path: &Path) -> Option<UnixPathIdentity> {
use std::os::unix::fs::MetadataExt;
match self.metadata(path) {
Ok(metadata) => Some(UnixPathIdentity::Present(metadata.dev(), metadata.ino())),
Err(error) if error.kind() == io::ErrorKind::NotFound => {
Some(UnixPathIdentity::Missing)
}
Err(_) => None,
}
}
fn record_alias(&mut self, a: &Path, b: &Path, same: bool) -> bool {
self.observations.push(HostObservation::Alias {
left: a.into(),
right: b.into(),
+58 -31
View File
@@ -317,9 +317,13 @@ impl Files {
|| resource.path == pdf
|| existing_log.as_ref() == Some(&resource.path)
|| existing_output.as_ref() == Some(&resource.path)
|| preparation.same_file(&resource.path, &log_path)
|| preparation.same_file(&resource.path, &pdf)
})
|| preparation.aliases_any(
&log_path,
world.trace().reads.iter().map(|r| r.path.as_path()),
)
|| preparation
.aliases_any(&pdf, world.trace().reads.iter().map(|r| r.path.as_path()))
|| preparation.same_file(source, &log_path)
|| preparation.same_file(&pdf, &log_path)
{
@@ -354,9 +358,8 @@ impl Files {
protected.extend(project.immutable_paths().map(Path::to_path_buf));
protected.sort();
protected.dedup();
if project
.immutable_paths()
.any(|p| preparation.same_file(p, &pdf) || preparation.same_file(p, &log_path))
if preparation.aliases_any(&pdf, project.immutable_paths())
|| preparation.aliases_any(&log_path, project.immutable_paths())
{
return Err("native output would overwrite an atomic project input".into());
}
@@ -365,11 +368,8 @@ impl Files {
preparation.same_file(p, source)
|| preparation.same_file(p, &pdf)
|| preparation.same_file(p, &log_path)
|| world
.trace()
.reads
.iter()
.any(|r| preparation.same_file(p, &r.path))
|| preparation
.aliases_any(p, world.trace().reads.iter().map(|r| r.path.as_path()))
}) {
return Err(
"native artifact metadata must differ from protected inputs and outputs".into(),
@@ -408,10 +408,13 @@ impl Files {
let immutable_claims: Vec<_> = ledger
.paths()
.filter(|path| {
protected
.iter()
.chain(std::iter::once(&log_path))
.any(|p| preparation.same_file(p, path))
preparation.aliases_any(
path,
protected
.iter()
.chain(std::iter::once(&log_path))
.map(PathBuf::as_path),
)
})
.map(Path::to_owned)
.collect();
@@ -423,11 +426,13 @@ impl Files {
.map(|p| p.generated().map(Path::to_path_buf).collect())
.unwrap_or_default();
for path in declared {
if protected
.iter()
.chain(std::iter::once(&log_path))
.any(|p| preparation.same_file(p, &path))
{
if preparation.aliases_any(
&path,
protected
.iter()
.chain(std::iter::once(&log_path))
.map(PathBuf::as_path),
) {
return Err("atomic generated role aliases a protected input or output".into());
}
let observed = file_version(&path);
@@ -848,10 +853,13 @@ impl Files {
Ok(())
}
fn is_protected(&self, path: &Path) -> bool {
self.protected
.iter()
.chain(std::iter::once(&self.log_path))
.any(|p| self.same_file(p, path))
self.runtime_observations.borrow_mut().aliases_any(
path,
self.protected
.iter()
.chain(std::iter::once(&self.log_path))
.map(PathBuf::as_path),
)
}
fn observe_resource(&mut self, kind: ResourceKind, resource: &LoadedResource) {
let path = &resource.version.path;
@@ -1511,10 +1519,7 @@ impl SourceProvider for Files {
Ok(resource)
}
fn file_size(&mut self, name: &str, from: &str) -> Result<Option<u64>, String> {
let Some(resource) = self
.world
.read_tex_input_optional(name, Path::new(from))?
else {
let Some(resource) = self.world.read_tex_input_optional(name, Path::new(from))? else {
return Ok(None);
};
self.observe_resource(ResourceKind::Tex, &resource);
@@ -1526,10 +1531,7 @@ impl SourceProvider for Files {
fn open_in(&mut self, stream: u8, name: &str, from: &str) -> Result<Option<String>, String> {
self.release_input(stream, ReleaseCause::Replaced, false);
let result = (|| -> Result<Option<InputOpened>, String> {
let Some(path) = self
.world
.resolve_tex_input(name, Path::new(from))?
else {
let Some(path) = self.world.resolve_tex_input(name, Path::new(from))? else {
return Ok(None);
};
let (reader, storage): (Box<dyn BufRead + Send>, InputStorage) =
@@ -2628,6 +2630,31 @@ mod generated_file_tests {
assert!(p.ledger.as_ref().unwrap().version.is_none());
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn scanning_aliases_rechecks_a_changed_target() {
let root = root();
let target = root.join("target");
let replacement = root.join("replacement");
let peer = root.join("main.tex");
let other = root.join("other");
fs::write(&target, b"original").unwrap();
fs::write(&other, b"other").unwrap();
fs::hard_link(&peer, &replacement).unwrap();
let switched = std::cell::Cell::new(false);
let candidates = [other.as_path(), peer.as_path()]
.into_iter()
.inspect(|candidate| {
if *candidate == peer && !switched.replace(true) {
fs::rename(&replacement, &target).unwrap();
}
});
let mut preparation = HostPreparation::default();
assert!(preparation.aliases_any(&target, candidates));
assert_eq!(fs::read(&target).unwrap(), fs::read(&peer).unwrap());
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn preparation_alias_rejection_records_actual_hardlink_identity() {