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

Bit packing step 1 #46

Merged
merged 11 commits into from
Dec 13, 2023
Merged

Bit packing step 1 #46

merged 11 commits into from
Dec 13, 2023

Conversation

toninorair
Copy link
Collaborator

@toninorair toninorair commented Dec 9, 2023

  • Bit-packing obvious things to get easy gains.

  • Picked approach - interfaces and interactions in uint256, internal storage fields - uintxx. Add safe conversions via introduced UintMath library.

Copy link

github-actions bot commented Dec 9, 2023

Changes to gas cost

Generated at commit: 740ef31035878135d0cbed0149932068fe4d64ed, compared to commit: d8d3a9eb48eb4621f4b66683fb6be9d0423b559e

🧾 Summary (20% most significant diffs)

Contract Method Avg (+/-) %
ProtocolHarness deactivateMinter
freezeMinter
mintM
proposeMint
proposeRetrieval
retrievalNonce
setCollateralUpdateOf
setLastCollateralUpdateIntervalOf
setLatestRate
setMintProposalOf
updateCollateral
-12,110 ✅
-11,363 ✅
-9,146 ✅
-18,316 ✅
-21,583 ✅
-1,908 ✅
-16,991 ✅
-21,841 ✅
-21,722 ✅
-43,228 ✅
-17,126 ✅
-23.14%
-45.73%
-24.14%
-35.86%
-38.17%
-74.24%
-74.13%
-95.29%
-96.07%
-38.49%
-22.35%
Protocol proposeMint
updateCollateral
-46,499 ✅
-46,107 ✅
-37.17%
-29.29%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
ProtocolHarness 5,041,022 (+907,941) activateMinter
activeOwedMOf
burnM
cancelMint
collateralOf
collateralUpdateOf
deactivateMinter
freezeMinter
getPenaltyForMissedCollateralUpdates
inactiveOwedMOf
isActiveMinter
lastCollateralUpdateIntervalOf
latestIndex
mintM
mintNonce
mintProposalOf
penalizedUntilOf
pendingCollateralRetrievalOf
principalOfActiveOwedMOf
proposeMint
proposeRetrieval
retrievalNonce
setActiveMinter
setCollateralOf
setCollateralUpdateOf
setLastCollateralUpdateIntervalOf
setLatestRate
setMintProposalOf
setPrincipalOfActiveOwedMOf
totalPendingCollateralRetrievalsOf
unfrozenTimeOf
updateCollateral
4,391 (+90)
5,854 (+271)
15,488 (-2,973)
7,989 (0)
2,254 (+310)
1,051 (+98)
3,600 (+56)
7,626 (+400)
5,939 (+171)
1,051 (+98)
1,007 (+56)
1,000 (+48)
613 (+42)
1,208 (+154)
2,548 (+42)
2,174 (+226)
1,007 (+98)
1,257 (+54)
979 (+48)
1,388 (+154)
2,826 (+56)
662 (-1,908)
4,768 (+44)
23,034 (+134)
5,931 (-16,991)
1,080 (-21,841)
889 (-21,722)
69,069 (-43,228)
41,740 (+605)
1,009 (+98)
1,006 (+98)
4,011 (+56)
+2.09%
+4.85%
-16.10%
0.00%
+15.95%
+10.28%
+1.58%
+5.54%
+2.96%
+10.28%
+5.89%
+5.04%
+7.36%
+14.61%
+1.68%
+11.60%
+10.78%
+4.49%
+5.16%
+12.48%
+2.02%
-74.24%
+0.93%
+0.59%
-74.13%
-95.29%
-96.07%
-38.49%
+1.47%
+10.76%
+10.79%
+1.42%
26,969 (+111)
6,211 (-443)
37,008 (-3,334)
9,321 (+130)
2,254 (+310)
1,051 (+98)
40,231 (-12,110)
13,486 (-11,363)
13,822 (-3,535)
1,051 (+98)
1,007 (+56)
1,000 (+48)
613 (+42)
28,747 (-9,146)
2,548 (+42)
2,174 (+226)
1,007 (+98)
1,257 (+54)
979 (+48)
32,767 (-18,316)
34,957 (-21,583)
662 (-1,908)
4,768 (+44)
23,034 (+134)
5,931 (-16,991)
1,080 (-21,841)
889 (-21,722)
69,069 (-43,228)
45,517 (+510)
1,009 (+98)
1,006 (+98)
59,490 (-17,126)
+0.41%
-6.66%
-8.26%
+1.41%
+15.95%
+10.28%
-23.14%
-45.73%
-20.37%
+10.28%
+5.89%
+5.04%
+7.36%
-24.14%
+1.68%
+11.60%
+10.78%
+4.49%
+5.16%
-35.86%
-38.17%
-74.24%
+0.93%
+0.59%
-74.13%
-95.29%
-96.07%
-38.49%
+1.13%
+10.76%
+10.79%
-22.35%
27,388 (+112)
5,854 (+271)
35,933 (+2,016)
9,517 (+238)
2,254 (+310)
1,051 (+98)
61,804 (-17,579)
14,126 (-23,600)
14,904 (-3,558)
1,051 (+98)
1,007 (+56)
1,000 (+48)
613 (+42)
14,285 (-1,626)
2,548 (+42)
2,174 (+226)
1,007 (+98)
1,257 (+54)
979 (+48)
11,108 (-4,604)
38,369 (-35,779)
662 (-1,908)
4,768 (+44)
23,034 (+134)
5,931 (-16,991)
1,080 (-21,841)
889 (-21,722)
69,069 (-43,228)
45,740 (+505)
1,009 (+98)
1,006 (+98)
60,668 (+757)
+0.41%
+4.85%
+5.94%
+2.56%
+15.95%
+10.28%
-22.14%
-62.56%
-19.27%
+10.28%
+5.89%
+5.04%
+7.36%
-10.22%
+1.68%
+11.60%
+10.78%
+4.49%
+5.16%
-29.30%
-48.25%
-74.24%
+0.93%
+0.59%
-74.13%
-95.29%
-96.07%
-38.49%
+1.12%
+10.76%
+10.79%
+1.26%
27,388 (+112)
7,854 (-3,729)
74,572 (-20,540)
10,261 (+42)
2,254 (+310)
1,051 (+98)
64,565 (-23,334)
18,926 (-18,800)
20,404 (-5,558)
1,051 (+98)
1,007 (+56)
1,000 (+48)
613 (+42)
73,705 (-24,698)
2,548 (+42)
2,174 (+226)
1,007 (+98)
1,257 (+54)
979 (+48)
87,055 (-52,499)
55,630 (-33,018)
662 (-1,908)
4,768 (+44)
23,034 (+134)
5,931 (-16,991)
1,080 (-21,841)
889 (-21,722)
69,069 (-43,228)
45,740 (+505)
1,009 (+98)
1,006 (+98)
113,574 (-46,870)
+0.41%
-32.19%
-21.60%
+0.41%
+15.95%
+10.28%
-26.55%
-49.83%
-21.41%
+10.28%
+5.89%
+5.04%
+7.36%
-25.10%
+1.68%
+11.60%
+10.78%
+4.49%
+5.16%
-37.62%
-37.25%
-74.24%
+0.93%
+0.59%
-74.13%
-95.29%
-96.07%
-38.49%
+1.12%
+10.76%
+10.79%
-29.21%
55 (0)
28 (0)
7 (0)
4 (0)
3 (0)
7 (0)
5 (0)
7 (0)
16 (0)
1 (0)
2 (0)
2 (0)
1 (0)
11 (0)
2 (0)
4 (0)
3 (0)
6 (0)
4 (0)
6 (0)
5 (0)
2 (0)
6 (0)
24 (0)
24 (0)
21 (0)
54 (0)
12 (0)
18 (0)
6 (0)
1 (0)
20 (0)
Protocol 4,539,109 (+752,090) activateMinter
activeOwedMOf
currentIndex
deactivateMinter
inactiveOwedMOf
isActiveMinter
latestIndex
latestUpdateTimestamp
mintM
minterRate
proposeMint
totalActiveOwedM
updateCollateral
updateIndex
29,475 (+112)
5,854 (+271)
4,762 (+223)
74,564 (-12,468)
1,095 (+98)
1,051 (+56)
613 (+42)
641 (+92)
134,948 (-25,805)
596 (+92)
78,599 (-46,499)
5,515 (+315)
111,320 (-46,107)
44,811 (-21,432)
+0.38%
+4.85%
+4.91%
-14.33%
+9.83%
+5.63%
+7.36%
+16.76%
-16.05%
+18.25%
-37.17%
+6.06%
-29.29%
-32.35%
29,475 (+112)
5,854 (+271)
4,762 (+223)
74,564 (-12,468)
1,095 (+98)
1,051 (+56)
613 (+42)
641 (+92)
134,948 (-25,805)
596 (+92)
78,599 (-46,499)
5,515 (+315)
111,320 (-46,107)
60,718 (-8,122)
+0.38%
+4.85%
+4.91%
-14.33%
+9.83%
+5.63%
+7.36%
+16.76%
-16.05%
+18.25%
-37.17%
+6.06%
-29.29%
-11.80%
29,475 (+112)
5,854 (+271)
4,762 (+223)
74,564 (-12,468)
1,095 (+98)
1,051 (+56)
613 (+42)
641 (+92)
134,948 (-25,805)
596 (+92)
78,599 (-46,499)
5,515 (+315)
111,320 (-46,107)
62,046 (-6,333)
+0.38%
+4.85%
+4.91%
-14.33%
+9.83%
+5.63%
+7.36%
+16.76%
-16.05%
+18.25%
-37.17%
+6.06%
-29.29%
-9.26%
29,475 (+112)
5,854 (+271)
4,762 (+223)
74,564 (-12,468)
1,095 (+98)
1,051 (+56)
613 (+42)
641 (+92)
134,948 (-25,805)
596 (+92)
78,599 (-46,499)
5,515 (+315)
111,320 (-46,107)
75,299 (+3,400)
+0.38%
+4.85%
+4.91%
-14.33%
+9.83%
+5.63%
+7.36%
+16.76%
-16.05%
+18.25%
-37.17%
+6.06%
-29.29%
+4.73%
1 (0)
8 (0)
14 (0)
1 (0)
2 (0)
1 (0)
14 (0)
14 (0)
1 (0)
18 (0)
1 (0)
10 (0)
1 (0)
3 (0)
MToken 2,815,004 (+92,800) balanceOf
currentIndex
earnerRate
latestIndex
latestUpdateTimestamp
mint
startEarning
totalEarningSupply
totalSupply
transfer
updateIndex
1,184 (0)
4,651 (+223)
640 (+92)
524 (+42)
641 (+92)
23,489 (0)
52,394 (+1,431)
5,337 (+223)
5,683 (+223)
75,640 (-1,054)
14,333 (+1,961)
0.00%
+5.04%
+16.79%
+8.71%
+16.76%
0.00%
+2.81%
+4.36%
+4.08%
-1.37%
+15.85%
3,549 (+99)
4,651 (+223)
640 (+92)
524 (+42)
641 (+92)
46,683 (-9,123)
54,294 (+1,589)
5,337 (+223)
7,349 (-777)
75,640 (-1,054)
20,556 (+1,830)
+2.87%
+5.04%
+16.79%
+8.71%
+16.76%
-16.35%
+3.01%
+4.36%
-9.56%
-1.37%
+9.77%
3,184 (0)
4,651 (+223)
640 (+92)
524 (+42)
641 (+92)
46,683 (-9,123)
54,294 (+1,589)
5,337 (+223)
5,683 (+223)
75,640 (-1,054)
19,264 (+1,077)
0.00%
+5.04%
+16.79%
+8.71%
+16.76%
-16.35%
+3.01%
+4.36%
+4.08%
-1.37%
+5.92%
6,006 (+223)
4,651 (+223)
640 (+92)
524 (+42)
641 (+92)
69,877 (-18,246)
56,194 (+1,746)
5,337 (+223)
11,683 (-3,777)
75,640 (-1,054)
26,888 (+1,838)
+3.86%
+5.04%
+16.79%
+8.71%
+16.76%
-20.71%
+3.21%
+4.36%
-24.43%
-1.37%
+7.34%
18 (0)
14 (0)
14 (0)
14 (0)
14 (0)
2 (0)
2 (0)
5 (0)
6 (0)
1 (0)
6 (0)

