Skip to content

add ssz clone#64

Merged
gballet merged 2 commits into
masterfrom
ssz-clone
May 29, 2026
Merged

add ssz clone#64
gballet merged 2 commits into
masterfrom
ssz-clone

Conversation

@anshalshukla
Copy link
Copy Markdown
Contributor

Adds clone(T, data, *T, ?Allocator) as a direct replacement for the serialize → deserialize round-trip previously used to deep-copy SSZ values. It produces an independent copy without any intermediate buffer, avoiding the encode/decode cost.

@anshalshukla anshalshukla requested a review from gballet as a code owner May 27, 2026 18:52
Copy link
Copy Markdown
Member

@g11tech g11tech left a comment

Choose a reason for hiding this comment

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

check if there there is a variable type size list, add it in the test case if not already

Copy link
Copy Markdown
Collaborator

@gballet gballet left a comment

Choose a reason for hiding this comment

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

I don't think this should be in this library, this is just a generic cloning feature. In fact, there is no need to call the function sszClone, a simple clone is enough. Then the semantics are the same for everything, because there is no reason for several clone functions to exist in any given object.

So I suggest moving all of that code to zeam, except the sszClone functions in utils.zig that should remain in this PR (which should be renamed clone).

Comment thread src/lib.zig Outdated
/// borrowed `[]const u8` data remains borrowed. Types providing `sszClone`
/// must also provide `deinit`; partial-failure unwinds rely on `deinit` to
/// release whatever the hook allocated.
pub fn clone(T: type, data: T, cloned: *T, allocator: ?Allocator) !void {
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.

it's kind of weird, in terms of interface, that you need both an allocator and a pre-allocated output. I'd say: either allocate nothing or all of it.

Comment thread src/utils.zig Outdated
Comment on lines +39 to +40
pub fn sszClone(self: *const Self, cloned: *Self, allocator: ?Allocator) !void {
const alloc = allocator orelse return error.AllocatorRequired;
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.

ideally, the interface should be changed to:

Suggested change
pub fn sszClone(self: *const Self, cloned: *Self, allocator: ?Allocator) !void {
const alloc = allocator orelse return error.AllocatorRequired;
pub fn sszClone(self: *const Self, cloned: *Self, allocator: Allocator) !void {

Because we always have an allocation. In the case of objects that don't need an allocation, it's fine to pass a dummy allocator, or just not use the parameter. It's ok, if it makes the whole code easier to implement, then I won't die on this hill.

Comment thread src/utils.zig Outdated
}

/// Clones this bitlist's backing storage.
pub fn sszClone(self: *const Self, cloned: *Self, allocator: ?Allocator) !void {
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.

as mentioned in the general review comment, this should simply be called clone

@gballet
Copy link
Copy Markdown
Collaborator

gballet commented May 29, 2026

Forgot to add: the code looks good to me, but most of it shouldn't be in the ssz lib

@gballet gballet merged commit 6164a98 into master May 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants