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

Clipper yank gas opt #175

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/clip.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,11 @@ contract Clipper {
emit SetBreaker(level);
}

// Cancel an auction during ES
// Cancel an auction during ES or via governance action.
function yank(uint id) external auth {
Sale memory sale = sales[id];
require(sale.usr != address(0), "Clipper/not-running-auction");
dog.digs(ilk, sale.tab);
vat.flux(ilk, address(this), msg.sender, sale.lot);
require(sales[id].usr != address(0), "Clipper/not-running-auction");
dog.digs(ilk, sales[id].tab);
vat.flux(ilk, address(this), msg.sender, sales[id].lot);
Copy link
Contributor

Choose a reason for hiding this comment

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

curious if you tested the gas difference using the optimizer with a lot of runs?

Copy link
Contributor Author

@kmbarry1 kmbarry1 Dec 17, 2020

Choose a reason for hiding this comment

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

Well, I hadn't, but since you ask (using solc 0.6.7, with the note modifier bodies commented out since you can't compile with optimizations on if your code contains explicit mstore calls):

10^6 optimizer runs:
...whole struct read: 36,064
...per-field reads: 34,210

No optimization:
...whole struct read: 41,005
...per-fields reads: 38,296

So the optimizer does succeed in narrowing the gap, and (optimizations+whole struct read) is better than (no optimizations+per-field reads) but per-field reads is still better than a whole-struct read when optimizations are on. Btw bumping the runs up to 10^9 doesn't change them, so likely the numbers above are already the limit of the current optimizer's abilities.

_remove(id);
emit Yank();
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/clip.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,31 @@ contract ClipperTest is DSTest {
hevm.warp(startTime + 3601 seconds);
assertTrue( try_redo(1));
}

function test_Clipper_yank() public takeSetup {
uint256 preGemBalance = vat.gem(ilk, address(this));
(,, uint256 origLot,,,) = clip.sales(1);

uint startGas = gasleft();
clip.yank(1);
uint endGas = gasleft();
emit log_named_uint("yank gas", startGas - endGas);

// Assert that the auction was deleted.
(uint256 pos, uint256 tab, uint256 lot, address usr, uint256 tic, uint256 top) = clip.sales(1);
assertEq(pos, 0);
assertEq(tab, 0);
assertEq(lot, 0);
assertEq(usr, address(0));
assertEq(uint256(tic), 0);
assertEq(top, 0);

// Assert that callback to clear dirt was successful.
assertEq(dog.Dirt(), 0);
(,,, uint256 dirt,,) = dog.ilks(ilk);
assertEq(dirt, 0);

// Assert transfer of gem.
assertEq(vat.gem(ilk, address(this)), preGemBalance + origLot);
}
}