perf(native): charge function matching for the copies it makes (#7108)

* perf(native): charge matching for what it copies, not borrowed retained inventories

Native function matching deep-walked every retained list_procedures record
at about 2.8 KB per Ghidra-shaped row, although the session ledger lends
those immutable records without a copy, and charged each inventory's one
Evidence ID once per function. The report and Evidence copies of every
emitted member were not charged at all.

Charge each inventory row before parsing for the member it becomes,
including its report and Evidence copies, charge the shared inventory ID
once, and skip the walk only for borrowed immutable inventories and closure
records that matching never parses. Inline Evidence, bundle records and
selected observations keep their existing charges. Retained references now
fit about 22,000 rows per side instead of 10,000, and an oversized side is
refused before its rows are parsed.

Refs #6406

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(native): charge the envelope of borrowed matching inputs

Borrowed inventories skipped admission entirely, but matching copies their
limitations into the report and Evidence and digests their profile, so a
retained inventory with few rows and large limitations escaped the
allowance. Charge every envelope field and leave only the result payloads,
which row charging covers or matching never parses, uncharged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822andClaude Opus 5.5 authored and GitHub committed 2026-10-25 05:21:09 -07:00
1 parent 58a5275aa2
commit cc2656be8d
5 files changed
+211 -13

No files matched your search

+25 -3
View File
@@ -239,7 +239,6 @@ const functionFact = async (
await checkpoint(256 + 8 * link.length);
for (const link of symbols?.links ?? [])
await checkpoint(256 + 8 * link.length);
await checkpoint(256 + 8 * inventory.evidence_id.length);
const links = new Set([inventory.evidence_id]);
for (const link of detail?.links ?? []) links.add(link);
for (const link of symbols?.links ?? []) links.add(link);
@@ -290,13 +289,36 @@ const functionFact = async (
};
};
const textLength = (value: unknown): number =>
typeof value === "string" ? value.length : 0;
/**
* Every row becomes exactly one emitted member: its parsed row and member
* (768 bytes), then the report validation and Evidence normalization copies
* that serialization digests (1280 bytes). Names and addresses are shared
* strings, charged for UTF-16 storage and serialized copies.
*/
const rowBytes = (row: unknown): number =>
typeof row === "object" && row !== null
? 2048 +
4 *
(textLength((row as { address?: unknown }).address) +
textLength((row as { value?: unknown }).value))
: 2048;
/** Consume application-admitted normalized facts; never infer body extents from neighbor entries. */
export const readFunctionInventory = async (
inventory: Evidence,
observations: readonly Evidence[],
checkpoint: NativeMatchingCheckpoint,
): Promise<MatchingFunction[]> => {
const rows = sourcePayload(matchingRowsSchema, inventory.normalized_result);
// Charge each row's member before parsing clones it, so a borrowed retained
// inventory is bounded without walking the resident record and an oversized
// side is refused before allocation. Members share one inventory ID string.
const source = inventory.normalized_result;
if (Array.isArray(source))
for (const row of source as unknown[]) await checkpoint(rowBytes(row));
await checkpoint(256 + 8 * inventory.evidence_id.length);
const rows = sourcePayload(matchingRowsSchema, source);
const facts: Facts = {
details: new Map(),
symbols: new Map(),
@@ -320,7 +342,7 @@ export const readFunctionInventory = async (
const functions: MatchingFunction[] = [];
for (let sourceIndex = 0; sourceIndex < rows.length; sourceIndex++) {
const row = rows[sourceIndex]!;
await checkpoint(1024 + 4 * (row.address.length + row.value.length));
await checkpoint();
functions.push(
await functionFact({ inventory, row, sourceIndex }, facts, checkpoint),
);