Skip to content

Use copy_nonoverlapping in allocator realloc#248

Closed
malik672 wants to merge 1 commit into
leanEthereum:mainfrom
malik672:overlap
Closed

Use copy_nonoverlapping in allocator realloc#248
malik672 wants to merge 1 commit into
leanEthereum:mainfrom
malik672:overlap

Conversation

@malik672

@malik672 malik672 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Use std::ptr::copy_nonoverlapping in zk-alloc's realloc path.

Why

For a growth realloc, the old allocation and the new allocation should not overlap, so copy_nonoverlapping is the more precise primitive.

Assembly note

copy_nonoverlapping lowers to memcpy, while copy lowers to memmove.

example::no_overlap::h1eef91d99543e88e:
  jmp qword ptr [rip + memcpy@GOTPCREL]

example::overlap_ok::hada851ac7e81e4a4:
  jmp qword ptr [rip + memmove@GOTPCREL]

non_overlapping

and overlapping:

example::realloc_style_copy::h30efdff70a615df7:
        mov     rax, rdi
        mov     rdi, rsi
        mov     rsi, rax
        jmp     qword ptr [rip + memmove@GOTPCREL]

example::realloc_style_copy_no_overlap::hcad6957ed56441d2:
        mov     rax, rdi
        mov     rdi, rsi
        mov     rsi, rax
        jmp     qword ptr [rip + memcpy@GOTPCREL]

Validation

cargo test -p zk-alloc

@TomWambsgans

Copy link
Copy Markdown
Collaborator

Thanks! The reasoning is correct, but zk-alloc no longer has a global allocator (removed in #247) — the realloc path moved to ArenaVec::realloc_to, which already uses copy_nonoverlapping. So this is already in main.

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.

2 participants