Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request 0xPolygonZero#1305 from topos-protocol/memset
Browse files Browse the repository at this point in the history
Speed-up memset and fix it to write 0 values
  • Loading branch information
Nashtare authored Oct 23, 2023
2 parents 0300a32 + 385ab3c commit 8af189b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/account_code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extcodecopy_large_offset:
// offset is larger than the code size. So we just have to write zeros.
// stack: code_size, size, offset, dest_offset, retdest
GET_CONTEXT
%stack (context, code_size, size, offset, dest_offset, retdest) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, 0, size, retdest)
%stack (context, code_size, size, offset, dest_offset, retdest) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, size, retdest)
%jump(memset)

// Loads the code at `address` into memory, at the given context and segment, starting at offset 0.
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/bignum/util.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
%macro clear_current_general
// stack: dst, len
GET_CONTEXT
%stack (context, dst, len) -> (context, @SEGMENT_KERNEL_GENERAL, dst, 0, len, %%after)
%stack (context, dst, len) -> (context, @SEGMENT_KERNEL_GENERAL, dst, len, %%after)
%jump(memset)
%%after:
%endmacro
47 changes: 27 additions & 20 deletions evm/src/cpu/kernel/asm/memory/memset.asm
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
// Sets `count` values to `value` at
// Sets `count` values to 0 at
// DST = (dst_ctx, dst_segment, dst_addr).
// This tuple definition is used for brevity in the stack comments below.
global memset:
// stack: DST, value, count, retdest
DUP5
// stack: count, DST, value, count, retdest
ISZERO
// stack: count == 0, DST, value, count, retdest
%jumpi(memset_finish)
// stack: DST, value, count, retdest
DUP4
// stack: value, DST, value, count, retdest
DUP4
// stack: DST, count, retdest
DUP4
DUP4
// stack: DST, value, DST, value, count, retdest
MSTORE_GENERAL
// stack: DST, value, count, retdest
// stack: count, DST, count, retdest
%lt_const(0x20)
// stack: count < 32, DST, count, retdest
%jumpi(memset_finish)
// stack: DST, count, retdest
PUSH 32
PUSH 0
DUP5
DUP5
DUP5
// stack: DST, 0, 32, DST, count, retdest
MSTORE_32BYTES
// stack: DST, count, retdest

// Increment dst_addr.
SWAP2
%increment
%add_const(0x20)
SWAP2
// Decrement count.
SWAP4
%decrement
%sub_const(0x20)
SWAP4

// Continue the loop.
%jump(memset)

memset_finish:
// stack: DST, value, count, retdest
%pop5
// stack: DST, final_count, retdest
DUP4
PUSH 0
DUP5
DUP5
DUP5
// stack: DST, 0, final_count, DST, final_count, retdest
MSTORE_32BYTES
// stack: DST, final_count, retdest
%pop4
// stack: retdest
JUMP
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/memory/syscalls.asm
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ wcopy_large_offset:
// stack: kexit_info, dest_offset, offset, size
GET_CONTEXT
%stack (context, kexit_info, dest_offset, offset, size) ->
(context, @SEGMENT_MAIN_MEMORY, dest_offset, 0, size, wcopy_after, kexit_info)
(context, @SEGMENT_MAIN_MEMORY, dest_offset, size, wcopy_after, kexit_info)
%jump(memset)

wcopy_after:
Expand Down

0 comments on commit 8af189b

Please sign in to comment.