From c85e6d9513ff66f05afdfbec620fffd427ba3333 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 24 Oct 2026 02:39:03 +0800 Subject: [PATCH] refactor(native): retain contained-span mapping fast path --- src/native/metadata/BinaryContainer.ts | 30 +++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/native/metadata/BinaryContainer.ts b/src/native/metadata/BinaryContainer.ts index 3f2a6404f1..7f993c9df0 100644 --- a/src/native/metadata/BinaryContainer.ts +++ b/src/native/metadata/BinaryContainer.ts @@ -232,6 +232,7 @@ export const mappedFileOffset = ( const end = address + BigInt(size); let found: number | undefined; const covered: { start: bigint; end: bigint }[] = []; + let whollyCovered = size === 0; for (const mapping of mappings) { const mappingEnd = mapping.address + BigInt(mapping.size); const start = address > mapping.address ? address : mapping.address; @@ -249,30 +250,33 @@ export const mappedFileOffset = ( "Virtual span has ambiguous or noncontiguous file mappings", ); found = origin; - covered.push({ start, end: overlapEnd }); + if (start === address && overlapEnd === end) whollyCovered = true; + else if (!whollyCovered) covered.push({ start, end: overlapEnd }); } if (found === undefined) throw new BinaryFormatFailure( "malformed", "Virtual address or string range is not file mapped", ); - covered.sort((left, right) => - left.start < right.start ? -1 : left.start > right.start ? 1 : 0, - ); - let cursor = address; - for (const span of covered) { - if (span.start > cursor) + if (!whollyCovered) { + covered.sort((left, right) => + left.start < right.start ? -1 : left.start > right.start ? 1 : 0, + ); + let cursor = address; + for (const span of covered) { + if (span.start > cursor) + throw new BinaryFormatFailure( + "malformed", + "Virtual address or string range is not file mapped", + ); + if (span.end > cursor) cursor = span.end; + } + if (cursor < end) throw new BinaryFormatFailure( "malformed", "Virtual address or string range is not file mapped", ); - if (span.end > cursor) cursor = span.end; } - if (cursor < end) - throw new BinaryFormatFailure( - "malformed", - "Virtual address or string range is not file mapped", - ); for (const zeroFill of zeroFills) { if (spansOverlap(address, BigInt(size), zeroFill.address, zeroFill.size)) throw new BinaryFormatFailure(