-
Notifications
You must be signed in to change notification settings - Fork 4
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
Save gas by packing total and expiresAt into the same 256-bit word in the Offer struct #45
Comments
Aren’t those variables already in the same 256 bit word? They just have to be in the same word I think, not next to each other in that word. |
Nope! The offer struct falls across two 256-bit words. As they originally structured it: The first 256-bit word is: The second 256-bit word is: So not only are they not next to each other in memory, they are not in the same word. According to my suggestion, it would be structured as: The first 256-bit word would be: The second 256-bit word would be: |
Ah thanks. I forgot a unit conversion from 20 bytes to 160 bits for the address. Thanks. Anyway, as I’ve stated here, this doesn’t help save gas anyway: #46 (comment) |
Thanks @michaelKim4736 for the feedback! We will take this into consideration. |
Thanks for your participation, @michaelKim4736! Our team has reviewed your submission and decided to reject the issue. |
Description
The
Offer
struct is composed of two 256-bit words. Since the function.updateOffer()
only updates a portion of this struct (thetotal
and theexpiresAt
portions), we can pack these two properties together to save gas.Scenario
If the
total
and theexpiresAt
properties are placed in the same word, we can save gas by only having to update one 256-bit word rather than two 256-bit words.As they are currently structured:
The first 256-bit word is:
64 bits of
expiresAt
160 bits of
bidder
16 bits of
offerCut
The first 16 bits of
total
The second 256-bit word is:
The remaining 112 bits of
total
128 bits of
unsuccessfulFee
So
total
andexpiresAt
are not in the same word.According to my suggestion, it would be structured as:
The first 256-bit word would be:
64 bits of
expiresAt
128 bits of
total
The first 64 bits of
bidder
The second 256-bit word would be:
The remaining 96 bits of
bidder
16 bits of
offerCut
128 bits of
unsuccessfulFee
Impact
Gas Savings: We can save gas by only paying 5000 gas to update one 256-bit word rather than 10,000 gas to update 256-bit words.
Reproduction
Observe that the
expiresAt
property is in the first 256-bit word of theOffer
struct, whereas thetotal
property is in the second 256-bit word:Fix
Reorder the struct to have the
expiresAt
property and thetotal
property in the same half of the struct:The text was updated successfully, but these errors were encountered: