Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #41

Open
code423n4 opened this issue Feb 15, 2022 · 1 comment
Open

Gas Optimizations #41

code423n4 opened this issue Feb 15, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Implement loops more efficiently

Caching the length saves gas. Moreover not assigning the default value 0 to i save gas;

Reference implementation


uint256 len = array.length;

for (uint256; i < len; ++i) {

// Operations

}

Scope:


./core/LensHub.sol:541:        for (uint256 i = 0; i < vars.datas.length; ++i) {
./core/modules/follow/ApprovalFollowModule.sol:41:        for (uint256 i = 0; i < addresses.length; ++i) {
./core/modules/follow/ApprovalFollowModule.sol:66:            for (uint256 i = 0; i < addresses.length; ++i) {
./core/modules/follow/ApprovalFollowModule.sol:128:        for (uint256 i = 0; i < toCheck.length; ++i) {
./libraries/InteractionLogic.sol:47:        for (uint256 i = 0; i < profileIds.length; ++i) {
./libraries/PublishingLogic.sol:403:        for (uint256 i = 0; i < byteHandle.length; ++i) {

For uint use != 0 rather than > 0

It costs less gas to do so.

Scope:


./core/base/ERC721Time.sol:135:            bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : '';
./core/modules/follow/ApprovalFollowModule.sol:64:        if (data.length > 0) {

Do not assign default values to save gas

Scope:

uint256 x; costs less gas then uint256 x = 0;.


./core/FollowNFT.sol:120:        uint256 lower = 0;
./core/FollowNFT.sol:162:        uint256 lower = 0;
./upgradeability/VersionedInitializable.sol:29:    uint256 private lastInitializedRevision = 0;

Use bit-shift to save gas rather than dividing by 2

Scope:


./core/FollowNFT.sol:134:            uint256 center = upper - (upper - lower) / 2;
./core/FollowNFT.sol:176:            uint256 center = upper - (upper - lower) / 2;
./core/LensHub.sol:27: *      2. Almost every event in the protocol emits the current block timestamp, reducing the need to fetch it manually.
./core/base/ERC721Time.sol:20: * 2. Constructor replaced with an initializer.
./core/modules/ModuleGlobals.sol:109:        if (newTreasuryFee >= BPS_MAX / 2) revert Errors.InitParamsInvalid();

Use increment, when possible

In the code base += 1 is used, but using preincrement ++ is cheaper. (Same applies for substraction)

Scope:


./core/base/ERC721Time.sol:348:        _balances[to] += 1;
./core/base/ERC721Time.sol:404:        _balances[to] += 1;
./core/base/ERC721Time.sol:373:        _balances[owner] -= 1;

./core/base/ERC721Time.sol:373:        _balances[owner] -= 1;
./core/base/ERC721Time.sol:403:        _balances[from] -= 1;

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 15, 2022
code423n4 added a commit that referenced this issue Feb 15, 2022
@Zer0dot
Copy link
Collaborator

Zer0dot commented Mar 24, 2022

Caching is included in lens-protocol/core#80, the rest, although valid except for the zero initialization (which is handled by the optimizer now afaik), we won't be taking any action on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants