Skip to content

Conversation

@yczhangsjtu
Copy link
Collaborator

@yczhangsjtu yczhangsjtu commented Nov 6, 2024

Extracting small PRs from #294

There was an implementation of Iterator for iterating through a FieldType. Turns out that it can be replaced with existing tools in iter_tools. This proposes the new implementation.

Also added some other utility functions for type conversion between FieldType and Vec<E>, to avoid match field_type everywhere.

@yczhangsjtu yczhangsjtu changed the title Add and reimplement some utility functions. BaseFold: Add and reimplement some utility functions. Nov 6, 2024
@matthiasgoergens
Copy link
Contributor

Thanks for breaking down the big PR into more digestible chunks!

Copy link
Contributor

@matthiasgoergens matthiasgoergens left a comment

Choose a reason for hiding this comment

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

Could you please add in the PR description why we are making this refactoring?

Just a quick note about what task becomes easier or simpler with the new organisation?

Thanks!

) -> impl Iterator<Item = &E::BaseField> + '_ {
match values {
FieldType::Ext(coeffs) => Either::Left(coeffs.iter().flat_map(|x| x.as_bases())),
FieldType::Base(coeffs) => Either::Right(coeffs.iter()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

does the Either::Left and Either::Right here is nessesary? Because return type are the same, here somehow Either is a bit over design

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No the return types are not the same. They are both iterators over BaseField but different types of Iterator.

Copy link
Collaborator Author

@yczhangsjtu yczhangsjtu Nov 28, 2024

Choose a reason for hiding this comment

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

What's the standard solution for this? The Either is some solution I found on the internet after many fruitless fightings with the compiler. Any better suggestions are welcome. I'm really not an expert in rust iterators.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another option is rely on runtime dispatch.

diff --git a/mpcs/src/util.rs b/mpcs/src/util.rs
index 7688b53e..6edd82d4 100644
--- a/mpcs/src/util.rs
+++ b/mpcs/src/util.rs
@@ -182,10 +182,10 @@ pub fn field_type_as_ext<E: ExtensionField>(values: &FieldType<E>) -> &Vec<E> {

 pub fn field_type_iter_base<E: ExtensionField>(
     values: &FieldType<E>,
-) -> impl Iterator<Item = &E::BaseField> + '_ {
+) -> Box<dyn Iterator<Item = &E::BaseField> + '_> {
     match values {
-        FieldType::Ext(coeffs) => Either::Left(coeffs.iter().flat_map(|x| x.as_bases())),
-        FieldType::Base(coeffs) => Either::Right(coeffs.iter()),
+        FieldType::Ext(coeffs) => Box::new(coeffs.iter().flat_map(|x| x.as_bases())),
+        FieldType::Base(coeffs) => Box::new(coeffs.iter()),
         _ => unreachable!(),
     }
 }

After reading document in Either, I think static dispatch via Either is better, and either already implement iterator so it's truly works this purpose. So LGTM under this scope! 👍

Copy link
Collaborator

@hero78119 hero78119 left a comment

Choose a reason for hiding this comment

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

👍

@yczhangsjtu yczhangsjtu merged commit 1f5b990 into master Nov 28, 2024
@yczhangsjtu yczhangsjtu deleted the feat/basefold-refactor-extract-2 branch November 28, 2024 07:00
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