perf(firmware): reuse HEX/SREC scratch buffer and eliminate OTA extent cloning (#13633)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 21:48:35 +00:00
1 parent 33ca1be61c
commit b3aaa3da52
3 files changed
+57 -33

No files matched your search

+18 -20
View File
@@ -1068,31 +1068,29 @@ export const parseAndroidOtaPayload = (
};
const partitions: AndroidOtaPartition[] = manifest.value.partitions.map(
(partition, index) => {
const operations: AndroidOtaInstallOperation[] = partition.operations.map(
(operation) => ({
const sourceOps = partition.operations;
const operations = new Array<AndroidOtaInstallOperation>(
sourceOps.length,
);
let needsBlob = false;
let blobFits = true;
for (let opIndex = 0; opIndex < sourceOps.length; opIndex += 1) {
const operation = sourceOps[opIndex]!;
if (operation.dataLength > 0) {
needsBlob = true;
if (operation.dataOffset + operation.dataLength > blobAvailable)
blobFits = false;
}
operations[opIndex] = {
typeCode: operation.typeCode,
typeName: INSTALL_OPERATION_NAMES[operation.typeCode] ?? "unknown",
dataOffset: operation.dataOffset,
dataLength: operation.dataLength,
dataHashHex: operation.dataHashHex,
srcExtents: operation.srcExtents.map((extent) => ({
startBlock: extent.startBlock,
numBlocks: extent.numBlocks,
})),
dstExtents: operation.dstExtents.map((extent) => ({
startBlock: extent.startBlock,
numBlocks: extent.numBlocks,
})),
}),
);
const needsBlob = operations.some(
(operation) => operation.dataLength > 0,
);
const blobFits = operations.every(
(operation) =>
operation.dataLength === 0 ||
operation.dataOffset + operation.dataLength <= blobAvailable,
);
srcExtents: operation.srcExtents,
dstExtents: operation.dstExtents,
};
}
const size = partition.newPartitionInfo.size;
const extractRefusal: AndroidOtaExtractRefusal | null = !isExtractable(
partition.operations,
+21 -7
View File
@@ -97,6 +97,7 @@ export const trimTrailingBlanks = (
const decodeHexDigits = (
line: Uint8Array,
from: number,
scratch?: Uint8Array,
): Result<Uint8Array, string> => {
for (let column = from; column < line.length; column += 1)
if (HEX_VALUES[line[column] ?? 0] === -1)
@@ -105,8 +106,12 @@ const decodeHexDigits = (
);
if ((line.length - from) % 2 !== 0)
return err(`odd number of hex digits (${String(line.length - from)})`);
const bytes = new Uint8Array((line.length - from) / 2);
for (let index = 0; index < bytes.length; index += 1)
const byteLength = (line.length - from) / 2;
const bytes =
scratch !== undefined && scratch.length >= byteLength
? scratch.subarray(0, byteLength)
: new Uint8Array(byteLength);
for (let index = 0; index < byteLength; index += 1)
bytes[index] =
(HEX_VALUES[line[from + index * 2] ?? 0] ?? 0) * 16 +
(HEX_VALUES[line[from + index * 2 + 1] ?? 0] ?? 0);
@@ -122,9 +127,12 @@ const byteSum = (bytes: Uint8Array, end = bytes.length): number => {
const startCharacterFailure = (line: Uint8Array, expected: string): string =>
`invalid character 0x${hex(line[0] ?? 0, 2)} at column 1; a record starts with '${expected}'`;
const parseIntelRecord = (line: Uint8Array): Result<HexRecord, string> => {
const parseIntelRecord = (
line: Uint8Array,
scratch?: Uint8Array,
): Result<HexRecord, string> => {
if (line[0] !== COLON) return err(startCharacterFailure(line, ":"));
const decoded = decodeHexDigits(line, 1);
const decoded = decodeHexDigits(line, 1, scratch);
if (!decoded.ok) return decoded;
const bytes = decoded.value;
if (bytes.length < 5)
@@ -163,7 +171,10 @@ const parseIntelRecord = (line: Uint8Array): Result<HexRecord, string> => {
});
};
const parseSrecRecord = (line: Uint8Array): Result<HexRecord, string> => {
const parseSrecRecord = (
line: Uint8Array,
scratch?: Uint8Array,
): Result<HexRecord, string> => {
if (line[0] !== LETTER_S) return err(startCharacterFailure(line, "S"));
if (line.length < 2) return err("record has no type character");
const digit = (line[1] ?? 0) - 0x30;
@@ -172,7 +183,7 @@ const parseSrecRecord = (line: Uint8Array): Result<HexRecord, string> => {
`invalid record type character 0x${hex(line[1] ?? 0, 2)} at column 2`,
);
const type = `S${String(digit)}`;
const decoded = decodeHexDigits(line, 2);
const decoded = decodeHexDigits(line, 2, scratch);
if (!decoded.ok) return decoded;
const bytes = decoded.value;
const count = bytes[0] ?? 0;
@@ -202,5 +213,8 @@ const parseSrecRecord = (line: Uint8Array): Result<HexRecord, string> => {
export const parseRecord = (
encoding: HexRecordEncoding,
line: Uint8Array,
scratch?: Uint8Array,
): Result<HexRecord, string> =>
encoding === "intel-hex" ? parseIntelRecord(line) : parseSrecRecord(line);
encoding === "intel-hex"
? parseIntelRecord(line, scratch)
: parseSrecRecord(line, scratch);
+18 -6
View File
@@ -106,6 +106,7 @@ const formatFailure = (line: number, detail: string): RegionDecodeFailure => ({
class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
readonly #encoding: HexRecordEncoding;
readonly #line: Uint8Array;
readonly #scratch: Uint8Array;
readonly #runs: RecordRun[] = [];
#arena: Uint8Array;
#arenaWriteOffset = 0;
@@ -127,7 +128,9 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
constructor(encoding: HexRecordEncoding, fileBytes: number) {
this.#encoding = encoding;
// One extra byte holds the CR of a CRLF line at the maximum length.
this.#line = new Uint8Array(maxLineChars(encoding) + 1);
const maxChars = maxLineChars(encoding);
this.#line = new Uint8Array(maxChars + 1);
this.#scratch = new Uint8Array(Math.ceil(maxChars / 2));
this.#arena = new Uint8Array(Math.floor(fileBytes / 2));
}
@@ -182,7 +185,11 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
return err(this.#overlong(this.#lineNumber));
length = trimTrailingBlanks(this.#line, length);
if (length === 0) return ok(undefined);
const parsed = parseRecord(this.#encoding, this.#line.subarray(0, length));
const parsed = parseRecord(
this.#encoding,
this.#line.subarray(0, length),
this.#scratch,
);
if (!parsed.ok) return err(this.#failure(parsed.error));
if (this.#termination !== undefined)
return err(
@@ -201,7 +208,6 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
}
#applyIntel(record: HexRecord): Result<void, RegionDecodeFailure> {
const value = readBigEndian(record.data);
switch (record.type) {
case "00":
return this.#intelData(record);
@@ -209,18 +215,24 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
this.#termination = { value: record.type, line: this.#lineNumber };
return ok(undefined);
case "02":
case "04":
case "04": {
const value = readBigEndian(record.data);
this.#segmentRecords ||= record.type === "02";
this.#linearRecords ||= record.type === "04";
this.#baseFromSegment = record.type === "02";
this.#base = record.type === "02" ? value * 16 : value * 0x1_0000;
return ok(undefined);
case "03":
}
case "03": {
const value = readBigEndian(record.data);
return this.#declareStartSegment([value >>> 16, value & 0xffff]);
default:
}
default: {
// "05": the start linear address.
const value = readBigEndian(record.data);
this.#linearRecords = true;
return this.#declareStart(value, record.type);
}
}
}