refactor(apple): share keyed archive field traversal order (#16992)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 16:12:15 +08:00
1 parent d0bab9178a
commit ff9994f7ea
1 file changed
+15 -19
+15 -19
View File
@@ -207,12 +207,22 @@ export const projectKeyedArchive = (
: { [selection.root]: top[selection.root] ?? null };
const references: z.infer<typeof linkSchema>[] = [];
type Pending = { value: JsonValue; path: (string | number)[] };
const enqueueFields = (
fields: Record<string, JsonValue>,
path: Pending["path"],
pending: Pending[] = [],
): Pending[] => {
for (const key of Object.keys(fields).sort().reverse()) {
const value = fields[key];
if (value !== undefined) pending.push({ value, path: [...path, key] });
}
return pending;
};
// Nesting needs no bound here: projectPlistValue above refuses the whole
// archive past MAX_PLIST_DEPTH, and every scanned value sits at least two
// levels below the archive root ($top or $objects, then its key or index).
// `initial` is popped from the end, so callers list it in reverse order.
const scan = (source: number | null, initial: Pending[]) => {
const pending = initial;
// The queue is popped from the end, so enqueue in reverse order.
const scan = (source: number | null, pending: Pending[]) => {
let visited = 0;
while (pending.length > 0) {
const item = pending.pop();
@@ -253,12 +263,7 @@ export const projectKeyedArchive = (
}
} else {
const fields = record(item.value, wrappedScalars);
if (fields !== undefined)
for (const key of Object.keys(fields).sort().reverse()) {
const child = fields[key];
if (child !== undefined)
pending.push({ value: child, path: [...item.path, key] });
}
if (fields !== undefined) enqueueFields(fields, item.path, pending);
}
if (references.length > MAX_REFERENCE_EDGES)
throw decodeLimit(
@@ -275,16 +280,7 @@ export const projectKeyedArchive = (
// that wrapper makes a sole scalar root named `UID` look like a UID marker.
// Seed one scan with each value under its key to preserve paths and share
// the existing aggregate visit budget across every root.
scan(
null,
Object.keys(roots)
.sort()
.reverse()
.flatMap((key) => {
const value = roots[key];
return value === undefined ? [] : [{ value, path: [key] }];
}),
);
scan(null, enqueueFields(roots, []));
const objects = table.map((value, id) => {
scan(id, [{ value, path: [] }]);
const fields = record(value, wrappedScalars);