From ff9994f7ea22d7003efb79dba93d4d39b1aac68c Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 5 Dec 2026 16:12:15 +0800 Subject: [PATCH] refactor(apple): share keyed archive field traversal order (#16992) --- src/domain/apple/keyedArchive.ts | 34 ++++++++++++++------------------ 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/domain/apple/keyedArchive.ts b/src/domain/apple/keyedArchive.ts index 91eb6c198c8..4b9464e03f5 100644 --- a/src/domain/apple/keyedArchive.ts +++ b/src/domain/apple/keyedArchive.ts @@ -207,12 +207,22 @@ export const projectKeyedArchive = ( : { [selection.root]: top[selection.root] ?? null }; const references: z.infer[] = []; type Pending = { value: JsonValue; path: (string | number)[] }; + const enqueueFields = ( + fields: Record, + 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);