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 optimisation - Improve storage of info objects in InstanceStore #660

Closed
Tracked by #594
doerfli opened this issue Sep 3, 2024 · 2 comments
Closed
Tracked by #594
Assignees
Labels

Comments

@doerfli
Copy link
Contributor

doerfli commented Sep 3, 2024

Currently all info objects are stored in generic encoded bytes for in the KeyValueStore. But this is bad as using abi.encode will use one slot per field of the struct (even if the struct is slot optimized).
To improve gas usage, the InstanceStore needs to be refactored to store the structs plain instead of as encoded bytes. A quick hack test resultes in reduction of the gas consumption by over 50% to store PolicyInfo, FeeInfo and RiskInfo objects. This results from reduced storage costs as well has not having to encode to bytes. Reading will also get cheaper, but will have less overall impact as read costs are really in the overall picture (but it could sum up in a complex process).

Some background in abi.encode can be found here https://medium.com/@scourgedev/deep-dive-into-abi-encode-types-padding-and-disassembly-84472f1b4543

Example tx: https://amoy.polygonscan.com/tx/0xc85256c6980d7c30ec633892906c51d38d800e24b18a1c7704cc63b2f9e25cd5

Screenshot from tenderly for a createApplication call in the InstanceStore
basetest___Tenderly

@doerfli doerfli added enhancement New feature or request instance gas costs labels Sep 3, 2024
@doerfli doerfli mentioned this issue Sep 3, 2024
4 tasks
@doerfli doerfli added this to the GIF v3 Audit Ready milestone Sep 3, 2024
@doerfli doerfli self-assigned this Sep 13, 2024
@doerfli
Copy link
Contributor Author

doerfli commented Sep 13, 2024

Gas comsumption table for test test_productCreateApplication_happyCase before change

Ran 1 test for test/component/product/ProductApplication.t.sol:ProductApplicationTest
[PASS] test_productCreateApplication_happyCase() (gas: 2307770)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 181.06ms (4.82ms CPU time)
| contracts/product/ApplicationService.sol:ApplicationService contract |                 |        |        |        |         |
|----------------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                                      | Deployment Size |        |        |        |         |
| 0                                                                    | 0               |        |        |        |         |
| Function Name                                                        | min             | avg    | median | max    | # calls |
| authority                                                            | 1007            | 1673   | 1007   | 3007   | 3       |
| create                                                               | 813762          | 813762 | 813762 | 813762 | 1       |
| getDomain                                                            | 956             | 956    | 956    | 956    | 5       |
| getInitialInfo                                                       | 14782           | 14782  | 14782  | 14782  | 1       |
| getNftId                                                             | 886             | 886    | 886    | 886    | 1       |
| getOwner                                                             | 7851            | 7851   | 7851   | 7851   | 1       |
| getRelease                                                           | 2757            | 2757   | 2757   | 2757   | 1       |
| getVersion                                                           | 3216            | 4466   | 4466   | 5716   | 2       |
| initializeVersionable                                                | 332461          | 332461 | 332461 | 332461 | 1       |
| linkToRegisteredNftId                                                | 22551           | 22551  | 22551  | 22551  | 1       |
| supportsInterface                                                    | 2812            | 2812   | 2812   | 2812   | 1       |


| contracts/product/PricingService.sol:PricingService contract |                 |        |        |        |         |
|--------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                              | Deployment Size |        |        |        |         |
| 0                                                            | 0               |        |        |        |         |
| Function Name                                                | min             | avg    | median | max    | # calls |
| authority                                                    | 919             | 1585   | 919    | 2919   | 3       |
| calculatePremium                                             | 383950          | 383950 | 383950 | 383950 | 1       |
| getDomain                                                    | 868             | 868    | 868    | 868    | 5       |
| getInitialInfo                                               | 14782           | 14782  | 14782  | 14782  | 1       |
| getNftId                                                     | 820             | 820    | 820    | 820    | 1       |
| getOwner                                                     | 7785            | 7785   | 7785   | 7785   | 1       |
| getRelease                                                   | 2691            | 2691   | 2691   | 2691   | 1       |
| getVersion                                                   | 3216            | 4466   | 4466   | 5716   | 2       |
| initializeVersionable                                        | 277327          | 277327 | 277327 | 277327 | 1       |
| linkToRegisteredNftId                                        | 22551           | 22551  | 22551  | 22551  | 1       |
| supportsInterface                                            | 2812            | 2812   | 2812   | 2812   | 1       |

@doerfli
Copy link
Contributor Author

doerfli commented Sep 16, 2024

Gas consumption after storing PolicyInfo not encoded in ProductStore

[PASS] test_productCreateApplication_happyCase() (gas: 2144629)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 174.43ms (3.82ms CPU time)
| contracts/product/ApplicationService.sol:ApplicationService contract |                 |        |        |        |         |
|----------------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                                      | Deployment Size |        |        |        |         |
| 0                                                                    | 0               |        |        |        |         |
| Function Name                                                        | min             | avg    | median | max    | # calls |
| authority                                                            | 1007            | 1673   | 1007   | 3007   | 3       |
| create                                                               | 646266          | 646266 | 646266 | 646266 | 1       |
| getDomain                                                            | 956             | 956    | 956    | 956    | 5       |
| getInitialInfo                                                       | 14782           | 14782  | 14782  | 14782  | 1       |
| getNftId                                                             | 886             | 886    | 886    | 886    | 1       |
| getOwner                                                             | 7851            | 7851   | 7851   | 7851   | 1       |
| getRelease                                                           | 2757            | 2757   | 2757   | 2757   | 1       |
| getVersion                                                           | 3216            | 4466   | 4466   | 5716   | 2       |
| initializeVersionable                                                | 332461          | 332461 | 332461 | 332461 | 1       |
| linkToRegisteredNftId                                                | 22551           | 22551  | 22551  | 22551  | 1       |
| supportsInterface                                                    | 2812            | 2812   | 2812   | 2812   | 1       |


| contracts/product/PricingService.sol:PricingService contract |                 |        |        |        |         |
|--------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                              | Deployment Size |        |        |        |         |
| 0                                                            | 0               |        |        |        |         |
| Function Name                                                | min             | avg    | median | max    | # calls |
| authority                                                    | 919             | 1585   | 919    | 2919   | 3       |
| calculatePremium                                             | 383686          | 383686 | 383686 | 383686 | 1       |
| getDomain                                                    | 868             | 868    | 868    | 868    | 5       |
| getInitialInfo                                               | 14782           | 14782  | 14782  | 14782  | 1       |
| getNftId                                                     | 820             | 820    | 820    | 820    | 1       |
| getOwner                                                     | 7785            | 7785   | 7785   | 7785   | 1       |
| getRelease                                                   | 2691            | 2691   | 2691   | 2691   | 1       |
| getVersion                                                   | 3216            | 4466   | 4466   | 5716   | 2       |
| initializeVersionable                                        | 277327          | 277327 | 277327 | 277327 | 1       |
| linkToRegisteredNftId                                        | 22551           | 22551  | 22551  | 22551  | 1       |
| supportsInterface                                            | 2812            | 2812   | 2812   | 2812   | 1       |

doerfli added a commit that referenced this issue Sep 16, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
doerfli added a commit that referenced this issue Sep 17, 2024
migrate product related infos to ProductStore (#660)
@doerfli doerfli closed this as completed Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant