Open code423n4 opened 2 years ago
Love this report -- thanks for the feedback.
Omit hash in contract salts
This is an interesting suggestion! We'll test it out.
Optimize BytesLibrary.startsWith
Great suggestions, will consider these.
Thanks! FYI, Saw-mon & Natalie came up with a cleaner, cheaper assembly startsWith
in their submission here. You can just mload
the 4 signature bytes and don't need to use a mask.
Gas Optimizations
Omit hash in contract salts
NFTCollectionFactory
generates a unique salt per collection by concatenating the creator address and a nonce and returning the hash of the concatenated values.NFTCollectionFactory._getSalt
:If you are willing to constrain the
nonce
value to auint96
, you can save a bit of gas by omitting thekeccak
hash and packing thecreator
address andnonce
together in abytes32
. Since this is paid by creators on every new drop and collection, small savings here may add up.Original implementation: 22974 gas.
Optimized: 22457 gas:
Optimize
BytesLibrary.startsWith
BytesLibrary.startsWith
checks whether the givenbytes callData
begin with a providedbytes4 functionSig
. It performs this check by iterating over each individual byte:Original implementation (~2150 gas):
There are a few options to optimize this function, depending on whether you want to use inline assembly.
No assembly required: cast
callData
tobytes4
It’s possible to convert
callData
to abytes4
(truncating the remainder), and compare directly withfunctionSig
. This saves about 850 gas.Some assembly required: cast
callData
tobytes4
in assemblyIf you’re up for some low level assembly, you can perform the same truncation more cheaply by assigning
callData
to abytes4
in assembly. This saves about 1300 gas.Assembly required: perform comparison in assembly
If you really want to show off, you can perform the whole comparison in assembly and save an additional 50-ish gas:
(This last one is probably not worth it, but it was fun to work out).