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

VRF V2 Plus Wrapper: Gas Optimization #12428

Conversation

leeyikjiun
Copy link
Contributor

@leeyikjiun leeyikjiun commented Mar 14, 2024

Re-organized the storage layout.

  • Swapped s_linkNativeFeed with s_link to be in the same slot as s_stalenessSeconds since they are always used together in _getFeedData
  • Shifted the smaller variables s_configured, s_disabled, and s_maxWords to the same slot as s_vrfCoordinator since they are always used together in onTokenTransfer and requestRandomWordsInNative. This also saves 1 storage slot.

Assign callback.callbackAddress to a local variable.

Gas savings, [-X-] is before, {+X+} is after.

| src/v0.8/vrf/dev/VRFV2PlusWrapper.sol:VRFV2PlusWrapper contract |                 |        |        |        |         |
|-----------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                                 | Deployment Size |        |        |        |         |
| [-2663658-]{+2731391+}                                                         | [-13220-]{+13288+}           |        |        |        |         |
| Function Name                                                   | min             | avg    | median | max    | # calls |
| calculateRequestPrice                                           | [-5030-]{+4870+}            | [-11881-]{+11388+}  | [-9084-]{+8924+}   | [-21530-]{+21370+}  | 6       |
| calculateRequestPriceNative                                     | [-763-]{+786+}             | [-3910-]{+3605+}   | [-2540-]{+2569+}   | [-8540-]{+6569+}   | 6       |
| disable                                                         | [-8168-]{+8180+}            | [-8168-]{+8180+}   | [-8168-]{+8180+}   | [-8168-]{+8180+}   | 1       |
| enable                                                          | [-1406-]{+1412+}            | [-1406-]{+1412+}   | [-1406-]{+1412+}   | [-1406-]{+1412+}   | 1       |
| estimateRequestPrice                                            | [-5089-]{+4929+}            | [-5089-]{+4929+}   | [-5089-]{+4929+}   | [-5089-]{+4929+}   | 2       |
| estimateRequestPriceNative                                      | [-2586-]{+2615+}            | [-2586-]{+2615+}   | [-2586-]{+2615+}   | [-2586-]{+2615+}   | 2       |
| getConfig                                                       | [-1091-]{+1115+}            | [-1091-]{+1115+}   | [-1091-]{+1115+}   | [-1091-]{+1115+}   | 11      |
| onTokenTransfer                                                 | [-95271-]{+93117+}           | [-97959-]{+96472+}  | [-96810-]{+94656+}  | [-101798-]{+101644+} | 3       |
| rawFulfillRandomWords                                           | [-68502-]{+68498+}           | [-68502-]{+68498+}  | [-68502-]{+68498+}  | [-68502-]{+68498+}  | 4       |
| requestRandomWordsInNative                                      | [-69245-]{+67257+}           | [-72508-]{+71520+}  | [-72508-]{+71520+}  | [-75772-]{+75784+}  | 2       |
| setConfig                                                       | 3355            | [-56506-]{+40486+}  | [-74028-]{+52178+}  | [-74028-]{+52178+}  | 15      |
  • Around 160 to 500 gas savings for calculateRequestPrice
  • Around 300 to 2k gas savings for calculateRequestPriceNative, but at times it increased by around 20 gas.
  • Around 1.5k to 2k gas savings for onTokenTransfer
  • Around 1k to 2k gas savings for requestRandomWordsInNative
  • There are some functions with 10 to 20 gas increments

Copy link
Contributor

I see that you haven't updated any README files. Would it make sense to do so?

@cl-sonarqube-production
Copy link

Quality Gate passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link
Contributor

@ibrajer ibrajer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kidambisrinivas kidambisrinivas merged commit 156b5f6 into VRF-921-VRF-V2-Plus-Wrapper-Remove-billing-parameters Mar 19, 2024
118 of 119 checks passed
@kidambisrinivas kidambisrinivas deleted the VRF-924-VRF-V2-Plus-Wrapper-Gas-Optimization branch March 19, 2024 14:43
github-merge-queue bot pushed a commit that referenced this pull request Mar 20, 2024
…_5 (#12407)

* Update VRFV2PlusWrapper billing parameters to mirror VRFCoordinatorV2_5

* Fix integration tests

* Fix Go scripts

* Fix calculate request price native using link premium percentage instead of native

* Update Go solidity wrappers

* vrfv2pluswrapper: optimize gas (#12428)

* Fix storage slot comment
kidambisrinivas pushed a commit that referenced this pull request Mar 27, 2024
…_5 (#12407)

* Update VRFV2PlusWrapper billing parameters to mirror VRFCoordinatorV2_5

* Fix integration tests

* Fix Go scripts

* Fix calculate request price native using link premium percentage instead of native

* Update Go solidity wrappers

* vrfv2pluswrapper: optimize gas (#12428)

* Fix storage slot comment
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