Welcome to the 🧪 Optimizooor's Lab!
Optimize code and run tests with our equipment for optimizooors:
MyLab
: An environment for running controlled experimentsGFlask
: An instrument for measuring gas savings with different optimizations
Running 1 test for MyLab.json:MyLab
[PASS] test() (gas: 510664)
Logs:
Increment with `i += 1`
Use `++i` instead
SAVES (GAS): 3
Try `i = i + 1`
No savings.
Try `i++`
[!] More expensive (gas): -2
A list of common Solidity optimization tips and myths.
Download foundryup
curl -L https://foundry.paradigm.xyz | bash
Install Foundry
foundryup
Create a new project using our template
forge init --template ZeroEkkusu/optimizooors-lab my-lab
... or install as a dependency
forge install ZeroEkkusu/optimizooors-lab
To use MyLab
, provide code samples in Samples.sol
:
contract Sample0 is SharedSetup {
function measureGas() external {
uint i;
i++;
}
}
contract Sample1 is SharedSetup {
function measureGas() external {
uint i;
++i;
}
}
Note: For more complex setups, use SharedSetup
to minimize boilerplate.
Run forge test
to compare all provided samples against Sample0
:
00x@ubuntu:~$ forge test
SAVES (GAS): 5
It is possible to customize the console output with Labels
:
abstract contract Labels {
string label0 = "Increment with `i++`";
string label1 = "Use `++i` instead";
string label2 = "";
string label3 = "";
string label4 = "";
string label5 = "";
}
Let's run forge test
again:
00x@ubuntu:~$ forge test
Increment `i++`
Use `++i` instead
SAVES (GAS): 5
Note: Combine with other commands at your disposal for more power. You can run forge test --gas-report
to print the absolute gas costs as well.
Samples.sol
files are easy to share with others!Try copy-pasting their contents to another system and they'll work out of the box. No setup required.
To use GFlask
in your project, mark the function with unoptimized code as unoptimized
and the ones with optimized code as optimized
.
It is possible to customize the console output with labels. In the example below, we left them unlabeled (""
).
Then, call all the functions inside one test
function, first calling the unoptimized
function:
import "optimizooors-lab/biohazard/GFlask.sol"
contract Increment is GFlask {
function iPlusPlus() public unoptimized("") {
uint i;
i++;
}
function plusPlusI() public optimized("") {
uint i;
++i;
}
function testIncrement() {
iPlusPlus();
plusPlusI();
}
}
After running forge test
, you get the following output:
00x@ubuntu:~$ forge test
SAVES (GAS): 5
Note: Combine with other commands at your disposal for more power. You can run forge test --gas-report
to print the absolute gas costs as well.
Thank you for visiting the 🧪 Optimizooor's Lab!
Measuring gas savings can be tricky sometimes. It is useful to have basic knowledge of how the optimizer works, so you can isolate the code properly.
Visit ⛳ Re:Golf Course to learn more about this, as well as common optimization tips and myths.