Copy link

github-actions bot commented Dec 9, 2023

LCOV of commit c270177 during Forge Coverage #57

Summary coverage rate:
  lines......: 89.1% (261 of 293 lines)
  functions..: 64.6% (84 of 130 functions)
  branches...: no data found

Files changed coverage rate:
                                     |Lines       |Functions  |Branches    
  Filename                           |Rate     Num|Rate    Num|Rate     Num
  =========================================================================
  src/ContinuousIndexing.sol         | 100%     14| 100%     8|    -      0
  src/Protocol.sol                   |98.8%    167|74.6%    59|    -      0
  src/libs/UIntMath.sol              | 100%      8| 100%     4|    -      0

@toninorair toninorair changed the title Toni/gas optimizations step 1 Bit packing step 1 Dec 9, 2023
@toninorair toninorair marked this pull request as ready for review December 11, 2023 14:30
src/Protocol.sol Outdated
@@ -527,10 +546,10 @@ contract Protocol is IProtocol, ContinuousIndexing, ERC712 {
if (penaltyBase_ == 0) return;

// Save penalization interval to not double charge for the same missed periods again
_penalizedUntilTimestamps[minter_] = penalizedUntil_;
_minterBasics[minter_].penalizedUntilTimestamp = UIntMath.safe48(penalizedUntil_);
Copy link
Member

Choose a reason for hiding this comment

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

This check should happen when computing penalizedUntil_ in _getPenaltyBaseAndTimeForMissedCollateralUpdates() and this function should return an uint48.

Suggested change
_minterBasics[minter_].penalizedUntilTimestamp = UIntMath.safe48(penalizedUntil_);
_minterBasics[minter_].penalizedUntilTimestamp = penalizedUntil_;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

disagree on this one. It is used once, compared to _getPrincipalValue. If this function returns uint48 , then all internal functions should accept and return uintxx for consistency.

The simplicity of approach working with uint256 and casting to exact data type when storing won't exist anymore and most likely will require readjustment of public interfaces too.

toninorair and others added 2 commits December 11, 2023 13:48
Co-authored-by: Pierrick Turelier <pierrick@turelier.com>
Co-authored-by: Pierrick Turelier <pierrick@turelier.com>
src/Protocol.sol Outdated
_pendingCollateralRetrievals[msg.sender][retrievalId_] = collateral_;
uint128 safeCollateral_ = UIntMath.safe128(collateral_);
_minterBasics[msg.sender].totalPendingRetrievals += safeCollateral_;
_pendingCollateralRetrievals[msg.sender][UIntMath.safe48(retrievalId_)] = safeCollateral_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The overflow check is cheap, but I agree with @PierrickGT. if we overflow the retrieval id's, then the system is broken anyway.

@toninorair toninorair merged commit 29d0bf3 into main Dec 13, 2023
2 checks passed
@toninorair toninorair deleted the toni/gas-optimizations-step-1 branch December 13, 2023 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants