perf(artifacts): stop parsing parent paths after a missing branch

Walk delimiter-terminated parent segments lazily instead of splitting full paths before discovering that no deeper observed parent exists. Preserve canonical paths and observed ancestry.

Verified real ZIP inventory with multiple 30000-component siblings and the full entry-order suite. A 96-member deep ZIP benchmark retained an identical full inventory digest and reduced median scan time from 517.5 to 464.5 ms across five measured inventories.
This commit is contained in:
morluto
2026-10-11 02:32:04 +08:00
parent acd8d86898
commit bb764d26bd
2 changed files with 25 additions and 16 deletions
@@ -325,15 +325,20 @@ export const resolveOccurrenceParents = (
branch.occurrence = occurrence; branch.occurrence = occurrence;
} }
for (const occurrence of occurrences) { for (const occurrence of occurrences) {
const segments = occurrence.logical_path.split("/"); const path = occurrence.logical_path;
let branch = root; let branch = root;
let parent: MutableOccurrence | undefined; let parent: MutableOccurrence | undefined;
// The final segment identifies this occurrence, not one of its parents. // Walk parent segments only. Stop scanning the path once no observed
for (const segment of segments.slice(0, -1)) { // directory or expanded container can be an ancestor.
for (let start = 0; ;) {
const slash = path.indexOf("/", start);
if (slash < 0) break;
const segment = path.slice(start, slash);
const child = branch.children.get(segment); const child = branch.children.get(segment);
if (child === undefined) break; if (child === undefined) break;
branch = child; branch = child;
parent = branch.occurrence ?? parent; parent = branch.occurrence ?? parent;
start = slash + 1;
} }
occurrence.parent_occurrence_id = parent?.occurrence_id ?? null; occurrence.parent_occurrence_id = parent?.occurrence_id ?? null;
} }
@@ -73,25 +73,29 @@ describe("artifact inventory entry order", () => {
const root = await createTestTempDirectory("rea-deep-archive-parent-"); const root = await createTestTempDirectory("rea-deep-archive-parent-");
const archive = join(root, "deep.zip"); const archive = join(root, "deep.zip");
// Fits the ZIP filename field while exceeding the formerly expensive depth. // Fits the ZIP filename field while exceeding the formerly expensive depth.
const member = `observed/${"d/".repeat(30_000)}data.txt`; const members = ["data.txt", "second.txt", "third.bin"].map(
await writeOrderedZip(archive, [member, "observed/", "unrelated/"]); (name) => `observed/${"d/".repeat(30_000)}${name}`,
);
await writeOrderedZip(archive, [...members, "observed/", "unrelated/"]);
const inventory = await inventoryArtifact(archive); const inventory = await inventoryArtifact(archive);
expect(artifactParentPaths(inventory)).toEqual({ expect(artifactParentPaths(inventory)).toEqual({
".": null, ".": null,
observed: ".", observed: ".",
unrelated: ".", unrelated: ".",
[member]: "observed", ...Object.fromEntries(members.map((member) => [member, "observed"])),
}); });
const occurrence = artifactOccurrenceAt(inventory, member); const observedParent = artifactOccurrenceAt(inventory, "observed");
expect(occurrence.hash_status).toBe("verified"); for (const member of members) {
expect(inventory.edges).toContainEqual( const occurrence = artifactOccurrenceAt(inventory, member);
expect.objectContaining({ expect(occurrence.hash_status).toBe("verified");
occurrence_id: occurrence.occurrence_id, expect(inventory.edges).toContainEqual(
parent_artifact_id: artifactOccurrenceAt(inventory, "observed") expect.objectContaining({
.artifact_id, occurrence_id: occurrence.occurrence_id,
relation: "contains", parent_artifact_id: observedParent.artifact_id,
}), relation: "contains",
); }),
);
}
}); });
it.each([ it.each([