Skip to content
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3afa909
✨ Add a few more tips
devtooligan Dec 14, 2021
0d796cb
docs: update descriptions and formatting
devtooligan Dec 15, 2021
5b1cad0
dapp init GolfCourse
devtooligan Dec 18, 2021
244182e
dapp install ds-test
devtooligan Dec 18, 2021
fc1882b
feat: adds examples and tests
devtooligan Dec 18, 2021
889b45f
docs: update readme.md with links and gas
devtooligan Dec 18, 2021
fb1f401
feat: work on formatting and add a bunch of examples
devtooligan Feb 13, 2022
48b3b6e
fix: typo
devtooligan Feb 14, 2022
d33debe
feat: update comments
devtooligan Feb 16, 2022
b814df2
feat: add RequireNeZero tip
devtooligan Feb 16, 2022
f45ed35
tweak: update gitignore
devtooligan Feb 16, 2022
70d07b7
fix: update directory structure to eliminate differences in contract …
devtooligan Feb 16, 2022
5606811
fix: update gas usage from new ctrct names
devtooligan Feb 16, 2022
52b02b0
fix: update links in readme
devtooligan Feb 16, 2022
00d2a4f
feat: add payable functions tip
devtooligan Feb 16, 2022
c4cfffb
fix: format code in markdown
ZeroEkkusu Feb 16, 2022
fc0707f
docs: add more info to "Make functions `payable`"
ZeroEkkusu Feb 16, 2022
d4dabe3
Merge pull request #4 from ZeroEkkusu/work-1
devtooligan Feb 16, 2022
da9e4de
fix: add tests
devtooligan Feb 17, 2022
bfac3ce
Merge branch 'some-tips-to-start' of github.com:Rari-Capital/golf-cou…
devtooligan Feb 17, 2022
964c888
docs: update explanation
devtooligan Feb 17, 2022
7b32928
feat: new tip re: array +=
devtooligan Feb 17, 2022
6bb2a4a
feat: ArayPlus contracts
devtooligan Feb 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A list of common Solidity optimization tips and myths.

## Tips

### Right Shift Instead of Dividing By 2
### When dividing by two, use `>> 1` instead of `/ 2`

The `SHR` opcode is 3 gas cheaper than `DIV` and also bypasses Solidity's division by 0 prevention overhead.

Expand All @@ -12,10 +12,63 @@ The `SHR` opcode is 3 gas cheaper than `DIV` and also bypasses Solidity's divisi

```solidity
// Unoptimized:
uint256 two = 4 / 2;
uint256 foo = bar / 2;

// Optimized:
uint256 two = 4 >> 1;
uint256 foo = bar >> 1;
```


### Use `>=` and `<=` instead of `>` and `<`

Explanation tbd

- [Gas Usage]()
- [Full Example]()

```solidity
// Unoptimized:
bool foo = bar > 0;

// Optimized:
bool foo = bar >= 1;
```


### Using `!=` is usually cheaper than `>` or `<`

Explanation tbd

- [Gas Usage]()
- [Full Example]()

```solidity
// Unoptimized:
uint256 foo = bar < 32;

// Optimized:
uint256 foo = bar != 32;
```


### Use `<address>.code.length` instead of assembly `extcodesize` to avoid variable overhead

Solidity [0.8.1](https://github.com/ethereum/solidity/blob/develop/Changelog.md#081-2021-01-27) implements a shortcut for `<address>.code.length` that avoids copying code to memory. More explanation TBD

- [Gas Usage]()
- [Full Example]()

```solidity
// Unoptimized:
uint256 foo;
assembly {
foo := extcodesize(bar)
}
return foo;

// Optimized:
return bar.code.length;
```


## Myths