Skip to content
Merged
Changes from 1 commit
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 src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ pub fn List(T: type, comptime N: usize) type {
const alloc = allocator orelse return error.AllocatorRequired;
out.* = try init(alloc);

if (comptime Self.Item == u8) {
// route through deserialize's existing []u8 fast path
if (serialized.len > N) return error.OffsetExceedsSize;
try out.inner.resize(alloc, serialized.len);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is tricky, because if the data isn't already allocated in the slice, then the resizing will incur a copy of everything that came before the deserialization. There are therefore cases in which allocating the data in deserialize by passing an allocator is the better move.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copying over from TG.

out is initialized as empty. so resize will be a fresh new allocation. Also if we let deserialize do the allocation, ArrayList will be weirdly initialized. capacity field will be not set properly, no?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the problem was really that there is an allocation site that is optional, and because the array list has its own allocator, it was really redundant. But if you look at it, the "fast path" is to just call the @memcopy function. Might as well save a lot of clutter and just do the copy directly here. LMK what you think

return deserialize([]u8, serialized, &out.inner.items, null);
}

// FastSSZ-style capacity optimization: pre-allocate based on input size
// TODO: replace this with the definite value, taken from the list
if (serialized.len > 0) {
Expand Down
Loading