mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
perf(firmware): measure each literal-pool peripheral name once
The literal-pool representation pass recounted a peripheral name's UTF-8 bytes and built an `svd:` group key from it for every access, and grouping built that key again per access. With 16,384 loads of one 1 MiB name, a default call copied about 16 GiB of text and took about 20 s to refuse. Grouping now keys an index on the identity itself (peripheral name, block name or page), so a name is hashed once per string. The measurement pass caches each name's byte length. The same refusal now takes about 0.4 s. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
1 parent
f06019cf09
commit
3d41ed79ac
3 files changed
+129
-40
No files matched your search
@@ -14,6 +14,7 @@ import {
|
||||
import {
|
||||
EMPTY_SVD_PERIPHERAL_PROFILE,
|
||||
createConstantClassifier,
|
||||
type ConstantClassification,
|
||||
type SvdPeripheralProfile,
|
||||
} from "./firmwareLiteralPoolRegions.js";
|
||||
import {
|
||||
@@ -27,6 +28,7 @@ import {
|
||||
import {
|
||||
buildMmioPeripheralGroups,
|
||||
maxUnnamedGroupBytes,
|
||||
MmioGroupIndex,
|
||||
mmioGroupIdentity,
|
||||
} from "./firmwareMmioGroups.js";
|
||||
|
||||
@@ -228,9 +230,23 @@ interface CandidateScanContext {
|
||||
readonly mmioOnly: boolean;
|
||||
}
|
||||
|
||||
const classificationGroup = (
|
||||
value: number,
|
||||
classified: ConstantClassification,
|
||||
) =>
|
||||
mmioGroupIdentity({
|
||||
address: BigInt(value >>> 0),
|
||||
peripheralName: classified.svdPeripheral,
|
||||
peripheralBase:
|
||||
classified.svdPeripheral === null || classified.mmioBaseAddress === null
|
||||
? null
|
||||
: BigInt(classified.mmioBaseAddress),
|
||||
architecturalBlock: classified.architecturalBlock,
|
||||
});
|
||||
|
||||
/**
|
||||
* Measure the UTF-8 bytes of the access rows a collection pass would emit,
|
||||
* plus an upper bound for each peripheral group they form.
|
||||
* Measure the UTF-8 bytes of the access rows a collection pass would emit, plus an upper bound for each peripheral group they form. Name
|
||||
* byte lengths are computed once per name, not once per access.
|
||||
*/
|
||||
function* measureRepresentationBytesSteps(
|
||||
context: CandidateScanContext,
|
||||
@@ -241,7 +257,17 @@ function* measureRepresentationBytesSteps(
|
||||
fileBytes: context.fileBytes.length,
|
||||
svdProfile: context.svdProfile,
|
||||
});
|
||||
const groupKeys = new Set<string>();
|
||||
const nameBytes = new Map<string, number>();
|
||||
const utf8Length = (text: string | null): number => {
|
||||
if (text === null) return 0;
|
||||
let bytes = nameBytes.get(text);
|
||||
if (bytes === undefined) {
|
||||
bytes = Buffer.byteLength(text, "utf8");
|
||||
nameBytes.set(text, bytes);
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
const groups = new MmioGroupIndex<true>();
|
||||
let totalBytes = BASE_REPORT_JSON_BYTES;
|
||||
// not-an-allowance: cooperative byte accumulator for yielding during representation measurement
|
||||
let bytesSinceYield = 0;
|
||||
@@ -263,22 +289,15 @@ function* measureRepresentationBytesSteps(
|
||||
if (context.mmioOnly && !isMmio) continue;
|
||||
let rowBytes =
|
||||
BASE_ACCESS_JSON_BYTES +
|
||||
Buffer.byteLength(classified.svdPeripheral ?? "", "utf8") +
|
||||
Buffer.byteLength(classified.architecturalBlock ?? "", "utf8");
|
||||
utf8Length(classified.svdPeripheral) +
|
||||
utf8Length(classified.architecturalBlock);
|
||||
if (isMmio) {
|
||||
const { key } = mmioGroupIdentity({
|
||||
address: BigInt(loadedValue >>> 0),
|
||||
peripheralName: classified.svdPeripheral,
|
||||
peripheralBase:
|
||||
classified.svdPeripheral === null ||
|
||||
classified.mmioBaseAddress === null
|
||||
? null
|
||||
: BigInt(classified.mmioBaseAddress),
|
||||
architecturalBlock: classified.architecturalBlock,
|
||||
});
|
||||
if (!groupKeys.has(key)) {
|
||||
groupKeys.add(key);
|
||||
rowBytes += maxUnnamedGroupBytes(key);
|
||||
const identity = classificationGroup(loadedValue, classified);
|
||||
if (groups.get(identity) === undefined) {
|
||||
groups.set(identity, true);
|
||||
rowBytes += maxUnnamedGroupBytes(
|
||||
identity.kind === "page" ? 0 : utf8Length(identity.name),
|
||||
);
|
||||
}
|
||||
}
|
||||
totalBytes += rowBytes;
|
||||
|
||||
@@ -14,7 +14,7 @@ const BASE_GROUP_JSON_BYTES = 320;
|
||||
const PAGE_MASK = ~0xfffn;
|
||||
|
||||
interface GroupAccumulator {
|
||||
readonly groupKey: string;
|
||||
readonly nameBytes: number;
|
||||
baseAddress: bigint;
|
||||
readonly first: FirmwareMmioAccess;
|
||||
// not-an-allowance: observed accesses in this peripheral group
|
||||
@@ -30,24 +30,67 @@ interface GroupAccumulator {
|
||||
readonly samples: string[];
|
||||
}
|
||||
|
||||
/** Group key and base of one MMIO access: SVD peripheral, architectural block, or 4 KiB page. */
|
||||
/** Grouping identity of one MMIO access: SVD peripheral, architectural block, or 4 KiB page. */
|
||||
export type MmioGroupIdentity =
|
||||
| {
|
||||
readonly kind: "svd" | "block";
|
||||
readonly name: string;
|
||||
readonly base: bigint;
|
||||
}
|
||||
| { readonly kind: "page"; readonly base: bigint };
|
||||
|
||||
export const mmioGroupIdentity = (access: {
|
||||
readonly address: bigint;
|
||||
readonly peripheralName: string | null;
|
||||
readonly peripheralBase: bigint | null;
|
||||
readonly architecturalBlock: string | null;
|
||||
}): { readonly key: string; readonly base: bigint } => {
|
||||
}): MmioGroupIdentity => {
|
||||
if (access.peripheralName !== null && access.peripheralBase !== null) {
|
||||
return { key: `svd:${access.peripheralName}`, base: access.peripheralBase };
|
||||
return {
|
||||
kind: "svd",
|
||||
name: access.peripheralName,
|
||||
base: access.peripheralBase,
|
||||
};
|
||||
}
|
||||
const page = access.address & PAGE_MASK;
|
||||
if (access.architecturalBlock !== null) {
|
||||
return { key: `block:${access.architecturalBlock}`, base: page };
|
||||
}
|
||||
return { key: `page:${formatHexAddress(page)}`, base: page };
|
||||
return access.architecturalBlock === null
|
||||
? { kind: "page", base: page }
|
||||
: { kind: "block", name: access.architecturalBlock, base: page };
|
||||
};
|
||||
|
||||
const groupIdentity = (access: FirmwareMmioAccess) =>
|
||||
/**
|
||||
* Groups keyed by identity without building a string key per access, so a
|
||||
* long peripheral name is hashed once per string rather than copied per access.
|
||||
*/
|
||||
export class MmioGroupIndex<T> {
|
||||
readonly #named = { svd: new Map<string, T>(), block: new Map<string, T>() };
|
||||
readonly #pages = new Map<bigint, T>();
|
||||
|
||||
get(identity: MmioGroupIdentity): T | undefined {
|
||||
return identity.kind === "page"
|
||||
? this.#pages.get(identity.base)
|
||||
: this.#named[identity.kind].get(identity.name);
|
||||
}
|
||||
|
||||
set(identity: MmioGroupIdentity, value: T): void {
|
||||
if (identity.kind === "page") this.#pages.set(identity.base, value);
|
||||
else this.#named[identity.kind].set(identity.name, value);
|
||||
}
|
||||
|
||||
values(): T[] {
|
||||
return [
|
||||
...this.#named.svd.values(),
|
||||
...this.#named.block.values(),
|
||||
...this.#pages.values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/** UTF-8 bytes of the identifying name a group repeats in its row. */
|
||||
export const mmioGroupNameBytes = (identity: MmioGroupIdentity): number =>
|
||||
identity.kind === "page" ? 0 : Buffer.byteLength(identity.name, "utf8");
|
||||
|
||||
const groupAccess = (access: FirmwareMmioAccess) =>
|
||||
mmioGroupIdentity({
|
||||
address: BigInt(access.address),
|
||||
peripheralName: access.peripheral_name,
|
||||
@@ -57,15 +100,13 @@ const groupIdentity = (access: FirmwareMmioAccess) =>
|
||||
});
|
||||
|
||||
const groupBytes = (
|
||||
groupKey: string,
|
||||
nameBytes: number,
|
||||
shownAddresses: number,
|
||||
samples: number,
|
||||
registerNames: readonly string[],
|
||||
): number => {
|
||||
let bytes =
|
||||
BASE_GROUP_JSON_BYTES +
|
||||
Buffer.byteLength(groupKey, "utf8") * 2 +
|
||||
(shownAddresses + samples) * 22;
|
||||
BASE_GROUP_JSON_BYTES + nameBytes * 2 + (shownAddresses + samples) * 22;
|
||||
for (const name of registerNames) {
|
||||
bytes += Buffer.byteLength(name, "utf8") + 3;
|
||||
}
|
||||
@@ -73,9 +114,9 @@ const groupBytes = (
|
||||
};
|
||||
|
||||
/** Upper bound of one group's representation bytes when no register names are bound. */
|
||||
export const maxUnnamedGroupBytes = (groupKey: string): number =>
|
||||
export const maxUnnamedGroupBytes = (nameBytes: number): number =>
|
||||
groupBytes(
|
||||
groupKey,
|
||||
nameBytes,
|
||||
DISTINCT_ADDRESS_DISPLAY_COUNT,
|
||||
SAMPLE_ADDRESS_DISPLAY_COUNT,
|
||||
[],
|
||||
@@ -92,14 +133,14 @@ export const buildMmioPeripheralGroups = (
|
||||
accesses: readonly FirmwareMmioAccess[],
|
||||
charge: (bytes: number) => void,
|
||||
): FirmwareMmioPeripheralGroup[] => {
|
||||
const byKey = new Map<string, GroupAccumulator>();
|
||||
const index = new MmioGroupIndex<GroupAccumulator>();
|
||||
for (const access of accesses) {
|
||||
if (!isMmioRegionKind(access.region_kind)) continue;
|
||||
const identity = groupIdentity(access);
|
||||
let group = byKey.get(identity.key);
|
||||
const identity = groupAccess(access);
|
||||
let group = index.get(identity);
|
||||
if (group === undefined) {
|
||||
group = {
|
||||
groupKey: identity.key,
|
||||
nameBytes: mmioGroupNameBytes(identity),
|
||||
baseAddress: identity.base,
|
||||
first: access,
|
||||
accessCount: 0,
|
||||
@@ -110,7 +151,7 @@ export const buildMmioPeripheralGroups = (
|
||||
registerNames: new Set(),
|
||||
samples: [],
|
||||
};
|
||||
byKey.set(identity.key, group);
|
||||
index.set(identity, group);
|
||||
} else if (identity.base < group.baseAddress) {
|
||||
group.baseAddress = identity.base;
|
||||
}
|
||||
@@ -128,7 +169,7 @@ export const buildMmioPeripheralGroups = (
|
||||
}
|
||||
|
||||
const groups: FirmwareMmioPeripheralGroup[] = [];
|
||||
for (const group of byKey.values()) {
|
||||
for (const group of index.values()) {
|
||||
const distinct = [...group.addresses].sort(compareBigints);
|
||||
const shown = boundedList(
|
||||
distinct,
|
||||
@@ -139,7 +180,7 @@ export const buildMmioPeripheralGroups = (
|
||||
);
|
||||
charge(
|
||||
groupBytes(
|
||||
group.groupKey,
|
||||
group.nameBytes,
|
||||
shown.length,
|
||||
group.samples.length,
|
||||
registerNames,
|
||||
|
||||
@@ -355,4 +355,33 @@ describe("FirmwareMmioProvider literal-pool architectures", () => {
|
||||
expect(elapsedMs).toBeLessThan(1500);
|
||||
await provider.close();
|
||||
});
|
||||
|
||||
it("refuses 16384 loads of one 1 MiB SVD peripheral name without rescanning the name per access", async () => {
|
||||
const dir = await createTestTempDirectory("rea-literal-pools-svd-refuse-");
|
||||
const provider = new FirmwareMmioProvider();
|
||||
const binPath = join(dir, "many_svd_loads.bin");
|
||||
await writeFile(binPath, thumb2LiteralLoads(16_384));
|
||||
const megaName = "S".repeat(1024 * 1024);
|
||||
const svdXml = `<device><peripherals><peripheral><name>${megaName}</name><baseAddress>0x40000000</baseAddress><addressBlock><offset>0x0</offset><size>0x100</size><usage>registers</usage></addressBlock></peripheral></peripherals></device>`;
|
||||
|
||||
const started = performance.now();
|
||||
const res = await provider.inspect({
|
||||
...DEFAULTS,
|
||||
path: binPath,
|
||||
architecture: "arm-thumb",
|
||||
base_address: "0x08000000",
|
||||
svd_xml: svdXml,
|
||||
});
|
||||
const elapsedMs = performance.now() - started;
|
||||
await provider.close();
|
||||
|
||||
if (res.ok) throw new Error("expected representation refusal");
|
||||
expect(res.error.limit).toMatchObject({
|
||||
parameter: "max_representation_bytes",
|
||||
effective_value: DEFAULTS.max_representation_bytes,
|
||||
});
|
||||
// Measuring the name once per peripheral keeps refusal well under the
|
||||
// seconds that per-access byte counting and key copying of 16 GiB took.
|
||||
expect(elapsedMs).toBeLessThan(3000);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user