Skip to content

Commit b5deca0

Browse files
authored
Merge pull request argotorg#4779 from ethereum/optimise-string-literals
Decide better in storing string literals as data
2 parents 599760b + d80d324 commit b5deca0

8 files changed

Lines changed: 116 additions & 2 deletions

File tree

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Language Features:
55

66
Compiler Features:
77
* Inline Assembly: Improve error messages around invalid function argument count.
8+
* Code Generator: Use codecopy for string constants more aggressively.
89
* Code Generator: Use binary search for dispatch function if more efficient. The size/speed tradeoff can be tuned using ``--optimize-runs``.
910
* Type Checker: Add an additional reason to be displayed when type conversion fails.
1011

libsolidity/codegen/CompilerUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ void CompilerUtils::storeStringData(bytesConstRef _data)
12051205
{
12061206
//@todo provide both alternatives to the optimiser
12071207
// stack: mempos
1208-
if (_data.size() <= 128)
1208+
if (_data.size() <= 32)
12091209
{
12101210
for (unsigned i = 0; i < _data.size(); i += 32)
12111211
{

test/boostTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] )
160160
"LLLEndToEndTest",
161161
#endif
162162
"GasMeterTests",
163+
"GasCostTests",
163164
"SolidityEndToEndTest",
164165
"SolidityOptimizer"
165166
})

test/cmdlineTests/data_storage.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pragma solidity >=0.0;
2+
3+
contract C {
4+
function f() pure public {
5+
require(false, "1234567890123456789012345678901");
6+
require(false, "12345678901234567890123456789012");
7+
require(false, "123456789012345678901234567890123");
8+
require(false, "1234567890123456789012345678901234");
9+
require(false, "12345678901234567890123456789012345");
10+
require(false, "123456789012345678901234567890123456");
11+
require(false, "123456789012345678901234567890121234567890123456789012345678901");
12+
require(false, "1234567890123456789012345678901212345678901234567890123456789012");
13+
require(false, "12345678901234567890123456789012123456789012345678901234567890123");
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--gas
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
======= data_storage.sol:C =======
3+
Gas estimation:
4+
construction:
5+
306 + 264400 = 264706
6+
external:
7+
f(): 263

test/libsolidity/GasCosts.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/**
18+
* Tests that check that the cost of certain operations stay within range.
19+
*/
20+
21+
#include <test/libsolidity/SolidityExecutionFramework.h>
22+
23+
#include <cmath>
24+
25+
using namespace std;
26+
using namespace langutil;
27+
using namespace dev::eth;
28+
using namespace dev::solidity;
29+
using namespace dev::test;
30+
31+
namespace dev
32+
{
33+
namespace solidity
34+
{
35+
namespace test
36+
{
37+
38+
#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \
39+
do \
40+
{ \
41+
u256 gasOpt{_gasOpt}; \
42+
u256 gasNoOpt{_gasNoOpt}; \
43+
u256 tolerance{_tolerance}; \
44+
u256 gas = m_optimize ? gasOpt : gasNoOpt; \
45+
u256 diff = gas < m_gasUsed ? m_gasUsed - gas : gas - m_gasUsed; \
46+
BOOST_CHECK_MESSAGE( \
47+
diff <= tolerance, \
48+
"Gas used: " + \
49+
m_gasUsed.str() + \
50+
" - expected: " + \
51+
gas.str() + \
52+
" (tolerance: " + \
53+
tolerance.str() + \
54+
")" \
55+
); \
56+
} while(0)
57+
58+
BOOST_FIXTURE_TEST_SUITE(GasCostTests, SolidityExecutionFramework)
59+
60+
BOOST_AUTO_TEST_CASE(string_storage)
61+
{
62+
char const* sourceCode = R"(
63+
contract C {
64+
function f() pure public {
65+
require(false, "Not Authorized. This function can only be called by the custodian or owner of this contract");
66+
}
67+
}
68+
)";
69+
compileAndRun(sourceCode);
70+
71+
if (Options::get().evmVersion() <= EVMVersion::byzantium())
72+
CHECK_GAS(134435, 130591, 100);
73+
else
74+
CHECK_GAS(127225, 124873, 100);
75+
if (Options::get().evmVersion() >= EVMVersion::byzantium())
76+
{
77+
callContractFunction("f()");
78+
if (Options::get().evmVersion() == EVMVersion::byzantium())
79+
CHECK_GAS(21551, 21526, 20);
80+
else
81+
CHECK_GAS(21546, 21526, 20);
82+
}
83+
}
84+
85+
BOOST_AUTO_TEST_SUITE_END()
86+
87+
}
88+
}
89+
}

test/libsolidity/SolidityEndToEndTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12245,7 +12245,7 @@ BOOST_AUTO_TEST_CASE(include_creation_bytecode_only_once)
1224512245
compileAndRun(sourceCode);
1224612246
BOOST_CHECK_LE(
1224712247
double(m_compiler.object("Double").bytecode.size()),
12248-
1.1 * double(m_compiler.object("Single").bytecode.size())
12248+
1.2 * double(m_compiler.object("Single").bytecode.size())
1224912249
);
1225012250
}
1225112251

0 commit comments

Comments
 (0)