Skip to content

Gas Optimization Bounty submission #1 #452

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

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

nkrishang
Copy link
Contributor

Summary

Submission Contributor Contracts Target gas report gasreport.txt Current gas report gasreport.txt
#417 https://github.com/WhiteOakKong NFTStake.sol 700e10b 4694d26

Overall gas reduced in target contracts: 9.26 %
Total before: 725_049 after: 657_895 (ref: src/test/benchmark/NFTStakeBenchmark.t.sol)

WhiteOakKong and others added 4 commits August 3, 2023 12:03
* Update Struct `Staker`

* uint256 amountStake => uint64 amountStaked
* uint256 conditionIdOflastUpdate => uint64 conditionIdOflastUpdate
* uint256 timeOfLastUpdate => uint128 timeOfLastUpdate
* Other associated changes to facilitate use of updated struct

* Remove `stakersArray` and reorder varibles

`stakersArray` does not appear to serve any purpose other than to allow
for easy off-chain retreival of all staked users.

Moving the declaration of indexArray below the non-dynamic variables
allows for more efficient variable packing.
This saves ~2K gas on the staking function.

* Remove ownership and approval check from `_stake`

The removed checks are included in all modern ERC721 implementations.
If a project removes them from their ERC721 contract,
they should override the `_stake` function and include them there.
However, in most instances, it is redundant to include them in `_stake`.

* linting

* run yarn gas

* update gas report to ensure accuracy
@nkrishang nkrishang merged commit 464d016 into main Aug 3, 2023
@kien-ngo
Copy link

@nkrishang @WhiteOakKong hey guys I'm looking to learn from this. Would you explain why we can shift from using uint256 to uint64/128 etc ?
image

@WhiteOakKong
Copy link
Contributor

@nkrishang @WhiteOakKong hey guys I'm looking to learn from this. Would you explain why we can shift from using uint256 to uint64/128 etc ?

image

Depending on the context of the specific variable, you may be able to use a size less than uint256.

For instance, in this NFT staking contract, we can make a reasonable assumption that a user is not going to have > unit64.max() (a value of ~18 quintillion) tokens to stake.

Similarly with the timestamp, uint128.max() allows for functionality well into the future, beyond what would reasonably be needed for this contract (or the planet...).

@kien-ngo
Copy link

Beautiful. Thank you for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants