From d185d30e8961b1f43e04276b56f3738d40d39811 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 23 Oct 2023 07:55:09 -0400 Subject: [PATCH 1/2] Speed-up memset and fix it to write 0 values --- evm/src/cpu/kernel/asm/account_code.asm | 2 +- evm/src/cpu/kernel/asm/bignum/util.asm | 2 +- evm/src/cpu/kernel/asm/memory/memset.asm | 47 +++++++++++++--------- evm/src/cpu/kernel/asm/memory/syscalls.asm | 2 +- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/evm/src/cpu/kernel/asm/account_code.asm b/evm/src/cpu/kernel/asm/account_code.asm index d7d09b5b7e..35f9c62dd1 100644 --- a/evm/src/cpu/kernel/asm/account_code.asm +++ b/evm/src/cpu/kernel/asm/account_code.asm @@ -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. diff --git a/evm/src/cpu/kernel/asm/bignum/util.asm b/evm/src/cpu/kernel/asm/bignum/util.asm index 79cbc1ec5e..7bd6e0dc33 100644 --- a/evm/src/cpu/kernel/asm/bignum/util.asm +++ b/evm/src/cpu/kernel/asm/bignum/util.asm @@ -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 diff --git a/evm/src/cpu/kernel/asm/memory/memset.asm b/evm/src/cpu/kernel/asm/memory/memset.asm index 77b37f6413..b04ade220b 100644 --- a/evm/src/cpu/kernel/asm/memory/memset.asm +++ b/evm/src/cpu/kernel/asm/memory/memset.asm @@ -1,38 +1,47 @@ -// 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 + // stack: DST, count, retdest + DUP4 + // stack: count, DST, count, retdest + %lt_const(0x20) + // stack: count < 32, DST, count, retdest %jumpi(memset_finish) - // stack: DST, value, count, retdest + // stack: DST, count, retdest - DUP4 - // stack: value, DST, value, count, retdest - DUP4 - DUP4 - DUP4 - // stack: DST, value, DST, value, count, retdest - MSTORE_GENERAL - // stack: DST, value, count, retdest + // 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 diff --git a/evm/src/cpu/kernel/asm/memory/syscalls.asm b/evm/src/cpu/kernel/asm/memory/syscalls.asm index f23d261105..20f584f7cf 100644 --- a/evm/src/cpu/kernel/asm/memory/syscalls.asm +++ b/evm/src/cpu/kernel/asm/memory/syscalls.asm @@ -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: From 385ab3c64f0ee73e7447c89e55086393520bbefb Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 23 Oct 2023 08:15:10 -0400 Subject: [PATCH 2/2] Remove redundant --- evm/src/cpu/kernel/asm/memory/memset.asm | 2 -- 1 file changed, 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/memset.asm b/evm/src/cpu/kernel/asm/memory/memset.asm index b04ade220b..c0bad482fa 100644 --- a/evm/src/cpu/kernel/asm/memory/memset.asm +++ b/evm/src/cpu/kernel/asm/memory/memset.asm @@ -8,8 +8,6 @@ global memset: %lt_const(0x20) // stack: count < 32, DST, count, retdest %jumpi(memset_finish) - // stack: DST, count, retdest - // stack: DST, count, retdest PUSH 32 PUSH 0