Update UniversalRewardsDistributor.sol: Modifier Optimization #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to optimize the gas by using these two modifiers -> 'onlyOwner' and 'notFrozen'. Actually, they are the ones which are used the most in this particular code and consumes a lot of gas. Why they do?
Usually, modifiers copies the code in each instances whenever they are used in a smart contract and thus increases the bytecode size which costs gas. So to tackle this, a good idea is to refactor them with the help of internal function like I did in this code. Moreover, there's a point to keep in mind -> This method do prevent the increase of bytecode size but at the cost of JUMP opcode, which means usually the developer needs to have a trade-off in what's best thing to use at a particular point.
That's why I used this 'refactoring' method with just these two modifiers because they were used the most in this contract (Try to prefer this method only when a modifier is used more than 2 times).
Anyways, enough talking...Let's get down to results, Below is a simple demo code which include two modifiers (simple ones) and some functions (without any code) that are using these modifiers. Then we gonna run them on remix and see the gas usage. Here's the code:
After deploying this contract on remix, I got this
then I made some desired changes which are:
and these changes do brought me some fruitful results
That's it. I know it was kind of a long read but explanation matters!!
Moreover, if anyone kind of feels somewhat wrong in this change then do let me know. Will be a great learning experience then😄