solstat/report/report_sections/optimizations/
solidity_math.rs1pub fn report_section_content() -> String {
2 String::from(
3 r##"
4
5## Use assembly for math (add, sub, mul, div)
6
7Use assembly for math instead of Solidity. You can check for overflow/underflow in assembly to ensure safety. If using Solidity versions < 0.8.0 and you are using Safemath, you can gain significant gas savings by using assembly to calculate values and checking for overflow/underflow.
8
9```js
10
11contract GasTest is DSTest {
12 Contract0 c0;
13 Contract1 c1;
14 Contract2 c2;
15 Contract3 c3;
16 Contract4 c4;
17 Contract5 c5;
18 Contract6 c6;
19 Contract7 c7;
20
21 function setUp() public {
22 c0 = new Contract0();
23 c1 = new Contract1();
24 c2 = new Contract2();
25 c3 = new Contract3();
26 c4 = new Contract4();
27 c5 = new Contract5();
28 c6 = new Contract6();
29 c7 = new Contract7();
30 }
31
32 function testGas() public {
33 c0.addTest(34598345, 100);
34 c1.addAssemblyTest(34598345, 100);
35 c2.subTest(34598345, 100);
36 c3.subAssemblyTest(34598345, 100);
37 c4.mulTest(34598345, 100);
38 c5.mulAssemblyTest(34598345, 100);
39 c6.divTest(34598345, 100);
40 c7.divAssemblyTest(34598345, 100);
41 }
42}
43
44contract Contract0 {
45 //addition in Solidity
46 function addTest(uint256 a, uint256 b) public pure {
47 uint256 c = a + b;
48 }
49}
50
51contract Contract1 {
52 //addition in assembly
53 function addAssemblyTest(uint256 a, uint256 b) public pure {
54 assembly {
55 let c := add(a, b)
56
57 if lt(c, a) {
58 mstore(0x00, "overflow")
59 revert(0x00, 0x20)
60 }
61 }
62 }
63}
64
65contract Contract2 {
66 //subtraction in Solidity
67 function subTest(uint256 a, uint256 b) public pure {
68 uint256 c = a - b;
69 }
70}
71
72contract Contract3 {
73 //subtraction in assembly
74 function subAssemblyTest(uint256 a, uint256 b) public pure {
75 assembly {
76 let c := sub(a, b)
77
78 if gt(c, a) {
79 mstore(0x00, "underflow")
80 revert(0x00, 0x20)
81 }
82 }
83 }
84}
85
86contract Contract4 {
87 //multiplication in Solidity
88 function mulTest(uint256 a, uint256 b) public pure {
89 uint256 c = a * b;
90 }
91}
92
93contract Contract5 {
94 //multiplication in assembly
95 function mulAssemblyTest(uint256 a, uint256 b) public pure {
96 assembly {
97 let c := mul(a, b)
98
99 if lt(c, a) {
100 mstore(0x00, "overflow")
101 revert(0x00, 0x20)
102 }
103 }
104 }
105}
106
107contract Contract6 {
108 //division in Solidity
109 function divTest(uint256 a, uint256 b) public pure {
110 uint256 c = a * b;
111 }
112}
113
114contract Contract7 {
115 //division in assembly
116 function divAssemblyTest(uint256 a, uint256 b) public pure {
117 assembly {
118 let c := div(a, b)
119
120 if gt(c, a) {
121 mstore(0x00, "underflow")
122 revert(0x00, 0x20)
123 }
124 }
125 }
126}
127
128
129```
130
131### Gas Report
132
133```js
134
135╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
136│ Contract0 contract ┆ ┆ ┆ ┆ ┆ │
137╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
138│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
139├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
140│ 40493 ┆ 233 ┆ ┆ ┆ ┆ │
141├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
142│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
143├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
144│ addTest ┆ 303 ┆ 303 ┆ 303 ┆ 303 ┆ 1 │
145╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
146╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
147│ Contract1 contract ┆ ┆ ┆ ┆ ┆ │
148╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
149│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
150├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
151│ 37087 ┆ 216 ┆ ┆ ┆ ┆ │
152├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
153│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
154├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
155│ addAssemblyTest ┆ 263 ┆ 263 ┆ 263 ┆ 263 ┆ 1 │
156╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
157╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
158│ Contract2 contract ┆ ┆ ┆ ┆ ┆ │
159╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
160│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
161├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
162│ 40293 ┆ 232 ┆ ┆ ┆ ┆ │
163├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
164│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
165├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
166│ subTest ┆ 300 ┆ 300 ┆ 300 ┆ 300 ┆ 1 │
167╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
168╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
169│ Contract3 contract ┆ ┆ ┆ ┆ ┆ │
170╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
171│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
172├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
173│ 37287 ┆ 217 ┆ ┆ ┆ ┆ │
174├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
175│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
176├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
177│ subAssemblyTest ┆ 263 ┆ 263 ┆ 263 ┆ 263 ┆ 1 │
178╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
179╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
180│ Contract4 contract ┆ ┆ ┆ ┆ ┆ │
181╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
182│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
183├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
184│ 41893 ┆ 240 ┆ ┆ ┆ ┆ │
185├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
186│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
187├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
188│ mulTest ┆ 325 ┆ 325 ┆ 325 ┆ 325 ┆ 1 │
189╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
190╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
191│ Contract5 contract ┆ ┆ ┆ ┆ ┆ │
192╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
193│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
194├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
195│ 37087 ┆ 216 ┆ ┆ ┆ ┆ │
196├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
197│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
198├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
199│ mulAssemblyTest ┆ 265 ┆ 265 ┆ 265 ┆ 265 ┆ 1 │
200╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
201╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
202│ Contract6 contract ┆ ┆ ┆ ┆ ┆ │
203╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
204│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
205├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
206│ 41893 ┆ 240 ┆ ┆ ┆ ┆ │
207├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
208│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
209├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
210│ divTest ┆ 325 ┆ 325 ┆ 325 ┆ 325 ┆ 1 │
211╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
212╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
213│ Contract7 contract ┆ ┆ ┆ ┆ ┆ │
214╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
215│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
216├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
217│ 37287 ┆ 217 ┆ ┆ ┆ ┆ │
218├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
219│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
220├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
221│ divAssemblyTest ┆ 265 ┆ 265 ┆ 265 ┆ 265 ┆ 1 │
222╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
223
224```
225
226"##,
227 )
228}