diff --git a/src/rust/RustBinaryImage.ts b/src/rust/RustBinaryImage.ts index 951a2ac368f..1ad908444a9 100644 --- a/src/rust/RustBinaryImage.ts +++ b/src/rust/RustBinaryImage.ts @@ -157,16 +157,15 @@ const rustcCommitFromReader = async ( pause: ZstandardFramePause, ): Promise => { const retention: RustcPathRetention = { - hashes: [], - seen: new Set(), + hashes: new Map(), maxHashes, maxBytes: maxOutputBytes, bytes: 2, - reason: undefined, + limit: undefined, }; for (const range of mergedFileRanges(spans)) { let cursor = range.offset; - while (cursor < range.end && retention.reason === undefined) { + while (cursor < range.end && retention.limit === undefined) { if (cursor > range.offset) await pause(); const length = Math.min(RUSTC_SCAN_WINDOW_BYTES, range.end - cursor); const chunk = await readByteSourceSlice(source, cursor, length); @@ -174,23 +173,16 @@ const rustcCommitFromReader = async ( if (cursor + length >= range.end) break; cursor += length - RUSTC_SCAN_OVERLAP_BYTES; } - if (retention.reason !== undefined) break; + if (retention.limit !== undefined) break; } - return retention.hashes.length === 0 + return retention.hashes.size === 0 ? null : { evidence_kind: "inferred", - hashes: retention.hashes, - ...(retention.reason !== undefined - ? { - coverage: rustcPartialCoverage( - retention.reason, - retention.reason === "report_bytes" - ? maxOutputBytes - : maxHashes, - ), - } - : {}), + hashes: [...retention.hashes.values()], + ...(retention.limit === undefined + ? {} + : { coverage: rustcPartialCoverage(...retention.limit) }), }; }; @@ -267,12 +259,11 @@ const dependencySection = ( /** One owner for compiler-path uniqueness, retained output and exhaustion. */ interface RustcPathRetention { - readonly hashes: { hash: string; file_offset: number }[]; - readonly seen: Set; + readonly hashes: Map; readonly maxHashes: number; readonly maxBytes: number; bytes: number; - reason: "max_rustc_hashes" | "report_bytes" | undefined; + limit: Parameters | undefined; } const mergedFileRanges = ( @@ -303,20 +294,19 @@ const collectHashesInChunk = ( if (at < 0) return; const start = at + RUSTC_PREFIX.length; const hash = hashAt(region, start); - if (hash !== null && !retention.seen.has(hash)) { - if (retention.seen.size >= retention.maxHashes) { - retention.reason = "max_rustc_hashes"; + if (hash !== null && !retention.hashes.has(hash)) { + if (retention.hashes.size >= retention.maxHashes) { + retention.limit = ["max_rustc_hashes", retention.maxHashes]; return; } const entry = { hash, file_offset: baseOffset + at }; const entryBytes = Buffer.byteLength(JSON.stringify(entry)) + 1; if (entryBytes > retention.maxBytes - retention.bytes) { - retention.reason = "report_bytes"; + retention.limit = ["report_bytes", retention.maxBytes]; return; } retention.bytes += entryBytes; - retention.seen.add(hash); - retention.hashes.push(entry); + retention.hashes.set(hash, entry); } cursor = at + 1; }