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

implemented iterable squashed felt252 dict #7289

Merged
merged 1 commit into from
Feb 17, 2025

Conversation

dean-starkware
Copy link
Collaborator

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

@dean-starkware dean-starkware marked this pull request as ready for review February 16, 2025 11:28
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 9 files reviewed, 1 unresolved discussion (waiting on @gilbens-starkware)


corelib/src/dict.cairo line 89 at r1 (raw file):

extern fn squashed_dict_into_entries<T>(
    dict: SquashedFelt252Dict<T>,
) -> Array<(felt252, T, T)> nopanic;

Have only a single method directly wrapping this function in this PR.

revert all other cairo code.

Code quote:

extern fn squashed_dict_into_entries<T>(
    dict: SquashedFelt252Dict<T>,
) -> Array<(felt252, T, T)> nopanic;

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.

Reviewed 5 of 9 files at r1.
Reviewable status: 5 of 9 files reviewed, 3 unresolved discussions (waiting on @dean-starkware and @gilbens-starkware)


a discussion (no related file):
add a test in e2e tests.


crates/cairo-lang-sierra/src/extensions/modules/squashed_felt252_dict.rs line 57 at r1 (raw file):

pub struct SquashedDictAsEntriesLibfuncWrapped;
impl SignatureAndTypeGenericLibfunc for SquashedDictAsEntriesLibfuncWrapped {
    const STR_ID: &'static str = "squashed_dict_into_entries";

Suggestion:

    const STR_ID: &'static str = "squashed_felt252_dict_into_entries";

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from 051b070 to af9545b Compare February 16, 2025 13:19
Copy link
Collaborator Author

@dean-starkware dean-starkware 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: 5 of 9 files reviewed, 3 unresolved discussions (waiting on @gilbens-starkware and @orizi)


corelib/src/dict.cairo line 89 at r1 (raw file):

Previously, orizi wrote…

Have only a single method directly wrapping this function in this PR.

revert all other cairo code.

Done.


crates/cairo-lang-sierra/src/extensions/modules/squashed_felt252_dict.rs line 57 at r1 (raw file):

pub struct SquashedDictAsEntriesLibfuncWrapped;
impl SignatureAndTypeGenericLibfunc for SquashedDictAsEntriesLibfuncWrapped {
    const STR_ID: &'static str = "squashed_dict_into_entries";

Done.

Copy link
Contributor

@gilbens-starkware gilbens-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: 5 of 9 files reviewed, 4 unresolved discussions (waiting on @dean-starkware and @orizi)


corelib/src/dict.cairo line 288 at r2 (raw file):

/// Basic trait for the `SquashedFelt252Dict` type.
pub trait SquashedFelt252DictTrait<T> {
    /// Returns an array of `(key, value, previous_value)` tuples.

And maybe mention that the first value is always 0.

Suggestion:

`(key, first_value, last_value)` tuples.

Copy link
Contributor

@gilbens-starkware gilbens-starkware 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: 5 of 9 files reviewed, 5 unresolved discussions (waiting on @dean-starkware and @orizi)


corelib/src/test/dict_test.cairo line 179 at r2 (raw file):

        assert_eq!(new_value, i, "New value should match expected value");
        i += 1;
    }

Otherwise it'll pass even if the returned array is empty.
You can also zip over the entries and the range 0..n.

Suggestion:

    let n = 5_u32;
    let mut dict: Felt252Dict<u32> = (0..n).into_iter().map(|x| (x.into(), x)).collect();
    let squashed_dict_entries = dict.squash().into_entries();
    let mut i = 0;
    let mut span = squashed_dict_entries.span();
    while let Some(entry) = span.pop_front() {
        let (key, _old_value, new_value) = *entry;
        assert_eq!(key, i.into(), "Key should match expected index");
        assert_eq!(new_value, i, "New value should match expected value");
        i += 1;
    }
    assert_eq!(i, n);

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.

Reviewed 1 of 9 files at r1, 2 of 3 files at r2.
Reviewable status: 8 of 9 files reviewed, 4 unresolved discussions (waiting on @dean-starkware and @gilbens-starkware)


corelib/src/test/dict_test.cairo line 179 at r2 (raw file):

        assert_eq!(new_value, i, "New value should match expected value");
        i += 1;
    }

Suggestion:

    assert_eq!(dict.squash().into_entries(), array![
        (0, 0, 0),
        (1, 0, 1),
        (2, 0, 2),
        (3, 0, 3),
        (4, 0, 4),
    ]);

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from af9545b to c6c4893 Compare February 16, 2025 14:41
Copy link
Collaborator Author

@dean-starkware dean-starkware 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: 8 of 9 files reviewed, 4 unresolved discussions (waiting on @gilbens-starkware and @orizi)


a discussion (no related file):

Previously, orizi wrote…

add a test in e2e tests.

Done.


corelib/src/dict.cairo line 288 at r2 (raw file):

Previously, gilbens-starkware (Gil Ben-Shachar) wrote…

And maybe mention that the first value is always 0.

Done.


corelib/src/test/dict_test.cairo line 179 at r2 (raw file):

Previously, gilbens-starkware (Gil Ben-Shachar) wrote…

Otherwise it'll pass even if the returned array is empty.
You can also zip over the entries and the range 0..n.

After Ori's suggestion I don't think it's valid anymore. Tell me if it is.


corelib/src/test/dict_test.cairo line 179 at r2 (raw file):

        assert_eq!(new_value, i, "New value should match expected value");
        i += 1;
    }

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.

Reviewed 1 of 3 files at r3.
Reviewable status: 8 of 10 files reviewed, 5 unresolved discussions (waiting on @gilbens-starkware)


tests/e2e_test_data/libfuncs/felt252_dict line 684 at r3 (raw file):


test::foo@0([0]: Felt252Dict<felt252>, [1]: felt252, [2]: felt252, [3]: felt252, [4]: felt252) -> (Tuple<Felt252Dict<felt252>, felt252, felt252>);

add another test with a nullable.


tests/e2e_test_data/libfuncs/felt252_dict line 697 at r3 (raw file):

fn test_squashed_dict_entries(dict: SquashedFelt252Dict<felt252>) -> Array<(felt252, felt252, felt252)> {
    dict::SquashedFelt252DictImpl::<felt252>::into_entries(dict)
}

run through formatter and re-paste here.

Code quote:

fn test_squashed_dict_entries(dict: SquashedFelt252Dict<felt252>) -> Array<(felt252, felt252, felt252)> {
    dict::SquashedFelt252DictImpl::<felt252>::into_entries(dict)
}

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from c6c4893 to 53b1b6e Compare February 16, 2025 14:51
Copy link
Contributor

@gilbens-starkware gilbens-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 9 files at r1, 2 of 3 files at r3.
Reviewable status: 6 of 11 files reviewed, 4 unresolved discussions (waiting on @dean-starkware and @orizi)


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 66 at r3 (raw file):

        CoreConcreteLibfunc::Felt252SquashedDict(_) => {
            vec![ApChange::Known(0)]
        }

And move to be after Felt252Dict. Same for the cost match.

Suggestion:

        Felt252SquashedDict(_) => {
            vec![ApChange::Known(0)]
        }

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: 6 of 11 files reviewed, 4 unresolved discussions (waiting on @dean-starkware and @gilbens-starkware)


corelib/src/dict.cairo line 298 at r3 (raw file):

    fn into_entries(self: SquashedFelt252Dict<T>) -> Array<(felt252, T, T)>;
}
impl SquashedFelt252DictImpl<T, +Drop<T>> of SquashedFelt252DictTrait<T> {

not required.

possibly also just use #[generate_trait] now.

Suggestion:

impl SquashedFelt252DictImpl<T> of SquashedFelt252DictTrait<T> {

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from 53b1b6e to afa1b42 Compare February 17, 2025 09:28
Copy link
Collaborator Author

@dean-starkware dean-starkware 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: 6 of 11 files reviewed, 4 unresolved discussions (waiting on @gilbens-starkware and @orizi)


corelib/src/dict.cairo line 298 at r3 (raw file):

Previously, orizi wrote…

not required.

possibly also just use #[generate_trait] now.

Done.


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 66 at r3 (raw file):

Previously, gilbens-starkware (Gil Ben-Shachar) wrote…

And move to be after Felt252Dict. Same for the cost match.

Done.


tests/e2e_test_data/libfuncs/felt252_dict line 684 at r3 (raw file):

Previously, orizi wrote…

add another test with a nullable.

Is this what you meant? If so, why is it important?


tests/e2e_test_data/libfuncs/felt252_dict line 697 at r3 (raw file):

Previously, orizi wrote…

run through formatter and re-paste here.

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.

Reviewed 2 of 5 files at r4, 4 of 5 files at r5, all commit messages.
Reviewable status: 10 of 11 files reviewed, 2 unresolved discussions (waiting on @gilbens-starkware)


tests/e2e_test_data/libfuncs/felt252_dict line 684 at r3 (raw file):

Previously, dean-starkware wrote…

Is this what you meant? If so, why is it important?

because it is different enough as a case.


tests/e2e_test_data/libfuncs/felt252_dict line 740 at r5 (raw file):

) -> core::option::Option<Array<(felt252, felt252, felt252)>> {
    Some(dict::SquashedFelt252DictImpl::<felt252>::into_entries(dict))
}

Suggestion:

fn test_squashed_dict_entries(
    dict: SquashedFelt252Dict<Nullable<u256>>,
) -> core::option::Option<Array<(felt252, Nullable<u256>, Nullable<u256>)>> {
    Some(dict::SquashedFelt252DictImpl::<felt252>::into_entries(dict))
}

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from afa1b42 to 8775d59 Compare February 17, 2025 10:31
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.

Reviewed 1 of 5 files at r5, 1 of 1 files at r6, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @dean-starkware and @gilbens-starkware)

@dean-starkware dean-starkware force-pushed the dean/iterable_squashed_felt252_dict branch from 8775d59 to 9246334 Compare February 17, 2025 10:52
Copy link
Collaborator Author

@dean-starkware dean-starkware 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: all files reviewed, 2 unresolved discussions (waiting on @gilbens-starkware and @orizi)


tests/e2e_test_data/libfuncs/felt252_dict line 740 at r5 (raw file):

) -> core::option::Option<Array<(felt252, felt252, felt252)>> {
    Some(dict::SquashedFelt252DictImpl::<felt252>::into_entries(dict))
}

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.

:lgtm:

Reviewed 1 of 1 files at r7, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @gilbens-starkware)

Copy link
Contributor

@gilbens-starkware gilbens-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 9 files at r1, 4 of 5 files at r4, 3 of 5 files at r5, 1 of 1 files at r6, 1 of 1 files at r7, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @dean-starkware)

@orizi orizi added this pull request to the merge queue Feb 17, 2025
Merged via the queue into main with commit 57407d4 Feb 17, 2025
47 checks passed
@orizi orizi deleted the dean/iterable_squashed_felt252_dict branch February 17, 2025 11:58
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