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

Gas Optimizations #46

Open
code423n4 opened this issue Jul 17, 2022 · 0 comments
Open

Gas Optimizations #46

code423n4 opened this issue Jul 17, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Hey guy, here are the Gas Optimizations:

-> X = X + Y IS CHEAPER THAN X += Y (Same for X = X - Y is cheaper than X -= Y)

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%3C%20shortest%3B-,idx%20%2B%3D%2032),-%7B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=%7D-,selfptr%20%2B%3D%2032%3B,-otherptr%20%2B%3D%2032

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=%2B%3D%2032%3B-,otherptr%20%2B%3D%2032%3B,-%7D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=%7D-,dest%20%2B%3D%2032%3B,-src%20%2B%3D%2032

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=%2B%3D%2032%3B-,src%20%2B%3D%2032%3B,-%7D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=idx-,%2B%3D,-labelLen%20%2B%201

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=counts-,%2D%3D,-1%3B

-> COMPARISON OPERATORS Problem

In the EVM, there is no opcode for >= or <=. When using greater than or equal, two operations are performed: > and =.
Using strict comparison operators hence saves gas

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=return%20self.length-,%3E%3D,-offset%20%2B%20other

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=for%20(%3B%20len-,%3E%3D,-32%3B%20len%20%2D%3D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=require(char-,%3E%3D,-0x30%20%26%26%20char

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=offset%20%2B%20len-,%3C%3D%20self.length)%3B,-assembly%20%7B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%2B%202-,%3C%3D,-self.length

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%2B%204-,%3C%3D,-self.length

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%2B%2032-,%3C%3D,-self.length

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%2B%2020-,%3C%3D,-self.length

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=require(len-,%3C%3D,-32)%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=idx%20%2B%20len-,%3C%3D,-self.length

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=offset%20%2B%20len-,%3C%3D%20self.length)%3B,-bytes%20memory%20ret

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=require(len-,%3C%3D,-52)%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=0x30%20%26%26%20char-,%3C%3D,-0x7A)%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=require(decoded-,%3C%3D,-0x20)%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=32%3B%20len-,%2D%3D,-32)%20%7B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=bitlen-,%2D%3D,-2%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=bitlen-,%2D%3D,-4%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=bitlen-,%2D%3D,-1%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=bitlen-,%2D%3D,-3%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=return%20iter.offset-,%3E%3D,-iter.data.

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=(iter.offset-,%3E%3D,-iter.data.

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=int32(i2)-,%3E%3D,-0%3B

-> It costs more gas to initialize a varaible to 0 than to let the default of zero be applied

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=for(-,uint%20i%20%3D%200%3B,-i%20%3C%20len

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=for%20(-,uint%20idx%20%3D%200%3B,-idx%20%3C%20shortest

-> ++i costs less gas compared to i++ or i += 1 (Also --i costs less gas compared to i--- or i -= 1)

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=i%20%3C%20len%3B-,i%2B%2B),-%7B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=for(-,uint%20i%20%3D%200%3B,-i%20%3C%20input

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=input.length%3B-,i%2B%2B),-%7B

->SPLITTING REQUIRE() STATEMENTS THAT USE && SAVES GAS

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=require(char%20%3E%3D%200x30%20%26%26%20char%20%3C%3D%200x7A)%3B

-> Usage of Units/Uints smaller than 32Byte (256Bits) incurs overhead

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=decoded%20%3D-,uint8,-(base32HexTable%5Buint

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#:~:text=base32HexTable%5Buint(-,uint8,-(char))%20%2D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=for%20*%3B-,uint16,-constant%20DNSCLASS_IN%20%3D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=%3D%201%3B-,uint16,-constant%20DNSTYPE_DS%20%3D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=%3D%2043%3B-,uint16,-constant%20DNSTYPE_DNSKEY%20%3D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=mapping%20(-,uint8,-%3D%3E%20Algorithm)

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#:~:text=mapping%20(-,uint8,-%3D%3E%20Digest)

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=struct%20SignedSet%20%7B-,uint16%20typeCovered%3B,-uint8%20algorithm%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=uint16%20typeCovered%3B-,uint8%20algorithm%3B,-uint8%20labels%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=uint8%20algorithm%3B-,uint8%20labels%3B,-uint32%20ttl%3B

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#:~:text=uint32%20inception%3B-,uint16%20keytag%3B,-bytes%20signerName%3B

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 17, 2022
code423n4 added a commit that referenced this issue Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant