Skip to content

Conversation

giladchase
Copy link
Contributor

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

@giladchase giladchase marked this pull request as ready for review September 3, 2025 08:30
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch 3 times, most recently from bfebe61 to 1584b10 Compare September 3, 2025 08:54
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi)


corelib/src/byte_array.cairo line 611 at r1 (raw file):

    fn len(self: @Span) -> usize {
        let raw_data_bytes = self.raw_data.len() * BYTES_IN_BYTES31;
        raw_data_bytes - *self.first_char_start_offset + *self.last_char_end_offset

casting everything to felt252 and then casting back to size would be more performant

Code quote:

raw_data_bytes - *self.first_char_start_offset + *self.last_char_end_offset

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @orizi)


corelib/src/byte_array.cairo line 611 at r1 (raw file):

Previously, TomerStarkware wrote…

casting everything to felt252 and then casting back to size would be more performant

this is wrong

@giladchase giladchase changed the base branch from gilad/09-03-refactor_byte_array_hoist_test_utils to graphite-base/8329 September 3, 2025 13:50
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 1584b10 to 2d5edca Compare September 3, 2025 13:50
@giladchase giladchase changed the base branch from graphite-base/8329 to gilad/09-03-refactor_test_remove_compare_byte_array_util_and_rework_tests September 3, 2025 13:50
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 2d5edca to adedbc2 Compare September 3, 2025 14:02
@giladchase giladchase force-pushed the gilad/09-03-refactor_test_remove_compare_byte_array_util_and_rework_tests branch from a2f209e to 195103b Compare September 3, 2025 14:02
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)


corelib/src/byte_array.cairo line 593 at r2 (raw file):

/// `Span` implements the `Copy` and the `Drop` traits.
#[derive(Copy, Drop)]
pub struct Span {

probably.

Suggestion:

pub struct ByteSpan {

@giladchase giladchase changed the base branch from gilad/09-03-refactor_test_remove_compare_byte_array_util_and_rework_tests to graphite-base/8329 September 4, 2025 04:31
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from adedbc2 to 848910c Compare September 4, 2025 04:31
@giladchase giladchase changed the base branch from graphite-base/8329 to gilad/09-04-refactor_test_remove_fixed-size_bytearray_testutils September 4, 2025 04:32
@giladchase giladchase force-pushed the gilad/09-04-refactor_test_remove_fixed-size_bytearray_testutils branch from 2263c9e to 5dd3247 Compare September 4, 2025 04:38
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch 2 times, most recently from 9c1bdd0 to 2b36019 Compare September 4, 2025 04:46
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 1 of 2 files reviewed, 2 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 593 at r2 (raw file):

Previously, orizi wrote…

probably.

Done.


corelib/src/byte_array.cairo line 598 at r3 (raw file):

    /// slices.
    /// Value should be in the range [0, 30].
    pub(crate) first_char_start_offset: usize,

Replace here and other with BoundedInt

Code quote:

    /// Value should be in the range [0, 30].
    pub(crate) first_char_start_offset: usize,

@giladchase giladchase force-pushed the gilad/09-04-refactor_test_remove_fixed-size_bytearray_testutils branch from 5dd3247 to b19fb2c Compare September 4, 2025 09:03
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 634 at r13 (raw file):

Previously, orizi wrote…

bounded-int arithmetics can help here to make only a single cast here.

Thanks.
Unless i'm missing something, i think this might be a bit tricky to do: bounded_sub(remainder_len , start_offset) can in practice be a negative BoundedInt<-30,30> (fore example span.slice(1,span_len() has offset=1 and remainder_len=0 ), and similarly bounded_sub(start_offset,remainder_len) can be negative.
Both cases will require another custom adder between a data_bytes: usize and whatever signed type we upcast the negative number to, which might be more trouble than an extra upcast.


corelib/src/byte_array.cairo line 651 at r13 (raw file):

Previously, orizi wrote…

or something of that manner.

Done.

Interestingly, this version seems to be the best performing version, in particular, it's than using data.is_empty() instead of data.len() == 0.
I verified this by (locally) changing array::Span::is_empty from it's current pop_front approach into something more similar to how Span::len is implemented (specifically array_len(self.snapshot) == 0 and gas usage decreased slightly in the relevant test in array_test.cairo.


corelib/src/byte_array.cairo line 64 at r13 (raw file):

/// The number of bytes in
pub type WordBytes = BoundedInt<0, BYTES_IN_BYTES31_MINUS_ONE_FELT>;

Done.
Note that once we make ByteArray use this typedef we might want to pub(crate) this typdef, since it will be used in a pub(crate) field of ByteArray (pending_word_len).

Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 651 at r13 (raw file):

Previously, giladchase wrote…

Done.

Interestingly, this version seems to be the best performing version, in particular, it's than using data.is_empty() instead of data.len() == 0.
I verified this by (locally) changing array::Span::is_empty from it's current pop_front approach into something more similar to how Span::len is implemented (specifically array_len(self.snapshot) == 0 and gas usage decreased slightly in the relevant test in array_test.cairo.

BTW the reason why start_offset isn't checked is because start_offset is always < self.remainder_len if data.len() == 0 (when start_offset >= remainder_len and data == [] then slice returns None)

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from e89a587 to 795d8e9 Compare September 18, 2025 08:17
@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch 2 times, most recently from 4e68a32 to a6ae7c7 Compare September 28, 2025 08:26
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 64 at r13 (raw file):

Previously, giladchase wrote…

Done.
Note that once we make ByteArray use this typedef we might want to pub(crate) this typdef, since it will be used in a pub(crate) field of ByteArray (pending_word_len).

The Done is a lie.
Now it's done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @TomerStarkware)


corelib/src/byte_array.cairo line 634 at r15 (raw file):

    fn len(self: @ByteSpan) -> usize {
        let data_bytes = self.data.len() * BYTES_IN_BYTES31;
        data_bytes + upcast(self.remainder_len) - upcast(self.first_char_start_offset)

Suggestion:

        downcast(bounded_int::sub(
            bounded_int::add(
                bounded_int::mul(self.data.len(), BYTES_IN_BYTES31),
                self.remainder_len),
            ),
            self.first_char_start_offset,
        )).unwrap()

corelib/src/byte_array.cairo line 651 at r15 (raw file):

    /// ```
    fn is_empty(self: @ByteSpan) -> bool {
        self.data.len() == 0 && *self.remainder_len == 0

can you check both: (in any case - opposite order probably marginally better)

Suggestion:

        *self.remainder_len == 0 && self.data.len() == 0
        
        |||
        
        *self.remainder_len == 0 && self.data.is_empty()

corelib/src/byte_array.cairo line 655 at r15 (raw file):

}

pub trait ToByteSpanTrait<C> {

doc

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from a6ae7c7 to 76e8b98 Compare September 28, 2025 12:30
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 651 at r15 (raw file):

*self.remainder_len == 0 && self.data.len() == 0

wins, the is_empty variant is a tad slower than this one, so i'm picking the len variant unless you still prefer the is_empty option.

Both of yr suggestions are faster than current code due to the order switch.


corelib/src/byte_array.cairo line 634 at r15 (raw file):

