-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGFlask.sol
More file actions
89 lines (78 loc) · 2.2 KB
/
GFlask.sol
File metadata and controls
89 lines (78 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: Unlicense
/*
|////////////////////////////|
| ⚠️ WARNING [ BIOHAZARD ] |
| -------------------------- |
| Authorized personnel only. |
|////////////////////////////|
*/
import "ds-test/test.sol";
/// @title GFlask
/// @author Zero Ekkusu
/// @notice Measure gas savings with different optimizations
abstract contract GFlask is DSTest {
uint256 private gasEmpty;
uint256 private gasUnoptimized;
modifier unoptimized(string memory label) {
uint256 startGas = gasleft();
_;
uint256 endGas = gasleft();
gFlask(false, startGas - endGas, label);
}
modifier optimized(string memory label) {
require(
gasUnoptimized != 0,
"No unoptimized function found (or the optimizer inlined it)!"
);
uint256 startGas = gasleft();
_;
uint256 endGas = gasleft();
gFlask(true, startGas - endGas, label);
}
function gFlask(
bool _optimized,
uint256 gas,
string memory label
) private {
if (!_optimized) {
require(
gasUnoptimized == 0,
"More than 1 unoptimized function found!"
);
measureEmpty();
gasUnoptimized = gas;
if (bytes(label).length != 0) {
emit log(label);
}
return;
}
if (gas == gasEmpty) {
return;
}
emit log("");
if (bytes(label).length != 0) {
emit log(label);
}
int256 savings = int256(gasUnoptimized) - int256(gas);
bool saved = savings > 0;
if (savings == 0) {
emit log("No savings.");
} else {
emit log_named_int(
saved ? "SAVES (GAS)" : "[!] More expensive (gas)",
savings
);
}
}
function measureEmpty() private {
Empty e = new Empty();
uint256 startGas = gasleft();
e.optimized();
uint256 endGas = gasleft();
gasEmpty = startGas - endGas;
}
}
import {SharedSetup} from "src/Samples.sol";
contract Empty is SharedSetup {
function optimized() public {}
}