Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/quick-arrays-decode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@solana/codecs-data-structures': patch
---

Avoid copying the remaining buffer in `getArrayDecoder`'s emptiness check

`getArrayDecoder`'s `read()` tested for an empty byte array with `bytes.slice(offset).length === 0`, which allocates and copies every byte from `offset` to the end just to read `.length` off the result. On large accounts containing many prefixed arrays, maps, or sets this made decoding quadratic in account size. The check is now the equivalent O(1) comparison `offset >= bytes.length`. `getMapDecoder` and `getSetDecoder` delegate to `getArrayDecoder` and benefit as well.
2 changes: 1 addition & 1 deletion packages/codecs-data-structures/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function getArrayDecoder<TTo>(item: Decoder<TTo>, config: ArrayCodecConfi
...(fixedSize !== null ? { fixedSize } : { maxSize }),
read: (bytes: ReadonlyUint8Array | Uint8Array, offset) => {
const array: TTo[] = [];
if (typeof size === 'object' && bytes.slice(offset).length === 0) {
if (typeof size === 'object' && offset >= bytes.length) {
return [array, offset];
}

Expand Down
Loading