@davideasden/pi-undo 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -1
- package/package.json +1 -1
- package/src/restore-engine.ts +37 -10
package/README.md
CHANGED
|
@@ -290,7 +290,54 @@ Benchmark tests assert Git call counts and WAL record counts, not wall-clock thr
|
|
|
290
290
|
| 100-file restore (delete) — batch deletes | ≤12 | 600 | ~0.6s |
|
|
291
291
|
| 4,000-file rollback snapshot — batch Git | ≤24 (4 x `mktree`, 4 x `commit-tree`) | — | ~7s |
|
|
292
292
|
|
|
293
|
-
The 104-file standalone apply probe (10 warmup iterations + 10 measured) completes in approximately 3.9s post-optimization on the TypeScript path
|
|
293
|
+
The 104-file standalone apply probe (10 warmup iterations + 10 measured) completes in approximately 3.9s post-optimization on the TypeScript path, and approximately 2.8s on the native Rust path.
|
|
294
|
+
|
|
295
|
+
### Performance Optimizations
|
|
296
|
+
|
|
297
|
+
Several hot paths were profiled and corrected from quadratic (n doubled ≈ 4× cost) to near-linear scaling. Each change was verified against the existing test suite (425+ tests) plus fault-injection and adversarial-diff tests. Optimization order was guided by real profile data rather than static code review alone. After this phase, attention shifted to the native Rust helper path, which dominates production workloads.
|
|
298
|
+
|
|
299
|
+
#### Wall-clock comparison (n changed files + n ignored files)
|
|
300
|
+
|
|
301
|
+
**Plan phase** — undo diff derivation and workspace topology check:
|
|
302
|
+
|
|
303
|
+
| n | Before | After | Improvement |
|
|
304
|
+
|---|---|---|---|
|
|
305
|
+
| 1,000 | ~178ms | ~131ms | 1.4× |
|
|
306
|
+
| 2,000 | ~399ms | ~259ms | 1.5× |
|
|
307
|
+
| 5,000 | ~2.5s | ~699ms | ~3.6× |
|
|
308
|
+
|
|
309
|
+
**Bidirectional durable pack prepare** — agent-settled caching for undo/redo:
|
|
310
|
+
|
|
311
|
+
| n | Before (no batch) | After (batched) | Improvement |
|
|
312
|
+
|---|---|---|---|
|
|
313
|
+
| 500 | ~949ms | ~249ms | 3.8× |
|
|
314
|
+
| 1,000 | ~2,940ms | ~386ms | 7.6× |
|
|
315
|
+
| 2,000 | ~10,512ms | ~677ms | 15.5× |
|
|
316
|
+
|
|
317
|
+
**Native apply** — the production hot path:
|
|
318
|
+
|
|
319
|
+
| n | TypeScript fallback | Native Rust helper | Improvement |
|
|
320
|
+
|---|---|---|---|
|
|
321
|
+
| 500 | ~2,572ms | ~253ms | 10.2× |
|
|
322
|
+
| 1,000 | ~5,726ms | ~374ms | 15.3× |
|
|
323
|
+
| 2,000 | ~10,521ms | ~646ms | 16.3× |
|
|
324
|
+
|
|
325
|
+
#### Algorithm microbenchmarks
|
|
326
|
+
|
|
327
|
+
| Scenario | Before | After | Improvement |
|
|
328
|
+
|---|---|---|---|
|
|
329
|
+
| 8,000×8,000 path overlap check | ~1,352ms | ~7ms | **193×** |
|
|
330
|
+
| 4,000-directory ignored prefix scan (plan) | ~544ms | ~161ms | **3.4×** |
|
|
331
|
+
| 5,000×5,000 manifest integrity verify | ~610ms | ~7ms | **87×** |
|
|
332
|
+
|
|
333
|
+
#### Key enablers
|
|
334
|
+
|
|
335
|
+
- **Manifest context batching** (`SnapshotStore.readBlobs`): one manifest read and full revalidation serves all blob fetches per operation instead of one manifest load per blob. 2,000-file pack creation dropped from 10.5s to 796ms.
|
|
336
|
+
- **Blob read batch sizing** (`BLOB_BATCH_MAX_ENTRIES`: 256 → 2,048): 40 `cat-file --batch` processes collapsed to 6 for 5,000 files by letting the 16MiB byte budget drive batch boundaries.
|
|
337
|
+
- **Path overlap indexing** (`pathSetsOverlap` in `path-safety.ts`): sorted binary search plus ancestor enumeration replaces a nested `some()` pattern. 8,000×8,000 path pairs from 1.35s to 7ms.
|
|
338
|
+
- **Ignored-proof prefix index** (`IgnoredProofIndex` in `restore-engine.ts`): lazily sorted binary lower-bound search for directory prefix lookups replaces a full-set spread per candidate. 4,000 directories from 544ms to 161ms.
|
|
339
|
+
- **WAL ordinal indexing** (`loadOrdinal` in `mutation-journal.ts`): direct array access by contiguous ordinal replaces linear `.find()` throughout quarantine and legacy recovery.
|
|
340
|
+
- **Native Rust no-clobber helper** (`native/pi-undo-fs`): platform native hardlink operations with EEXIST guard. Processes in ~0.3ms per file vs ~5.3ms for the TypeScript fallback, with WAL materialized from the durable pack.
|
|
294
341
|
|
|
295
342
|
## License
|
|
296
343
|
|
package/package.json
CHANGED
package/src/restore-engine.ts
CHANGED
|
@@ -169,7 +169,7 @@ export class RestoreEngine {
|
|
|
169
169
|
this.readOwnedPaths(current, canonicalScopePaths),
|
|
170
170
|
this.readOwnedPaths(target, canonicalScopePaths),
|
|
171
171
|
]);
|
|
172
|
-
const
|
|
172
|
+
const targetIgnoredProof = new IgnoredProofIndex(ignoredWorkspacePaths(target));
|
|
173
173
|
const deleteByRoot = new Map<string, string[]>();
|
|
174
174
|
const writeByRoot = new Map<string, string[]>();
|
|
175
175
|
|
|
@@ -183,7 +183,7 @@ export class RestoreEngine {
|
|
|
183
183
|
continue;
|
|
184
184
|
}
|
|
185
185
|
if (targetOwned === undefined) {
|
|
186
|
-
if (
|
|
186
|
+
if (targetIgnoredProof.isProtected(path, owned.entry.kind)) {
|
|
187
187
|
continue;
|
|
188
188
|
}
|
|
189
189
|
appendPath(deleteByRoot, owned.root.relativeRoot, path);
|
|
@@ -1728,15 +1728,42 @@ function ignoredWorkspacePaths(manifest: SnapshotManifest): Set<string> {
|
|
|
1728
1728
|
));
|
|
1729
1729
|
}
|
|
1730
1730
|
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1731
|
+
/**
|
|
1732
|
+
* ignored 证明的前缀索引。
|
|
1733
|
+
*
|
|
1734
|
+
* 目录判定需要回答"是否存在以 `${path}/` 开头的 ignored 路径"。逐次线性扫描
|
|
1735
|
+
* 整个 set 会让"目录数 × ignored 数"变成平方项,因此这里预排序一次,
|
|
1736
|
+
* 之后每次判定用二分下界定位第一个不小于前缀的元素。
|
|
1737
|
+
*
|
|
1738
|
+
* 语义与线性扫描完全一致:只回答存在性,不改变 fail-closed 行为,也不放宽
|
|
1739
|
+
* 任何 ignored 保护。非目录仍走精确 `has()`。
|
|
1740
|
+
*/
|
|
1741
|
+
class IgnoredProofIndex {
|
|
1742
|
+
private readonly exact: ReadonlySet<string>;
|
|
1743
|
+
private sortedPaths: readonly string[] | undefined;
|
|
1744
|
+
|
|
1745
|
+
constructor(paths: ReadonlySet<string>) {
|
|
1746
|
+
this.exact = paths;
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
isProtected(path: string, kind: RestorePath["kind"]): boolean {
|
|
1750
|
+
if (kind !== "directory") {
|
|
1751
|
+
return this.exact.has(path);
|
|
1752
|
+
}
|
|
1753
|
+
if (this.exact.size === 0) return false;
|
|
1754
|
+
// 排序成本只在第一次目录判定时付出,纯文件计划完全不触发。
|
|
1755
|
+
this.sortedPaths ??= [...this.exact].sort();
|
|
1756
|
+
const sorted = this.sortedPaths;
|
|
1757
|
+
const prefix = `${path}/`;
|
|
1758
|
+
let low = 0;
|
|
1759
|
+
let high = sorted.length;
|
|
1760
|
+
while (low < high) {
|
|
1761
|
+
const middle = (low + high) >>> 1;
|
|
1762
|
+
if (sorted[middle]! < prefix) low = middle + 1;
|
|
1763
|
+
else high = middle;
|
|
1764
|
+
}
|
|
1765
|
+
return low < sorted.length && sorted[low]!.startsWith(prefix);
|
|
1738
1766
|
}
|
|
1739
|
-
return [...ignoredPaths].some((ignoredPath) => ignoredPath.startsWith(`${path}/`));
|
|
1740
1767
|
}
|
|
1741
1768
|
|
|
1742
1769
|
function rootBoundaryDirectories(root: string): string[] {
|