From bb6fb675e0f7f31c6880226186605d5d6f49b880 Mon Sep 17 00:00:00 2001 From: pinkiebell <40266861+pinkiebell@users.noreply.github.com> Date: Wed, 22 Jan 2020 17:49:15 +0100 Subject: [PATCH] libsolidity/codegen: Use calldatacopy to cheaply zero memory instead of codecopy. Motiviation: Zero'ing memory is commonplace in contracts, but with the upcoming Layer-2 EVM translation layers and other on-chain verification mechanisms, using `codecopy` becomes a `costly` operation in those sandboxes. Using `calldatacopy` achieves the same thing, gas costs are also the same as codecopy, and is significantly cheaper in the `sandbox` situation. --- libsolidity/codegen/CompilerUtils.cpp | 2 +- test/libsolidity/SolidityEndToEndTest.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index f9a0fbcec..c15d2537e 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -605,7 +605,7 @@ void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type) Whiskers templ(R"({ let size := mul(length, ) // cheap way of zero-initializing a memory range - codecopy(memptr, codesize(), size) + calldatacopy(memptr, calldatasize(), size) memptr := add(memptr, size) })"); templ("element_size", to_string(_type.memoryStride())); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index df89fb3f9..ee20e5b37 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -10179,7 +10179,7 @@ BOOST_AUTO_TEST_CASE(create_dynamic_array_with_zero_length) BOOST_AUTO_TEST_CASE(correctly_initialize_memory_array_in_constructor) { - // Memory arrays are initialized using codecopy past the size of the code. + // Memory arrays are initialized using calldatacopy past the size of the calldata. // This test checks that it also works in the constructor context. char const* sourceCode = R"( contract C {