Skip to content

Commit

Permalink
dev: optimize gas usage
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Oct 11, 2023
1 parent a0822bc commit b6a84b4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gas_reports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.0-rc0
scarb-version: 2.3.0-rc1

- name: Run compare_snapshot script
id: run-script
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gas_snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.0-rc0
scarb-version: 2.3.0-rc1

- name: Generate gas snapshot
run: python scripts/gen_snapshot.py
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.0-rc0
scarb-version: 2.3.0-rc1
- run: scarb fmt --check
- run: scarb build
- run: scarb test
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.3.0-rc0
scarb 2.3.0-rc1
105 changes: 86 additions & 19 deletions crates/utils/src/num.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use math::{Zeroable, Oneable};

// === SizeOf ===

trait SizeOf<T> {
Expand Down Expand Up @@ -45,27 +43,33 @@ impl U64SizeOf of SizeOf<u64> {
}

impl U128SizeOf of SizeOf<u128> {
#[inline(always)]
fn size() -> u128 {
128
}
#[inline(always)]
fn size_of(self: @u128) -> u128 {
U128SizeOf::size()
}
}

impl Felt252SizeOf of SizeOf<felt252> {
#[inline(always)]
fn size() -> felt252 {
252
}
#[inline(always)]
fn size_of(self: @felt252) -> felt252 {
Felt252SizeOf::size()
}
}

impl U256SizeOf of SizeOf<u256> {
#[inline(always)]
fn size() -> u256 {
256
}
#[inline(always)]
fn size_of(self: @u256) -> u256 {
U256SizeOf::size()
}
Expand All @@ -84,15 +88,55 @@ trait Zero<T> {
fn is_non_zero(self: @T) -> bool;
}

impl ZeroImpl<T, +Zeroable<T>, +PartialEq<T>, +Drop<T>, +Copy<T>> of Zero<T> {
fn zero() -> T {
Zeroable::zero()
impl Felt252Zero of Zero<felt252> {
#[inline(always)]
fn zero() -> felt252 {
0
}

#[inline(always)]
fn is_zero(self: @felt252) -> bool {
*self == Zero::zero()
}

#[inline(always)]
fn is_non_zero(self: @felt252) -> bool {
!self.is_zero()
}
}


impl U128Zero of Zero<u128> {
#[inline(always)]
fn zero() -> u128 {
0
}

#[inline(always)]
fn is_zero(self: @u128) -> bool {
*self == Zero::zero()
}

#[inline(always)]
fn is_non_zero(self: @u128) -> bool {
!self.is_zero()
}
}


impl U256Zero of Zero<u256> {
#[inline(always)]
fn zero() -> u256 {
0
}
fn is_zero(self: @T) -> bool {
*self == ZeroImpl::zero()

#[inline(always)]
fn is_zero(self: @u256) -> bool {
*self == Zero::zero()
}

fn is_non_zero(self: @T) -> bool {
#[inline(always)]
fn is_non_zero(self: @u256) -> bool {
!self.is_zero()
}
}
Expand All @@ -110,30 +154,53 @@ trait One<T> {
fn is_non_one(self: @T) -> bool;
}

impl OneImpl<T, +Oneable<T>, +PartialEq<T>, +Drop<T>, +Copy<T>> of One<T> {
fn one() -> T {
Oneable::one()
impl Felt252One of One<felt252> {
#[inline(always)]
fn one() -> felt252 {
1
}

fn is_one(self: @T) -> bool {
*self == OneImpl::one()
#[inline(always)]
fn is_one(self: @felt252) -> bool {
*self == One::one()
}

fn is_non_one(self: @T) -> bool {
#[inline(always)]
fn is_non_one(self: @felt252) -> bool {
!self.is_one()
}
}

impl Felt252One of One<felt252> {
fn one() -> felt252 {
impl U128One of One<u128> {
#[inline(always)]
fn one() -> u128 {
1
}

fn is_one(self: @felt252) -> bool {
*self == Felt252One::one()
#[inline(always)]
fn is_one(self: @u128) -> bool {
*self == One::one()
}

fn is_non_one(self: @felt252) -> bool {
#[inline(always)]
fn is_non_one(self: @u128) -> bool {
!self.is_one()
}
}

impl U256One of One<u256> {
#[inline(always)]
fn one() -> u256 {
1
}

#[inline(always)]
fn is_one(self: @u256) -> bool {
*self == One::one()
}

#[inline(always)]
fn is_non_one(self: @u256) -> bool {
!self.is_one()
}
}

0 comments on commit b6a84b4

Please sign in to comment.