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

Keep functions simple and save a bit of gas in the process #133

Closed

Conversation

danielattilasimon
Copy link
Collaborator

@danielattilasimon danielattilasimon commented Apr 26, 2024

This is a further optimization of #129, mostly to keep the code simple and easy to read. As a side effect, it also saves a small bit of gas (1-47).

Before:

| src/BorrowerOperations.sol:BorrowerOperations contract |                 |        |        |        |         |
|--------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                        | Deployment Size |        |        |        |         |
| 3519685                                                | 16358           |        |        |        |         |
| Function Name                                          | min             | avg    | median | max    | # calls |
| BOLD_GAS_COMPENSATION                                  | 273             | 273    | 273    | 273    | 8       |
| activePool                                             | 2426            | 2426   | 2426   | 2426   | 1       |
| addColl                                                | 108100          | 200549 | 226285 | 226285 | 15      |
| adjustTrove                                            | 194292          | 196401 | 197104 | 197104 | 4       |
| adjustTroveInterestRate                                | 22568           | 126375 | 148760 | 162980 | 19      |
| applyTroveInterestPermissionless                       | 31948           | 109918 | 125513 | 125513 | 12      |
| closeTrove                                             | 137847          | 228278 | 242557 | 242557 | 10      |
| defaultPool                                            | 2383            | 2383   | 2383   | 2383   | 1       |
| openTrove                                              | 61636           | 563904 | 614499 | 654395 | 333     |
| priceFeed                                              | 2449            | 2449   | 2449   | 2449   | 1       |
| repayBold                                              | 114270          | 188387 | 207764 | 207764 | 15      |
| setAddManager                                          | 32601           | 49158  | 54677  | 54677  | 8       |
| setAddresses                                           | 288185          | 288185 | 288185 | 288185 | 206     |
| setRemoveManager                                       | 32529           | 49086  | 54605  | 54605  | 8       |
| sortedTroves                                           | 2403            | 2403   | 2403   | 2403   | 1       |
| troveManager                                           | 2405            | 2405   | 2405   | 2405   | 1       |
| withdrawBold                                           | 108817          | 187933 | 208080 | 208080 | 15      |
| withdrawColl                                           | 108172          | 196305 | 221377 | 221377 | 15      |

After:

| src/BorrowerOperations.sol:BorrowerOperations contract |                 |        |        |        |         |
|--------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost                                        | Deployment Size |        |        |        |         |
| 3519685                                                | 16358           |        |        |        |         |
| Function Name                                          | min             | avg    | median | max    | # calls |
| BOLD_GAS_COMPENSATION                                  | 273             | 273    | 273    | 273    | 8       |
| activePool                                             | 2426            | 2426   | 2426   | 2426   | 1       |
| addColl                                                | 108100          | 200549 | 226284 | 226284 | 15      |
| adjustTrove                                            | 194291          | 196400 | 197103 | 197103 | 4       |
| adjustTroveInterestRate                                | 22568           | 126375 | 148759 | 162979 | 19      |
| applyTroveInterestPermissionless                       | 31948           | 109918 | 125512 | 125512 | 12      |
| closeTrove                                             | 137846          | 228235 | 242510 | 242510 | 10      |
| defaultPool                                            | 2383            | 2383   | 2383   | 2383   | 1       |
| openTrove                                              | 61636           | 563904 | 614498 | 654394 | 333     |
| priceFeed                                              | 2449            | 2449   | 2449   | 2449   | 1       |
| repayBold                                              | 114270          | 188346 | 207717 | 207717 | 15      |
| setAddManager                                          | 32601           | 49158  | 54677  | 54677  | 8       |
| setAddresses                                           | 288185          | 288185 | 288185 | 288185 | 206     |
| setRemoveManager                                       | 32529           | 49086  | 54605  | 54605  | 8       |
| sortedTroves                                           | 2403            | 2403   | 2403   | 2403   | 1       |
| troveManager                                           | 2405            | 2405   | 2405   | 2405   | 1       |
| withdrawBold                                           | 108817          | 187893 | 208033 | 208033 | 15      |
| withdrawColl                                           | 108172          | 196265 | 221330 | 221330 | 15      |


ETH.safeTransfer(_account, _amount);
}

function sendETHToDefaultPool(uint256 _amount) external override {
_requireCallerIsTroveManager();

address defaultPoolAddressCached = defaultPoolAddress;
Copy link
Collaborator Author

@danielattilasimon danielattilasimon Apr 26, 2024

Choose a reason for hiding this comment

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

This kind of caching optimization is not needed when we're no longer calling a function inbetween. The compiler is capable of optimizing away the 2 accesses. The problem is when you call a function, the compiler has to assume that the state variable may have changed. Technically, for internal calls, that shouldn't even be a concern, as the compiler should be able to analyze that the function being called can't possibly change this bit of state, but maybe it wasn't being that smart.

Copy link
Collaborator

@bingen bingen left a comment

Choose a reason for hiding this comment

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

I’m fine with duplicating the accounting code, specially if we can get rid of those ugly cachings, but not a big fan of those operations inside the events.

@@ -136,25 +136,19 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
function sendETH(address _account, uint256 _amount) external override {
_requireCallerIsBOorTroveMorSP();

_accountForSendETH(_account, _amount);
emit ActivePoolETHBalanceUpdated(ETHBalance -= _amount);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don’t like having the operation inside the event. In my opinion it doesn’t help with readability, specially being inside an event (which I tend to overlook)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wasn't sure myself either. That settles it then, I'm moving it outside. BTW is there a reason for emitting events before the transfer when sending ETH, but the other way around when receiving?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, to be honest, I’m not worrying too much about events, and I was hoping we get them right in #98.
I would use the order that makes it easier to process later, or that feels more natural.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TBH I've never found these events useful for anything, I didn't even request adding ActivePool/DefaultPool to Dune's tables. I will probably be proposing that we remove them 😆

@danielattilasimon danielattilasimon deleted the gas-refactor-refactor branch May 6, 2024 08:11
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.

2 participants