    fn len(self: @ByteSpan) -> usize {
        let data_bytes = self.data.len() * BYTES_IN_BYTES31;
        data_bytes + upcast(self.remainder_len) - upcast(self.first_char_start_offset)

Done.
I added the operator impls in a helpers mod like in bytes_31.cairo.
Should i define U32ByB31 in bytes_31? Wasn't sure if it's interesting enough

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 76e8b98 to 3e3bc95 Compare September 28, 2025 13:00
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @giladchase and @TomerStarkware)


corelib/src/byte_array.cairo line 65 at r17 (raw file):

/// The number of bytes in [`ByteArray::pending_word`].
type WordBytes = BoundedInt<0, { BYTES_IN_BYTES31_MINUS_ONE.into() }>;
type BYTES_IN_BYTES31_TYPED = UnitInt<{ BYTES_IN_BYTES31.into() }>;

this is a type - not a const.


corelib/src/byte_array.cairo line 707 at r17 (raw file):

    }
}
use helpers::{B30AddU32ByB31, B30AddU32ByB31SubB30, U32ByB31};

if you want these just for a single function - add the use within it.
better yet - just add the len calculation as a function in the helper.
(and remove the pub from the impls)

Code quote:

use helpers::{B30AddU32ByB31, B30AddU32ByB31SubB30, U32ByB31};

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch 2 times, most recently from 43bf5aa to 3cd790e Compare September 29, 2025 10:15
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 655 at r15 (raw file):

Previously, orizi wrote…

doc

Done.


corelib/src/byte_array.cairo line 65 at r17 (raw file):

Previously, orizi wrote…

this is a type - not a const.

Done.


corelib/src/byte_array.cairo line 707 at r17 (raw file):

Previously, orizi wrote…

if you want these just for a single function - add the use within it.
better yet - just add the len calculation as a function in the helper.
(and remove the pub from the impls)

This OK? Or also add push the downcast into it?

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @giladchase and @TomerStarkware)


corelib/src/byte_array.cairo line 707 at r17 (raw file):

Previously, giladchase wrote…

This OK? Or also add push the downcast into it?

downcast as well IMO - would be better encapsulation.


corelib/src/byte_array.cairo line 692 at r18 (raw file):

    impl U32ByB31 of MulHelper<u32, BytesInBytes31Typed> {
        type Result = BoundedInt<0, { U32_MAX_TIMES_B31 }>;

Suggestion:

        type Result = BoundedInt<0, U32_MAX_TIMES_B31>;

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch 2 times, most recently from 19dc717 to 78477ed Compare September 29, 2025 10:25
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 707 at r17 (raw file):

Previously, orizi wrote…

downcast as well IMO - would be better encapsulation.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @giladchase and @TomerStarkware)


corelib/src/byte_array.cairo line 715 at r20 (raw file):

    }
}
use helpers::calc_bytespan_len;

no need for the use IMO.

Code quote:

use helpers::calc_bytespan_len;

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 78477ed to 8e174c0 Compare September 29, 2025 10:35
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 715 at r20 (raw file):

Previously, orizi wrote…

no need for the use IMO.

Right, replaced with a qualified path.


corelib/src/byte_array.cairo line 692 at r18 (raw file):

    impl U32ByB31 of MulHelper<u32, BytesInBytes31Typed> {
        type Result = BoundedInt<0, { U32_MAX_TIMES_B31 }>;

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


corelib/src/byte_array.cairo line 650 at r21 (raw file):

    /// ```
    fn is_empty(self: ByteSpan) -> bool {
        self.remainder_len == 0 && self.data.len() == 0

doc why this is enough.

Code quote:

self.remainder_len == 0

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 8e174c0 to 5141e98 Compare September 29, 2025 14:11
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 2 unresolved discussions (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 650 at r21 (raw file):

Previously, orizi wrote…

doc why this is enough.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @giladchase and @TomerStarkware)


corelib/src/byte_array.cairo line 650 at r21 (raw file):

Previously, giladchase wrote…

Done.

do the implementation - not the function.

@giladchase giladchase force-pushed the gilad/09-03-feat_byte_array_add_bytearray_span_and_span_len_ branch from 5141e98 to e479077 Compare September 30, 2025 09:23
Copy link
Contributor Author

@giladchase giladchase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi and @TomerStarkware)


corelib/src/byte_array.cairo line 650 at r21 (raw file):

Previously, orizi wrote…

do the implementation - not the function.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed all commit messages.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)


corelib/src/byte_array.cairo line 650 at r23 (raw file):

    /// ```
    fn is_empty(self: ByteSpan) -> bool {
        // No partial bytes31-sized words in `remainder_word`, nor any full words in `data`.

i meant that you need to doc why self.remainder_len == 0 is enough.
since you COULD have had something like "abcd".get(1..)

Code quote:

        // No partial bytes31-sized words in `remainder_word`, nor any full words in `data`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants