From b6a84b47a729d8df94682c0c86dd1aff418debff Mon Sep 17 00:00:00 2001 From: msaug Date: Wed, 11 Oct 2023 12:21:42 +0700 Subject: [PATCH] dev: optimize gas usage --- .github/workflows/gas_reports.yml | 2 +- .github/workflows/gas_snapshot.yml | 2 +- .github/workflows/test.yml | 2 +- .tool-versions | 2 +- crates/utils/src/num.cairo | 105 +++++++++++++++++++++++------ 5 files changed, 90 insertions(+), 23 deletions(-) diff --git a/.github/workflows/gas_reports.yml b/.github/workflows/gas_reports.yml index 6f443b411..e838fb817 100644 --- a/.github/workflows/gas_reports.yml +++ b/.github/workflows/gas_reports.yml @@ -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 diff --git a/.github/workflows/gas_snapshot.yml b/.github/workflows/gas_snapshot.yml index cadbd2214..edc8d1dd4 100644 --- a/.github/workflows/gas_snapshot.yml +++ b/.github/workflows/gas_snapshot.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c0d2afd61..72381644a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/.tool-versions b/.tool-versions index 54917fa62..ff11276f7 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -scarb 2.3.0-rc0 +scarb 2.3.0-rc1 diff --git a/crates/utils/src/num.cairo b/crates/utils/src/num.cairo index 02b54abe5..b612d69e8 100644 --- a/crates/utils/src/num.cairo +++ b/crates/utils/src/num.cairo @@ -1,5 +1,3 @@ -use math::{Zeroable, Oneable}; - // === SizeOf === trait SizeOf { @@ -45,27 +43,33 @@ impl U64SizeOf of SizeOf { } impl U128SizeOf of SizeOf { + #[inline(always)] fn size() -> u128 { 128 } + #[inline(always)] fn size_of(self: @u128) -> u128 { U128SizeOf::size() } } impl Felt252SizeOf of SizeOf { + #[inline(always)] fn size() -> felt252 { 252 } + #[inline(always)] fn size_of(self: @felt252) -> felt252 { Felt252SizeOf::size() } } impl U256SizeOf of SizeOf { + #[inline(always)] fn size() -> u256 { 256 } + #[inline(always)] fn size_of(self: @u256) -> u256 { U256SizeOf::size() } @@ -84,15 +88,55 @@ trait Zero { fn is_non_zero(self: @T) -> bool; } -impl ZeroImpl, +PartialEq, +Drop, +Copy> of Zero { - fn zero() -> T { - Zeroable::zero() +impl Felt252Zero of Zero { + #[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 { + #[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 { + #[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() } } @@ -110,30 +154,53 @@ trait One { fn is_non_one(self: @T) -> bool; } -impl OneImpl, +PartialEq, +Drop, +Copy> of One { - fn one() -> T { - Oneable::one() +impl Felt252One of One { + #[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 { - fn one() -> felt252 { +impl U128One of One { + #[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 { + #[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() } }