Create empty dynamic memory arrays more efficiently.

This commit is contained in:
chriseth
2018-04-04 12:37:04 +02:00
committed by Alex Beregszaszi
parent c63efebd45
commit 0cbe55005d
8 changed files with 107 additions and 20 deletions
+4 -4
View File
@@ -111,12 +111,12 @@ BOOST_AUTO_TEST_CASE(basic_compilation)
BOOST_CHECK(contract["bytecode"].isString());
BOOST_CHECK_EQUAL(
dev::test::bytecodeSansMetadata(contract["bytecode"].asString()),
"60606040523415600e57600080fd5b603580601b6000396000f3006060604052600080fd00"
"60806040523415600e57600080fd5b603580601b6000396000f3006080604052600080fd00"
);
BOOST_CHECK(contract["runtimeBytecode"].isString());
BOOST_CHECK_EQUAL(
dev::test::bytecodeSansMetadata(contract["runtimeBytecode"].asString()),
"6060604052600080fd00"
"6080604052600080fd00"
);
BOOST_CHECK(contract["functionHashes"].isObject());
BOOST_CHECK(contract["gasEstimates"].isObject());
@@ -153,12 +153,12 @@ BOOST_AUTO_TEST_CASE(single_compilation)
BOOST_CHECK(contract["bytecode"].isString());
BOOST_CHECK_EQUAL(
dev::test::bytecodeSansMetadata(contract["bytecode"].asString()),
"60606040523415600e57600080fd5b603580601b6000396000f3006060604052600080fd00"
"60806040523415600e57600080fd5b603580601b6000396000f3006080604052600080fd00"
);
BOOST_CHECK(contract["runtimeBytecode"].isString());
BOOST_CHECK_EQUAL(
dev::test::bytecodeSansMetadata(contract["runtimeBytecode"].asString()),
"6060604052600080fd00"
"6080604052600080fd00"
);
BOOST_CHECK(contract["functionHashes"].isObject());
BOOST_CHECK(contract["gasEstimates"].isObject());
+35 -1
View File
@@ -7687,7 +7687,6 @@ BOOST_AUTO_TEST_CASE(create_memory_array_allocation_size)
ABI_CHECK(callContractFunction("f()"), encodeArgs(0x40, 0x40, 0x20 + 256));
}
BOOST_AUTO_TEST_CASE(memory_arrays_of_various_sizes)
{
// Computes binomial coefficients the chinese way
@@ -7710,6 +7709,41 @@ BOOST_AUTO_TEST_CASE(memory_arrays_of_various_sizes)
ABI_CHECK(callContractFunction("f(uint256,uint256)", encodeArgs(u256(9), u256(5))), encodeArgs(u256(70)));
}
BOOST_AUTO_TEST_CASE(create_multiple_dynamic_arrays)
{
char const* sourceCode = R"(
contract C {
function f() returns (uint) {
uint[][] memory x = new uint[][](42);
assert(x[0].length == 0);
x[0] = new uint[](1);
x[0][0] = 1;
assert(x[4].length == 0);
x[4] = new uint[](1);
x[4][0] = 2;
assert(x[10].length == 0);
x[10] = new uint[](1);
x[10][0] = 44;
uint[][] memory y = new uint[][](24);
assert(y[0].length == 0);
y[0] = new uint[](1);
y[0][0] = 1;
assert(y[4].length == 0);
y[4] = new uint[](1);
y[4][0] = 2;
assert(y[10].length == 0);
y[10] = new uint[](1);
y[10][0] = 88;
if ((x[0][0] == y[0][0]) && (x[4][0] == y[4][0]) && (x[10][0] == 44) && (y[10][0] == 88))
return 7;
return 0;
}
}
)";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(7)));
}
BOOST_AUTO_TEST_CASE(memory_overwrite)
{
char const* sourceCode = R"(
+24
View File
@@ -93,8 +93,10 @@ public:
{
m_contractAddress = m_nonOptimizedContract;
bytes nonOptimizedOutput = callContractFunction(_sig, _arguments...);
m_gasUsedNonOptimized = m_gasUsed;
m_contractAddress = m_optimizedContract;
bytes optimizedOutput = callContractFunction(_sig, _arguments...);
m_gasUsedOptimized = m_gasUsed;
BOOST_CHECK_MESSAGE(!optimizedOutput.empty(), "No optimized output for " + _sig);
BOOST_CHECK_MESSAGE(!nonOptimizedOutput.empty(), "No un-optimized output for " + _sig);
BOOST_CHECK_MESSAGE(nonOptimizedOutput == optimizedOutput, "Computed values do not match."
@@ -120,6 +122,8 @@ public:
}
protected:
u256 m_gasUsedOptimized;
u256 m_gasUsedNonOptimized;
bytes m_nonOptimizedBytecode;
bytes m_optimizedBytecode;
Address m_optimizedContract;
@@ -584,6 +588,26 @@ BOOST_AUTO_TEST_CASE(invalid_state_at_control_flow_join)
compareVersions("test()");
}
BOOST_AUTO_TEST_CASE(init_empty_dynamic_arrays)
{
// This is not so much an optimizer test, but rather a test
// that allocating empty arrays is implemented efficiently.
// In particular, initializing a dynamic memory array does
// not use any memory.
char const* sourceCode = R"(
contract Test {
function f() pure returns (uint r) {
uint[][] memory x = new uint[][](20000);
return x.length;
}
}
)";
compileBothVersions(sourceCode);
compareVersions("f()");
BOOST_CHECK_LE(m_gasUsedNonOptimized, 1900000);
BOOST_CHECK_LE(1600000, m_gasUsedNonOptimized);
}
BOOST_AUTO_TEST_CASE(optimise_multi_stores)
{
char const* sourceCode = R"(
+3 -3
View File
@@ -261,14 +261,14 @@ BOOST_AUTO_TEST_CASE(basic_compilation)
BOOST_CHECK(contract["evm"]["bytecode"]["object"].isString());
BOOST_CHECK_EQUAL(
dev::test::bytecodeSansMetadata(contract["evm"]["bytecode"]["object"].asString()),
"60606040523415600e57600080fd5b603580601b6000396000f3006060604052600080fd00"
"60806040523415600e57600080fd5b603580601b6000396000f3006080604052600080fd00"
);
BOOST_CHECK(contract["evm"]["assembly"].isString());
BOOST_CHECK(contract["evm"]["assembly"].asString().find(
" /* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x60)\n jumpi(tag_1, iszero(callvalue))\n"
" /* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x80)\n jumpi(tag_1, iszero(callvalue))\n"
" 0x0\n dup1\n revert\ntag_1:\n dataSize(sub_0)\n dup1\n dataOffset(sub_0)\n 0x0\n codecopy\n 0x0\n"
" return\nstop\n\nsub_0: assembly {\n /* \"fileA\":0:14 contract A { } */\n"
" mstore(0x40, 0x60)\n 0x0\n dup1\n revert\n\n"
" mstore(0x40, 0x80)\n 0x0\n dup1\n revert\n\n"
" auxdata: 0xa165627a7a7230582") == 0);
BOOST_CHECK(contract["evm"]["gasEstimates"].isObject());
BOOST_CHECK_EQUAL(